rpredictmumin

Error in predict(), R will not call predict.averaging() from MuMIn


Any help greatly appreciated, thanks in advance.

I have averaged three GLMM models using the model.avg() function within the MuMIn package. I would now like to predict from this averaged model. However, when I call predict() R seems to think I want predict.merMod() and hence throws a warning message because I have asked for standard errors, which are not provided by predict.merMod(). Instead I am wanting to call predict.averaging() which is the appropriate call for a model of class averaging and will provide standard errors for model predictions. If I try to force R to use predict.averaging() by specifying MuMIn::predict() or MuMIn::predict.averaging() I get an error saying that the function is not exported from MuMIn.

Any help to resolve this would be greatly appreciated? I want to predict from an averaged model, of class averaging, and want to obtain estimates as well as standard errors.

Reproducable example below.

# Load packages
> library(MuMIn); library(lme4)

# Create test data set
> test <- rbind(mtcars, mtcars)
> test <- rbind(mtcars, test)

# Create test models
> t1 <- glmer(am ~  hp + (1 | carb), data = test, family = binomial)
> t2 <- glmer(am ~  hp + vs + (1 | carb), data = test, family = binomial)

# Create model list and average models
> list <- list(t1, t2)
> p1 <- model.avg(list)

# Create new data set for predictions
> nd <- mtcars[1:2,]
> nd <- select(nd, hp, vs)

# Model predictions demonstrating errors and warnings
> predict(p1, backtransform = TRUE, newdata = nd, re.form = NA, se.fit = TRUE)
    Mazda RX4 Mazda RX4 Wag 
    0.8938837     0.8938837 
Warning messages:
1: In predict.merMod(object = new("glmerMod", resp = new("glmResp",  :
  unused arguments ignored
2: In predict.merMod(object = new("glmerMod", resp = new("glmResp",  :
  unused arguments ignored

> MuMIn::predict(p1, backtransform = TRUE, newdata = nd, re.form = NA, se.fit = TRUE)
Error: 'predict' is not an exported object from 'namespace:MuMIn'

> predict.averaging(p1, backtransform = TRUE, newdata = nd, re.form = NA, se.fit = TRUE)
Error in predict.averaging(p1, backtransform = TRUE, newdata = nd, re.form = NA,  : 
  could not find function "predict.averaging"

> MuMIn::predict.averaging(p1, backtransform = TRUE, newdata = nd, re.form = NA, se.fit = TRUE)
Error: 'predict.averaging' is not an exported object from 'namespace:MuMIn'

> MuMIn:::predict.averaging(p1, backtransform = TRUE, newdata = nd, re.form = NA, se.fit = TRUE)
    Mazda RX4 Mazda RX4 Wag 
    0.8938837     0.8938837 
Warning messages:
1: In predict.merMod(object = new("glmerMod", resp = new("glmResp",  :
  unused arguments ignored
2: In predict.merMod(object = new("glmerMod", resp = new("glmResp",  :
  unused arguments ignored


Solution

  • Use glmmTMB instead of lmer- the syntax is identical and it yields similar results while allowing for se.fit in predict, and is often more efficient.

    Internal predict replacement for "merMod", which calculated se.fit, has been removed as of MuMIn 1.43.9 since R complains about packages overwriting registered methods.