0

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)

  • have you checked https://rdrr.io/cran/visreg/man/visreg-faq.html point 3 "How do I use visreg for a model with offset terms?" ? – Ben Bolker Jul 29 '23 at 16:51
  • Hi Ben, it works perfectly with glm() and glm.nb() objects, but when I have tried it with the glmmTMB function for zero-inflated negative binomial regression is not plotting the partial residuals in the same scale. In addition, the curve seems to be not fitting the data as expected. – Sergio Nolazco Jul 30 '23 at 04:57

1 Answers1

0

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