Marginal effects for negative binomial mixed effects models (glmer.nb and glmmTMB) #rstats

Here’s a small preview of forthcoming features in the ggeffects-package, which are already available in the GitHub-version: For marginal effects from models fitted with glmmTMB() or glmer() resp. glmer.nb(), confidence intervals are now also computed.

If you want to test these features, simply install the package from GitHub:

library(devtools)
devtools::install_github("strengejacke/ggeffects")

Here are three examples:

library(glmmTMB)
library(lme4)
library(ggeffects)
data(Owls)

m1 <- glmmTMB(SiblingNegotiation ~ SexParent + ArrivalTime + (1 | Nest), data = Owls, family = nbinom1)
m2 <- glmmTMB(SiblingNegotiation ~ SexParent + ArrivalTime + (1 | Nest), data = Owls, family = nbinom2)
m3 <- glmer.nb(SiblingNegotiation ~ SexParent + ArrivalTime + (1 | Nest), data = Owls)
m4 <-
  glmmTMB(
    SiblingNegotiation ~ FoodTreatment + ArrivalTime + SexParent + (1 | Nest),
    data = Owls,
    ziformula =  ~ 1,
    family = list(family = "truncated_poisson", link = "log")
  )
pr1 <- ggpredict(m1, c("ArrivalTime", "SexParent"))
plot(pr1)

pr2 <- ggpredict(m2, c("ArrivalTime", "SexParent"))
plot(pr2)

pr3 <- ggpredict(m3, c("ArrivalTime", "SexParent"))
plot(pr3)

pr4 <- ggpredict(
  m4, 
  c("FoodTreatment", "ArrivalTime [21,24,30]", "SexParent")
)
plot(pr4)

The code to calculate confidence intervals is based on the FAQ provided from Ben Bolker. Here is another example, that reproduces this plot (note, since age is numeric, ggpredict() produces a straight line, and not points with error bars).

library(nlme)
data(Orthodont)
m5 <- lmer(distance ~ age * Sex + (age|Subject), data = Orthodont)
pr5 <- ggpredict(m5, c("age", "Sex"))
plot(pr5)

Advertisements