我在编写for循环函数时遇到问题,我要求gmulti找到最佳模型。我有以下示例数据集:
dput(数据)
structure(list(Studbook.ID = structure(c(16L, 16L, 16L, 16L,
16L, 16L, 16L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 30L, 30L, 30L), .Label = c("230", "298",
"308", "329", "357", "358", "374", "382", "385", "394", "397",
"399", "404", "413", "414", "418", "432", "433", "434", "437",
"439", "444", "446", "455", "458", "460", "473", "475", "476",
"477", "478", "492", "495", "496", "499", "503"), class = "factor"),
Season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("Breeding", "Nonbreeding"), class = "factor"),
Year = c(1999L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L,
2000L, 2000L, 2000L, 2000L, 2000L, 2000L), Age.Class = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Adult",
"Sub-Adult"), class = "factor"), Sex = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L,
1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L)), .Names =c("Studbook.ID",
"Season", "Year", "Age.Class", "Sex", "Captive_Wild", "C.SA.F",
"C.HA.F", "W.MW.F", "W.MW.DUR", "C.CHEW.F"), row.names = c(NA,
40L), class = "data.frame")
我的总循环代码是:
#lmer wrapper for glmulti function
lmer.glmulti <- function (formula, data, family=binomial, random, ...) {
lmer(paste(deparse(formula), random), data = data,...)
}
#make a dependent variable list for loop
dep_list<-colnames(Bamboo)
dep_list<-dep_list[-c(1:6)]
outglm<-c()
outdesc<-c()
#start loop coding
for (depend in dep_list){
y <-Bamboo[,depend]
#gmluti loop
#glm full model (substitute behavioral variables in place of 'depend')
glmmod<-y~Captive_Wild+Sex+Age.Class+Season
glm.glmulti<-glmulti(glmmod, random="+(1|Studbook.ID)", data=Bamboo, fitfunc = lmer.glmulti, family=binomial, level=2)
#make and print table for final best model
htmlreg(glm.glmulti@objects[[1]], file=paste(depend, ".doc", sep=""), caption = depend, caption.above = TRUE)
}
它挂在了glmulti代码上,它给了我这个错误:
Error in model.frame.default(as.formula(paste(y, "~", paste(x, sep = "", :
variable lengths differ (found for 'Captive_Wild')
跟踪看起来像:
8 model.frame.default(as.formula(paste(y, "~", paste(x, sep = "",
collapse = "+"), sep = "")), data = data)
7 model.frame(as.formula(paste(y, "~", paste(x, sep = "", collapse = "+"),
sep = "")), data = data)
6 glmulti(y = "y", data = Bamboo, level = 2, fitfunction = lmer.glmulti,
random = "+(1|Studbook.ID)", xr = c("Sex", "Season"), exclude = 1)
5 glmulti(y = "y", data = Bamboo, level = 2, fitfunction = lmer.glmulti,
random = "+(1|Studbook.ID)", xr = c("Sex", "Season"), exclude = 1)
4 eval(expr, envir, enclos)
3 eval(call)
2 glmulti(y ~ Sex + Season, random = "+(1|Studbook.ID)", data = Bamboo,
fitfunc = lmer.glmulti, level = 2)
1 glmulti(y ~ Sex + Season, random = "+(1|Studbook.ID)", data = Bamboo,
fitfunc = lmer.glmulti, level = 2)
我也尝试过当我逐个手动运行变量时,glmulti工作得很好,当我删除Captive_Wild(我当然不想这样做)时,它给了我同样的错误性与季节同上。我检查了所有变量长度,它们是相同的。
这对我来说意味着glmulti在某处遇到for循环的问题,但我不确定在哪里。任何人都可以建议修复?这是我第一次尝试for循环,所以任何和所有的帮助将不胜感激!
答案 0 :(得分:0)
经过一番愚弄后,我在代码中发现了两个问题: 1)lmer包装器是旧的所以需要调用:
glmer.glmulti <- function (formula, data, family=binomial, random, ...) {
glmer(paste(deparse(formula), random), data = data,...)
}
和2)使用在for循环中调用glmulti的替代形式,如下所示:
for (depend in dep_list){
glm.glmulti = glmulti(depend, c("Captive_Wild", "Sex", "Age.Class", "Season"), random="+(1|Studbook.ID)", data=Bamboo, fitfunc=lmer.glmulti, family=binomial, level=2)
#make and print table for final best model
htmlreg(glm.glmulti@objects[[1]], file=paste(depend, ".doc", sep=""), caption = depend, caption.above = TRUE)
}