r - glmulti not working in for-loop -


i'm having trouble writing loop function ask gmulti find best model. have following example data set:

dput(data)

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 total loop is:

#lmer wrapper glmulti function  lmer.glmulti <- function (formula, data, family=binomial, random, ...) {  lmer(paste(deparse(formula), random), data = data,...)  }   #make dependent variable list loop  dep_list<-colnames(bamboo)  dep_list<-dep_list[-c(1:6)]   outglm<-c()  outdesc<-c()   #start loop coding  (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 , print table final best model    htmlreg(glm.glmulti@objects[[1]], file=paste(depend, ".doc", sep=""), caption = depend, caption.above = true)     } 

it's hanging on glmulti code gives me error:

error in model.frame.default(as.formula(paste(y, "~", paste(x, sep = "",  :  variable lengths differ (found '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 tried when run variables through hand one-by-one glmulti works fine , when remove captive_wild (which of course don't want do) gives me same error sex , ditto season. i've checked variable lengths , same.

this implies me glmulti having problem loop somewhere i'm not sure where. can suggest fixes? first attempt @ loops , appreciated!

after little more fooling around found 2 problems in code: 1) lmer wrapper old need call:

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

and 2) using alternate form of calling glmulti in for-loop so:

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 , print table final best model htmlreg(glm.glmulti@objects[[1]], file=paste(depend, ".doc", sep=""), caption = depend, caption.above = true)    } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -