简体   繁体   English

glmulti在for循环中不起作用

[英]glmulti not working in for-loop

I'm having trouble writing a for loop function where I ask gmulti to find the best model. 我在编写for循环函数时遇到麻烦,我要求gmulti找到最佳模型。 I have the following example data set: 我有以下示例数据集:

dput(Data) 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, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Female", "Male"
), class = "factor"), Captive_Wild = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Captive", "Wild"
), class = "factor"), C.SA.F = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), C.HA.F = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), W.MW.F = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L
), W.MW.DUR = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L), 
C.CHEW.F = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 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")

Code for my total loop is: 我的总循环代码是:

#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)   
 }

It's hanging up on the glmulti code where it gives me this error: 它挂在glmulti代码上,它给了我这个错误:

Error in model.frame.default(as.formula(paste(y, "~", paste(x, sep = "",  : 
variable lengths differ (found for 'Captive_Wild') 

And traceback looks like: 追溯看起来像:

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) 

I've also tried When I run the variables through by hand one-by-one the glmulti works just fine and when I remove Captive_Wild (which of course I don't want to do) it gives me the same error with Sex and ditto with Season. 我还尝试了一次手动操作变量时,glmulti可以正常工作,而当我删除Captive_Wild(当然,我不想这样做)时,它也出现了与Sex和ditto相同的错误与季节。 I've checked all variable lengths and they are the same. 我检查了所有可变长度,它们是相同的。

This implies to me that glmulti is having a problem with the for loop somewhere but I'm not sure where. 这对我来说意味着glmulti在某处的for循环有问题,但我不确定在哪里。 Can anyone suggest fixes? 谁能建议修复? This is my first attempt at for loops so any and all help would be much appreciated! 这是我第一次尝试for循环,因此我们将不胜感激!

After a little more fooling around I found two problems in the code: 1) lmer wrapper is old so need to call: 经过一番鬼混之后,我在代码中发现了两个问题:1)lmer包装器很旧,因此需要调用:

glmer.glmulti <- function (formula, data, family=binomial, random, ...) {
glmer(paste(deparse(formula), random), data = data,...)
}

and 2) using the alternate form of calling glmulti in the for-loop like so: 和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)   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM