Variable lengths differ in R (linear modelling with lme4)

对着背影说爱祢 提交于 2019-12-07 16:03:29

@Roland's diagnosis (lmer is looking for a variable called i, not a variable whose name is i: obligatory Lewis Carroll reference) is correct, I think. The most immediate way to handle this would be with reformulate(), something like:

for (i in names(genefile)){
    form <- reformulate(c("Treat1*Treat2","(1|Batch)"),response=i)
    lmer_results <- lmer(form, data=mydata)
    lmer_summary <- summary(lmer_results)
    write(lmer_summary,file="results_file",
           append=TRUE, sep="\t", quote=FALSE)
}

On second thought, you should be able to speed up your computations significantly using the built-in refit() method, which refits a model for a new response variable: suppose for simplicity that the first gene is called geneAAA:

wfun <- function(x)  write(summary(x), 
       file="results_file", append=TRUE, sep="\t",quote=FALSE)
mod0 <- lmer(geneAAA ~ Treat1*Treat2 + (1|Batch), data=mydata)
wfun(mod0)
for (i in names(genefile)[-1]) {
    mod1 <- refit(mod0,mydata[[i]])
    wfun(mod1)
}

(By the way, I'm not sure your write() command does anything sensible ...)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!