Syntax for glmer function for use with glmulti?

792 views Asked by At

Using glmer, I can run a logistic regression mixed model just fine. But when I try to do the same using glmulti, I get errors (described below). I think the problem is with the function I am specifying for use in glmulti. I want a function that specifies a logistic regression model for data containing continuous fixed covariates and categorical random effects, using a logit link. The response variable is a binary 0/1.

Sample data:

library(lme4)
library(rJava)
library(glmulti)

set.seed(666)
x1 = rnorm(1000)  # some continuous variables 
x2 = rnorm(1000)
x3 = rnorm(1000)
r1 = rep(c("red", "blue"), times = 500)  #categorical random effects
r2 = rep(c("big", "small"), times = 500)
z = 1 + 2*x1 + 3*x2 +2*x3       
pr = 1/(1+exp(-z))         
y = rbinom(1000,1,pr)      # bernoulli response variable
df = data.frame(y=y,x1=x1,x2=x2, x3=x3, r1=r1, r2=r2)

A single glmer logistic regression works just fine:

model1<-glmer(y~x1+x2+x3+(1|r1)+(1|r2),data=df,family="binomial")

But errors occur when I try to use the same model structure through glmulti:

# create a function - I think this is where my problem is 

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

# run glmulti models
glmulti.logregmixed <-
  glmulti(formula(glmer(y~x1+x2+x3+(1|r1)+(1|r2), data=df),   fixed.only=TRUE), #error w/o fixed.only=TRUE
      data=df,
      level = 2,              
      method = "g",            
      crit = "aicc",           
      confsetsize = 128,       
      plotty = F, report = F,  
      fitfunc = glmer.glmulti,
      family = binomial(link ="logit"),
      random="+(1|r1)","+(1|r2)",  # possibly this line is incorrect?
      intercept=TRUE)

#Errors returned:
singular fit
Error in glmulti(formula(glmer(y ~ x1 + x2 + x3 + (1 | r1) + (1 | r2),  : 
  Improper call of glmulti.
In addition: Warning message:
In glmer(y ~ x1 + x2 + x3 + (1 | r1) + (1 | r2), data = df) :
  calling glmer() with family=gaussian (identity link) as a shortcut     to lmer() is deprecated; please call lmer() directly

I've tried various changes to the function, and within the formula and fitfunc portion of the glmulti code. I've tried substituting lmer for glmer and I guess I don't understand the error. I'm also afraid that calling lmer may change the model structure, as during one of my attempts the summary() of the model stated "Linear mixed model fit by REML ['lmerMod']." I need the glmulti models to be the same as what I'm obtaining with model1 using glmer (ie summary(model1) gives "Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']"

Many similar questions remain unanswered. Thanks in advance!

Credit:

sample data set created with help from here: https://stats.stackexchange.com/questions/46523/how-to-simulate-artificial-data-for-logistic-regression

glmulti code adapted from here: Model selection using glmulti

0

There are 0 answers