简体   繁体   English

lapply 用于回归模型的多个列表和函数

[英]lapply with multiple lists and functions for regression models

I'd like to run four multilevel models (using lmer) simultaneously using lapply.我想使用 lapply 同时运行四个多级模型(使用 lmer)。

A simple example using lm() with one dependent variable and a list of independent variables would be:使用带有一个因变量和一系列自变量的 lm() 的简单示例是:

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?如何扩展它以运行四个 lmer() 模型,每个模型都有不同的因变量和不同的自变量列表? 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?有任何想法吗?

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 .我们可以有一个因变量(或vector )和自变量的list ,并将其传递给Map以创建formula并应用lmer The unit element of a list would be the vector here for independent variables and the single element for dependent variable. list的单位元素将是这里的自变量vector和因变量的单个元素。

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM