使用函数lmer()将随机效应合并到广义线性混合模型中的循环

时间:2016-08-25 13:42:01

标签: r loops linear-regression glm random-effects

以下是我的数据框的一小部分。实际数据帧具有每个变量的显式名称;不只是“DepVar1,DepVar2(2个响应变量)”或“IndVar(1-9)”(9个解释变量 - 1个分类变量和8个连续变量)。

我想通过将函数glm()更改为 中的 lmer() 来调整循环written by Bergan lme4包 生成一系列广义线性混合模型(GLMM),包含所有可能的解释变量组合(Indvar 1-9),并使用(1 | IndVarType)语法指定随机效应来解释方差在响应变量(DepVar1和DepVar2)中。

Example of glmm models: 

DepVar1 ~ Indvar (1-9) + (1|IndVarType)
DepVar2 ~ Indvar (1-9) + (1|IndVarType)

在运行循环以生成所有glmm模型后,我的目标是使用 AICcmodavg 包中的 aictab()功能,以最低的AICc值对最佳glmm模型进行排序显示相关统计数据:(1)Delta_AICc; (2)AICcWt; (3)Cum.Wt.

我一直在尝试修改Bergans代码以包含随机效果(1 | IndVarType),但到目前为止我一直没有成功。有什么建议怎么做?我做了一些搜索,只能找到包含glm()函数的循环的例子。非常感谢,如果有人有解决方案。

代码

library(lme4)

ind_vars <-  c("Indvar1",
               "Indvar2",
               "Indvar3", 
               "Indvar4",
               "Indvar4",
               "Indvar5",
               "Indvar6",
               "Indvar7",
               "Indvar8",
               "Indvar9",
               "IndvarType")

   dep_vars <- c("Depvar1", "DepVar2")

   # create all combinations of ind_vars

ind_vars_comb <- 
  unlist(sapply( seq_len(length(ind_vars)), 
              function(i) {
                apply( combn(ind_vars,i), 2, function(x) paste(x, collapse = "+"))
              }))

    # pair with dep_vars:
      var_comb <- expand.grid(dep_vars, ind_vars_comb ) 

    # formulas for all combinations
      formula_vec <- sprintf("%s ~ %s", var_comb$Var1, var_comb$Var2)

    # create models
      # create models
     glm_mixed <- lapply(formula_vec, function(f)   {
          fit1 <- lmer(f, (1|IndvarType), data = bats)
          fit1$coefficients <- coef(summary(fit1))
          return(fit1)
          })
          names(glm_mixed) <- formula_vec

          ##Error: No random effects terms specified in formula 

 # Model selection
 # Installed AICcmodavg package for AICc values into R 

AICc信息

 # R code from Mazerolle (2014)

   library(AICcmodavg)

   mydata.aov <- glm_mixed # list of models
   mydata.model.names <- formula_vec # list of model names

# generates AICc values # sort models into order of AIC value

   aictab(mydata.aov, mydata.model.names, second.ord = TRUE, sort = TRUE) 

数据结构

structure(list(Indvar1 = c(0, 5, 10, 19, 30, 33, 39, 44, 54, 
63, 68, 72, 81, 87, 93, 100, 105, 110, 119, 127, 134, 141, 149, 
155, 115, 120, 125, 0, 5, 9, 17, 22, 29, 35, 39, 44, 45, 50, 
55, 63), IndvarType = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L), .Label = c("CONTROL", "LED", "Metal Halide", "SOX"), class = "factor"), 
`IndvarCat ` = c(26.9, 25.16, 39, 29.81, 21.83, 20.22, 2.9, 
2.1, 0.85, 0.62, 0.39, 0.26, 24.7, 21.99, 20.46, 26.32, 0, 
0, 0.43, 0.02, 0.02, 0.03, 0.02, 0.03, 2.62, 0.43, 0.44, 
25.16, 39, 29.81, 21.83, 20.22, 20.88, 0.63, 0.56, 0.56, 
86.63, 87.97, 88.59, 0.31), Indvar2 = c(10.34, 12.56, 15.76, 
10.35, 11.15, 14.6, 15.05, 12.54, 15.29, 19.5, 17.12, 17.62, 
13.92, 12.7, 12.55, 17.86, 18.86, 18.23, 19.65, 19.59, 18.11, 
19.04, 16.92, 18.39, 18.97, 18.96, 17.72, 7.65, 8.61, 8.98, 
8.68, 12.25, 11.71, 16.19, 15.73, 16.02, 13.62, 14.89, 14.98, 
17.14), Indvar3 = c(7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 2, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 13, 13, 13, 13, 13, 8, 8), Indvar4 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Full Moon", 
"Waning Gibbous", "Waxing Crescent", "Waxing Gibbous"), class = "factor"), 
Indvar5 = c(32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 
32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 32.2, 
32.2, 32.9, 32.9, 32.9, 32.9, 32.9, 41.4, 41.4, 41.4, 41.4, 
41.4, 41.4, 41.4, 41.4, 41.4, 41.1, 41.1, 41.1, 41.1, 41.1, 
42.2, 42.2), Indvar6 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Indvar7 = c(18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 14, 14, 14, 14, 14, 14, 
14, 14, 13, 13, 13, 14.3, 14.3, 14.3, 14.3, 14.3, 14.3, 14.3, 
14.3, 14.3, 15.5, 15.5, 15.5, 15.5, 15.5, 14.6, 14.6), Indvar8 = c(51, 
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 69, 69, 69, 
69, 69, 69, 77, 77, 77, 77, 77, 62, 62, 62, 62, 62, 62, 62, 
62, 62, 57, 57, 57, 57, 57, 61, 61), Indvar9 = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Depvar1 = c(3, 
2, 5, 6, 15, 2, 10, 12, 17, 2, 0, 0, 15, 7, 17, 0, 1, 0, 
14, 10, 12, 7, 4, 1, 5, 4, 2, 9, 7, 7, 9, 5, 4, 3, 0, 0, 
12, 11, 9, 1), DepVar2 = c(0.444444444, 0, 0, 0.027777778, 
0, 0, 0.25, 0, 0.08650519, 0, 0, 0, 0.111111111, 0, 0.124567474, 
0, 0, 0, 0.25, 0.01, 0.111111111, 0.081632653, 0, 0, 0.04, 
0.25, 0.25, 0.790123457, 0.510204082, 2.040816327, 1.777777778, 
0, 2.25, 0.111111111, 0, 0, 0.027777778, 0.074380165, 0.012345679, 
0)), .Names = c("Indvar1", "IndvarType", "IndvarCat ", "Indvar2", 
"Indvar3", "Indvar4", "Indvar5", "Indvar6", "Indvar7", "Indvar8", 
"Indvar9", "Depvar1", "DepVar2"), row.names = c(NA, 40L), class =      "data.frame")

0 个答案:

没有答案