Ben
Ben

Reputation: 65

lapply with multiple lists and functions for regression models

I'd like to run four multilevel models (using lmer) simultaneously using lapply.

A simple example using lm() with one dependent variable and a list of independent variables would be:

data(mtcars)
varlist <- names(mtcars)[3:6]

models <- lapply(varlist, function(x) {
  lm(substitute(mpg ~ i, list(i = as.name(x))), data = mtcars)
})

How can I expand this to run four lmer() models, each having a different dependent variable and a different list of independent variables? The two levels would remain the same for all four models. Four (bogus) example models would be:

data(mtcars)
library(lme4)

model1 <- lmer(mpg ~ cyl + disp + hp + (1 | am) +  (1 | vs), data = mtcars)

model2 <- lmer(cyl ~ mpg + disp + qsec + (1 | am) +  (1 | vs), data = mtcars)

model3 <- lmer(disp ~ mpg + cyl + carb + (1 | am) +  (1 | vs), data = mtcars)

model4 <- lmer(qsec ~ mpg + cyl + drat + (1 | am) +  (1 | vs), data = mtcars)

Any ideas?

Upvotes: 4

Views: 952

Answers (1)

akrun
akrun

Reputation: 887951

We can have a list of dependent (or vector) and independent variables and pass that into Map to create the formula and apply the lmer. The unit element of a list would be the vector here for independent variables and the single element for dependent variable.

library(lme4)
indep_var_list <- list(c("cyl", "disp", "hp"),
                   c("mpg", "disp", "qsec"),
                    c("mpg", "cyl", "carb"),
                    c("mpg", "cyl", "drat"))

dep_vars <- c("mpg", "cyl", "disp", "qsec")

out <- Map(function(x, y) {

       fmla <-  as.formula(paste(y, "~ ", paste(x, collapse= " + ") ,
                            " + (1 | am) + (1 | vs)"))
       model <- lmer(fmla, data = mtcars)
       model
       }, indep_var_list, dep_vars)
       
 

-output

[1]]
Linear mixed model fit by REML ['lmerMod']
Formula: mpg ~ cyl + disp + hp + (1 | am) + (1 | vs)
   Data: mtcars
REML criterion at convergence: 169.5913
Random effects:
 Groups   Name        Std.Dev.
 am       (Intercept) 2.209   
 vs       (Intercept) 0.000   
 Residual             2.831   
Number of obs: 32, groups:  am, 2; vs, 2
Fixed Effects:
(Intercept)          cyl         disp           hp  
   32.55270     -0.90447     -0.00972     -0.02971  
convergence code 0; 0 optimizer warnings; 1 lme4 warnings 

[[2]]
Linear mixed model fit by REML ['lmerMod']
Formula: cyl ~ mpg + disp + qsec + (1 | am) + (1 | vs)
   Data: mtcars
REML criterion at convergence: 78.0586
Random effects:
 Groups   Name        Std.Dev.
 am       (Intercept) 0.5773  
 vs       (Intercept) 0.4491  
 Residual             0.5743  
Number of obs: 32, groups:  am, 2; vs, 2
Fixed Effects:
(Intercept)          mpg         disp         qsec  
  10.592032    -0.045832     0.006052    -0.279176  

[[3]]
Linear mixed model fit by REML ['lmerMod']
Formula: disp ~ mpg + cyl + carb + (1 | am) + (1 | vs)
   Data: mtcars
REML criterion at convergence: 316.1521
Random effects:
 Groups   Name        Std.Dev.
 am       (Intercept)  0.00   
 vs       (Intercept)  0.00   
 Residual             49.83   
Number of obs: 32, groups:  am, 2; vs, 2
Fixed Effects:
(Intercept)          mpg          cyl         carb  
     112.57        -7.15        47.90       -12.30  
convergence code 0; 0 optimizer warnings; 1 lme4 warnings 

[[4]]
Linear mixed model fit by REML ['lmerMod']
Formula: qsec ~ mpg + cyl + drat + (1 | am) + (1 | vs)
   Data: mtcars
REML criterion at convergence: 92.9165
Random effects:
 Groups   Name        Std.Dev.
 am       (Intercept) 1.4979  
 vs       (Intercept) 0.6131  
 Residual             0.9008  
Number of obs: 32, groups:  am, 2; vs, 2
Fixed Effects:
(Intercept)          mpg          cyl         drat  
    24.5519       0.0288      -0.7956      -0.6974  

Upvotes: 4

Related Questions