【问题标题】:Cannot run lmer from within a function无法从函数内运行 lmer
【发布时间】:2014-03-20 20:20:14
【问题描述】:

我在尝试将lmer 嵌入函数时遇到了问题。这是一个使用来自lexdec 的数据的可重现示例。如果我直接在数据框上运行lmer,是没有问题的。例如,假设我想查看词汇决策任务中的阅读时间是否作为 Trial 的函数而不同。有两种类型的词刺激,“动物”(例如“狗”)和“植物”(例如“樱桃”)。我可以计算动物词的混合效果模型:

library(languageR)       #load lexdec data
library(lme4)            #load lmer()
s <- summary(lmer(RT ~ Trial + (1|Subject) + (1|Word), data = lexdec[lexdec$Class== "animal", ]))
s                        #this works well

但是,如果我将 lmer 模型嵌入到一个函数中(比如不要为每个级别的类键入相同的命令),我会收到一条错误消息。 你知道为什么吗?任何建议都将不胜感激!

#lmer() is now embedded in a function
compute.lmer <- function(df,class) {
  m <- lmer(RT ~ Trial + (1|Subject) + (1|Word),data = df[df$Class== class, ])
  m <- summary(m)
  return(m)
}

#Now I can use this function to iterate over the 2 levels of the **Class** factor
for (c in levels(lexdec$Class)){
 s <- compute.lmer(lexdec,c)
 print(c)
 print(s)
}

#But this gives an error message
Error in `colnames<-`(`*tmp*`, value = c("Estimate", "Std. Error", "df",  : 
  length of 'dimnames' [2] not equal to array extent 

【问题讨论】:

  • 这是一个合理的问题,但我认为标题有点误导——我认为它没有将结果保存到给你带来麻烦的列表中,它是从函数中运行 lmer数据被传递到的地方...
  • 感谢本。我试图重新表述问题以使其更准确

标签: r lme4


【解决方案1】:

我不知道问题出在哪里,您的代码对我来说运行良好。 (你的包是最新的吗?你运行的是什么 R 版本?你清理过你的工作空间并从头开始尝试你的代码吗?)

也就是说,这是 plyr::dlply 的一个很好的用例。我会这样做:

library(languageR) 
library(lme4)
library(plyr)

stats <- dlply(lexdec,
      .variables = c("Class"),
      .fun=function(x) return(summary(lmer(RT ~ Trial + (1 | Subject) +
                                                (1 | Word), data = x))))

names(stats) <- levels(lexdec$Class)

然后产生

> stats[["plant"]]
Linear mixed model fit by REML ['lmerMod']
Formula: RT ~ Trial + (1 | Subject) + (1 | Word)
   Data: x

REML criterion at convergence: -389.5

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.2647 -0.6082 -0.1155  0.4502  6.0593 

Random effects:
 Groups   Name        Variance Std.Dev.
 Word     (Intercept) 0.003718 0.06097 
 Subject  (Intercept) 0.023293 0.15262 
 Residual             0.028697 0.16940 
Number of obs: 735, groups: Word, 35; Subject, 21

Fixed effects:
              Estimate Std. Error t value
(Intercept)  6.3999245  0.0382700  167.23
Trial       -0.0001702  0.0001357   -1.25

Correlation of Fixed Effects:
      (Intr)
Trial -0.379

当我运行您的代码时(复制和粘贴未修改),我得到类似的输出。除了Data: 行之外,它是相同的。

stats = list()  

compute.lmer <- function(df,class) {
    m <- lmer(RT ~ Trial + (1|Subject) + (1|Word),data = df[df$Class== class, ])
    m <- summary(m)
    return(m)
}

for (c in levels(lexdec$Class)){
    s <- compute.lmer(lexdec,c)
    stats[[c]] <- s
}

> stats[["plant"]]
Linear mixed model fit by REML ['lmerMod']
Formula: RT ~ Trial + (1 | Subject) + (1 | Word)
   Data: df[df$Class == class, ]

REML criterion at convergence: -389.5

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.2647 -0.6082 -0.1155  0.4502  6.0593 

Random effects:
 Groups   Name        Variance Std.Dev.
 Word     (Intercept) 0.003718 0.06097 
 Subject  (Intercept) 0.023293 0.15262 
 Residual             0.028697 0.16940 
Number of obs: 735, groups: Word, 35; Subject, 21

Fixed effects:
              Estimate Std. Error t value
(Intercept)  6.3999245  0.0382700  167.23
Trial       -0.0001702  0.0001357   -1.25

Correlation of Fixed Effects:
      (Intr)
Trial -0.379

【讨论】:

  • 不错。不知道dlply
  • 同意,对我有用。对于旧版本/以前的版本可能会失败,这并不让我感到惊讶;让 lmer 在正确的环境中查找数据,无论它最初是从什么环境中调用的,这比您想象的要棘手......
  • @shujaa:谢谢!你是对的:当我更新 lme4 时问题就消失了。我一直在避免更新它,因为我读到 lme4 1.0 有时会产生比以前版本link 更差的模型。 dlply的方式也不错!
  • @rawr 这就是plyr 的伟大之处,(a,d,m,l,r)(a,d,l,_)ply(input)(output)ply 的每一种组合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 2021-12-06
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2018-12-04
相关资源
最近更新 更多