Skip to contents

For many mediation and outcome models, we simply want to apply a univariate model across all response variable. Parallelize enables this conversion. For example, applying parallelize to ranger() returns a function that estimates separate random forest models for each response on the left hand side of a formula.

Usage

parallelize(f, progress = TRUE)

Arguments

f

A function for estimating a single response model given a formula and input dataset. This is the model that we would like to parallelize across responses.

progress

A logical indicating whether to show a progress bar.

Value

f_multi A function that takes a formula and dataset and applies f to each response on the left hand side of the original formula.

Examples

mat <- data.frame(matrix(rnorm(100), 25, 4))
colnames(mat) <- c("y1", "y2", "x1", "x2")
plm <- parallelize(lm)
plm(y1 + y2 ~ x1 + x2, mat)
#> $y1
#> 
#> Call:
#> f(formula = fmla, data = ..1)
#> 
#> Coefficients:
#> (Intercept)           x1           x2  
#>     -0.2283      -0.3802       0.2084  
#> 
#> 
#> $y2
#> 
#> Call:
#> f(formula = fmla, data = ..1)
#> 
#> Coefficients:
#> (Intercept)           x1           x2  
#>     0.12993      0.03197      0.22464  
#> 
#> 

prf <- parallelize(ranger::ranger)
prf(mpg + hp ~ wt + disp + cyl, data = mtcars)
#> $mpg
#> Ranger result
#> 
#> Call:
#>  f(fmla, ...) 
#> 
#> Type:                             Regression 
#> Number of trees:                  500 
#> Sample size:                      32 
#> Number of independent variables:  3 
#> Mtry:                             1 
#> Target node size:                 5 
#> Variable importance mode:         none 
#> Splitrule:                        variance 
#> OOB prediction error (MSE):       5.87662 
#> R squared (OOB):                  0.8382171 
#> 
#> $hp
#> Ranger result
#> 
#> Call:
#>  f(fmla, ...) 
#> 
#> Type:                             Regression 
#> Number of trees:                  500 
#> Sample size:                      32 
#> Number of independent variables:  3 
#> Mtry:                             1 
#> Target node size:                 5 
#> Variable importance mode:         none 
#> Splitrule:                        variance 
#> OOB prediction error (MSE):       1960.957 
#> R squared (OOB):                  0.5828521 
#>