Search code examples
rpredictglmmtmb

How to calculate predicted means for specific fixed effects from model output using glmmTMB


I am running a glmm using glmmTMB and using predict() to calculate predicted means. I want to know if it is possible to calculate predicted means based on specific fixed effects from the model.

run the model

model_1 <- glmmTMB(step.rate ~ Treatment*Week + Logger.ID + (1|Animal.ID), 
           data = data.df, family = nbinom1)

I am currently using the following code to calculate predicted means for all fixed effects:

new data

new.dat <- data.frame(Treatment = data.df$Treatment, Week = data.df$Week, Logger.ID = 
           data.df$Logger.ID, Animal.ID = data.df$Animal.ID) 

predicted means

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

I don't want Logger.ID to be included when calculating predicted means and want to know if it possible to ignore Logger.ID and how to do this.


Solution

  • You cannot "remove" a predictor from the predicted means (when you use predict()), because this would return just NA values. Thus, I would recommend the same as Allen and use a sensible / meaningful value at which you hold Logger.ID constant. Here's an example from the ggeffects package:

    library(ggeffects)
    library(glmmTMB)
    data("Salamanders")
    m <- glmmTMB(count ~ spp * mined + sample + (1 | site), family = nbinom1, data = Salamanders)
    
    # hold "sample" constant at its mean value
    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).
    
    # predicted means when sample is set to "0"
    ggpredict(m, c("spp", "mined"), condition = list(sample = 0))
    #> 
    #> # Predicted counts of count
    #> # x = spp
    #> 
    #> # mined = yes
    #> 
    #> x    | Predicted |   SE |       95% CI
    #> --------------------------------------
    #> GP   |      0.04 | 1.02 | [0.00, 0.27]
    #> PR   |      0.11 | 0.62 | [0.03, 0.36]
    #> DM   |      0.38 | 0.38 | [0.18, 0.80]
    #> EC-A |      0.11 | 0.61 | [0.03, 0.36]
    #> EC-L |      0.32 | 0.40 | [0.15, 0.69]
    #> DF   |      0.54 | 0.34 | [0.28, 1.06]
    #> 
    #> # mined = no
    #> 
    #> x    | Predicted |   SE |       95% CI
    #> --------------------------------------
    #> GP   |      2.22 | 0.24 | [1.40, 3.52]
    #> PR   |      0.45 | 0.36 | [0.22, 0.90]
    #> DM   |      2.37 | 0.24 | [1.49, 3.78]
    #> EC-A |      0.87 | 0.30 | [0.49, 1.58]
    #> EC-L |      3.14 | 0.22 | [2.04, 4.81]
    #> DF   |      1.81 | 0.25 | [1.11, 2.95]
    #> 
    #> Adjusted for:
    #> * site = NA (population-level)
    #> Standard errors are on the link-scale (untransformed).
    

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