1

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

Pat Taggart
  • 321
  • 1
  • 9
  • Try `MuMIn:::predict.averaging()` – jay.sf May 12 '20 at 07:14
  • Thanks for the quick reply. I get the same error message: ```In predict.merMod(object = new("glmerMod", resp = new("glmResp", :unused arguments ignored``` – Pat Taggart May 12 '20 at 07:32
  • This looks like the _warning_ from above though rather than the _error_. Anyway, we need you to provide commented, minimal, self-contained, reproducible code, consider: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – jay.sf May 12 '20 at 07:39
  • Thanks again. I have now edited to include a reproducible example. – Pat Taggart May 13 '20 at 02:56
  • Well done! However, there simply seems to be no option for computing standard errors, see my answer below. – jay.sf May 13 '20 at 05:51

2 Answers2

1

It's the se.fit=TRUE option that's causing the warnings. There appears to be no option for computing standard errors for glmerMod. MuMIn:::predict.averaging has an option se.fit=, but it internally applies lme4:::predict.merMod which has not (that's the reason why we receive the warnings), and which help page states:

?lme4:::predict.merMod There is no option for computing standard errors of predictions because it is difficult to define an efficient method that incorporates uncertainty in the variance parameters; we recommend bootMer for this task.

In other words, you could try to bootstrap prediction errors using lme4::bootMer. The authors of package merTools discussed alternatives to calculate prediction intervals (their function merTools::predictInterval won't work for averaged models like yours, though).

Probably it isn't that hard to write a bootstrap method from scratch, see e.g. this post on Cross Validated. Unfortunately I'm too unfamiliar with that model to help you further, and things are getting too statistical for Stack Overflow, but you could ask a related question on Cross Validated on how to calculate/bootstrap prediction standard errors for averaged generalized linear mixed-effects models.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    Thanks jay.sf, I really appreciate this. I also suspected that it was the ```se.fit``` component that was driving the warnings but was confused as ```MuMIn:::predict.averaging``` suggests that it provides a CI. I suspect something may have changed recently in the underlying code. This relates to a script that I have opened after not touching for about two months and it worked last time I ran it, weird. I only opened script up again to edit a figure I had previously created, which I now can't re-create. – Pat Taggart May 13 '20 at 06:36
1

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.

Kamil Bartoń
  • 1,482
  • 9
  • 10