Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

r - glmulti and liner mixed models

I have a problem with the glmulti package and linear mixed models. When I try to estimate the model-averaged coefficients with the coff.glmulti function I get this error:

Error in data.frame(..., check.names = FALSE) :arguments imply differing number of rows: 1, 0

I did a little bit of debugging and I found that the problem starts in the highlighted line of the coeff.glmulti function:

         if (length(object@adi) >= 1) 
             for (j in 1:length(object@adi)) {
               cak[[length(names(cak)) + 1]] = object@adi[[j]]

               names(cak)[length(names(cak))] = names(object@adi)[j]
             }
         modf = eval(cak)

         coffee = c(coffee, list(modf))
     }
 }

 if (length(coffee) == 1) {
     warning("Only one candidate: standard conditional inference was performed.")

     **return(coef(coffee[[1]]))**
 }

After, when it tries to apply getfit on the coffee object it fails. I think that the error is due to a different structure of the lmer.fir object respect to lm or other type of models object.

I'm pasting a minimum repeatable example to facilitate who wants to help me:

#Add the required package
library(lme4)
library(glmulti)

# A random vector of count data
vy1<-round(runif(100, min=1,max=20)*round(runif(100,min=1,max=20)))

# Predictors
va = runif(100,min=1,max=100)
vb = runif(100,min=,max=100)
random_effect <- as.factor(rep(c(1,2,3,4),each=25))
pippo<-as.data.frame(cbind(vy1,va,vb,random_effect))
form_glmulti = as.formula(paste("vy1~va*vb")) 

# The wrapper function for linear mixed-models
lmer.glmulti<-function(formula,data,random="",...){
  lmer(paste(deparse(formula),random),data=data,REML=F,...)
}
# The wrapper function for linear models
lm.glmulti<-function(formula,data,...){
  lm(paste(deparse(formula)),data=data,...)
}

# Multi selection for lmer
glmulti_lmm<-glmulti(form_glmulti,random="+(1|random_effect)",data=pippo,method="h", 
                     fitfunc=lmer.glmulti, intercept=TRUE,marginality=FALSE,level=2)
# Model selection for lm
glmulti_lm<-glmulti(form_glmulti,data=pippo,method="h",fitfunc=lm.glmulti,intercept=TRUE, 
                        marginality=FALSE, level=2)

# Coeffs estimation lmer #Here the error
coef.glmulti(glmulti_lmm,select="all",varweighting="Johnson",icmethod="Burnham")
#Coeffs estimation lm #With lm everything is ok
coef(glmulti_lm,varweighting="Johnson",icmethod="Burnham",select="all")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using one of the latest versions of lme4, the getfit() function I recommended is no longer adapted. Indeed, the lme4 package maintainers have made quite a lot of changes in their package: the class of objects is now "merMod", where it was "mer", and a few other things.

Then the getfit function must be slightly adjusted in order to interface glmulti with the new lme4 structure. Here a a getfit definition that works with the latest builds of lme4 for Ubuntu 12.04, as of yesterday:

setMethod('getfit', 'merMod', function(object, ...) {
summ=summary(object)$coef
summ1=summ[,1:2]
if (length(dimnames(summ)[[1]])==1) {
    summ1=matrix(summ1, nr=1, dimnames=list(c((Intercept)"),c("Estimate","Std.    Error")))
}
cbind(summ1, df=rep(10000,length(fixef(object))))
})

This should fix the issue. [see also my website http://vcalcagnoresearch.wordpress.com/package-glmulti/] Regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...