简体   繁体   中英

How to fit confidence intervals using predict function for glmmTMB

I am running a mixed model using the package glmmTMB and using the predict function to calculate predicted means using the following code:

run model

model_1 <- glmmTMB(Step.rate ~ Treatment*Week + 
    (1|Treatment.Group/Lamb.ID) +  (1|Plot),
     data = data.df, family = nbinom1) 

create new dataframe

new.dat <- data.frame(Treatment = data.df$Treatment,
                      Week = data.df$Week, Plot = data.df$Plot, 
                      Treatment.Group = data.df$Treatment.Group,
                      Lamb.ID = data.df$Lamb.ID) 

predict mean values

new.dat$prediction <- predict(model_1, new.data = new.dat, 
       type = "response", re.form = NA) 

This code works fine, but when I add in intervals = "confidence" to calculate confidence intervals it doesn't seem to work. R ignores the last part of the code and only the predicted means are calculated.

new.dat$prediction <- predict(model_1, new.data = new.dat, 
     type = "response", re.form = NA, intervals = "confidence")

Why is intervals = "confidence" not working? Could this be a problem associated with the package glmmTMB?

You can use the argument se.fit = TRUE to get the standard errors of the predicted values and then use these to calculate the confidence intervals.

https://www.rdocumentation.org/packages/glmmTMB/versions/1.0.2.1/topics/predict.glmmTMB

I think the other answer gives you a work-around for getting CI for glmmTMB objects using the se.fit argument. But the issue of having specific versions of functions for different object types (defined by the class of the object) is something that caused me some grief in the past so it may be worth expanding on here.

Without getting in too much detail, functions in R that are common across many object types have generic versions. If you got to the documentation for ?predict , for example, you will see the help page for the generic version of the function. There you will see a few general statements about how the function normally works but little to no explanation of specific arguments since the arguments available depend on the type of object you are working with. The description from the help page for generic predict() :

predict is a generic function for predictions from the results of various model fitting functions. The function invokes particular methods which depend on the class of the first argument.

Specific model fitting functions can have specific versions of predict() that work with the resulting model object. For example, there is a specific predict() for models fit with lm() . Objects returned from lm() are of class lm . You can see the documentation for the version of the function for lm objects at ?predict.lm . It is this function that contains an intervals argument for calculating confidence and prediction intervals. While many of us start with lm objects and so learn intervals , it turns out many (most?) other predict() functions do not have this option.

The key for getting to the help page for the specific predict() function you are using is to know the class of the object returned by the model fitting function you are using. For example, models fit with glmmTMB() are of class glmmTMB so you can go to ?predict.glmmTMB . Models fit with lme4::lmer() are of class merMod so you can go to ?predict.merMod. If you don't know the class a model-fitting function returns, it looks like you can often find the info in the documentation under the Value section. This is true for lm() and lmer() , at least.

Finally, if you need to know if a certain class of object has a specific version of a generic function associated with it, you can look at the methods available for that class with the methods() function. Example for lm:

methods(class = "lm")
 [1] add1           alias          anova          case.names     coerce        
 [6] confint        cooks.distance deviance       dfbeta         dfbetas       
[11] drop1          dummy.coef     effects        extractAIC     family        
[16] formula        hatvalues      influence      initialize     kappa         
[21] labels         logLik         model.frame    model.matrix   nobs          
[26] plot           predict        print          proj           qqnorm        
[31] qr             residuals      rstandard      rstudent       show          
[36] simulate       slotsFromS3    summary        variable.names vcov      

There are some packages that do the work for you, like emmeans or ggeffects , or the effects package (and likely some more packages):

library(ggeffects)
library(glmmTMB)
library(emmeans)
data("Salamanders")
m <- glmmTMB(count ~ spp * mined + sample + (1 | site), family = nbinom1, data = Salamanders)

emmeans(m, c("spp", "mined"), type = "response")
#>  spp   mined response     SE  df lower.CL upper.CL
#>  GP    yes     0.0368 0.0373 627  0.00504    0.269
#>  PR    yes     0.1099 0.0661 627  0.03368    0.358
#>  DM    yes     0.3842 0.1397 627  0.18808    0.785
#>  EC-A  yes     0.1099 0.0660 627  0.03377    0.357
#>  EC-L  yes     0.3238 0.1222 627  0.15437    0.679
#>  DES-L yes     0.4910 0.1641 627  0.25468    0.947
#>  DF    yes     0.5561 0.1764 627  0.29822    1.037
#>  GP    no      2.2686 0.4577 627  1.52646    3.372
#>  PR    no      0.4582 0.1515 627  0.23940    0.877
#>  DM    no      2.4201 0.4835 627  1.63472    3.583
#>  EC-A  no      0.8931 0.2373 627  0.53005    1.505
#>  EC-L  no      3.2017 0.6084 627  2.20451    4.650
#>  DES-L no      3.4921 0.6517 627  2.42061    5.038
#>  DF    no      1.8495 0.3948 627  1.21623    2.813
#> 
#> Confidence level used: 0.95 
#> Intervals are back-transformed from the log scale
ggpredict(m, c("spp", "mined"))
#> 
#> # Predicted counts of count
#> # x = spp
#> 
#> # mined = yes
#> 
#> x    | Predicted |   SE |       95% CI
#> --------------------------------------
#> GP   |      0.04 | 1.01 | [0.01, 0.27]
#> PR   |      0.11 | 0.60 | [0.03, 0.36]
#> DM   |      0.38 | 0.36 | [0.19, 0.78]
#> EC-A |      0.11 | 0.60 | [0.03, 0.36]
#> EC-L |      0.32 | 0.38 | [0.15, 0.68]
#> DF   |      0.56 | 0.32 | [0.30, 1.04]
#> 
#> # mined = no
#> 
#> x    | Predicted |   SE |       95% CI
#> --------------------------------------
#> GP   |      2.27 | 0.20 | [1.53, 3.37]
#> PR   |      0.46 | 0.33 | [0.24, 0.88]
#> DM   |      2.42 | 0.20 | [1.64, 3.58]
#> EC-A |      0.89 | 0.27 | [0.53, 1.50]
#> EC-L |      3.20 | 0.19 | [2.21, 4.65]
#> DF   |      1.85 | 0.21 | [1.22, 2.81]
#> 
#> Adjusted for:
#> * sample = 2.50
#> *   site = NA (population-level)
#> Standard errors are on the link-scale (untransformed).

Created on 2020-09-14 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM