How to make a plot with partial residuals using the 'visreg' function when my model is a 'glmmTMB' object with offsets?

308 views Asked by At

I am having some trouble trying to make a conditional plot using the 'visreg' function. My model is a zero-inflated negative binomial regression built using the 'glmmTMB' function and it includes an offset.

Why the partial residuals are so far away from the prediction curve? My guess is that something is happening with the scaling of the offset.

y <- c(18, 0, 2, 0,  0,  0,  2,  0,  0,  1,  7,  0,  0,  0,  0,  0,  0,  0,  0)
x1 <- c(501, 1597, 1156, 1134, 1924,  507, 1022,  0,  92, 1729, 85, 963, 544, 1315, 2250, 1366,  458,  385,  930)
x2 <- c(0,  92,  959, 1146,  900,  0,  276, 210,  980, 8, 0, 473, 0, 255, 1194, 542, 983, 331,  923)
offset_1 <- c(59, 34, 33, 35, 60, 58, 59, 33, 34, 61, 58, 58, 55, 26, 26, 18, 26, 26, 26)
data_1 <- data.frame(y,x1,x2,offset_1)

m1 <- glmmTMB(y ~ -1 + x1 + x2 + offset(log(offset_1)), data=data_1, 
                           family = nbinom2, zi = ~1)
summary(m1)

visreg(m1, "x1", scale="response", cond=list(offset_1=1), partial=TRUE,
       rug=2,line=list(lwd=0.5, col="black"), points=list(cex=1.4, lwd=0.1, col="black", pch=21))

visreg(m1, "x1", scale="response", cond=list(offset_1=1), partial=FALSE,
       rug=2,line=list(lwd=0.5, col="black"), points=list(cex=1.4, lwd=0.1, col="black", pch=21))

Here are the conditional plots adding the partial residuals (partials = TRUE), and without them (partials = FALSE).

This is the visreg plot including the partial residuals

This is the plot without adding the partial residuals (using the arg: partials=F)

1

There are 1 answers

0
Sergio Nolazco On

Thanks to the developer of the 'visreg' package, Patrick Breheny, now visreg() produces plots with the residuals on the linear predictor scale for glmmTMB model objects.

remotes::install_github("pbreheny/visreg")
library("visreg")
packageVersion("visreg")
y <- c(18, 0, 2, 0,  0,  0,  2,  0,  0,  1,  7,  0,  0,  0,  0,  0,  0,  0,  0)
x1 <- c(501, 1597, 1156, 1134, 1924,  507, 1022,  0,  92, 1729, 85, 963, 544, 1315, 2250, 1366,  458,  385,  930)
x2 <- c(0,  92,  959, 1146,  900,  0,  276, 210,  980, 8, 0, 473, 0, 255, 1194, 542, 983, 331,  923)
offset_1 <- c(59, 34, 33, 35, 60, 58, 59, 33, 34, 61, 58, 58, 55, 26, 26, 18, 26, 26, 26)
data_1 <- data.frame(y,x1,x2,offset_1)

m1 <- glmmTMB(y ~ -1 + x1 + x2 + offset(log(offset_1)), data=data_1, 
                           family = nbinom2, zi = ~1)
summary(m1)

visreg(m1, "x1", scale="response", cond=list(offset_1=1), partial=TRUE,
       rug=2,line=list(lwd=0.5, col="black"), points=list(cex=1.4, lwd=0.1, col="black", pch=21))

enter image description here