我在尝试嵌入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