It is often useful to plot the result of regression to visualize the effect of predictors.
Now that we understand the predict
function for zero-inflated models, we can use it to get predictions for new values and plot them.
We can also use package emmeans
to prob interactions for the model and plot them.
Two key arguments for functions in package emmeans
are:
mode = c("response", "count", "zero", "prob0")
and lin.pred = c(FALSE, TRUE)
.
# Create new data for predictions
new.data <- data.frame(
hospital = mean(dat$hospital),
health = "poor",
chronic = seq(0, 8, 0.1),
gender = "female",
school = mean(dat$school),
insurance = "no"
)
# Predicted response
new.data$response <- predict(m.zeroinf.nb, newdata = new.data, type ="response")
# Predicted count only in count component
new.data$count <- predict(m.zeroinf.nb, newdata = new.data, type = "count")
# Predicted probability of excess zero
new.data$pi <- predict(m.zeroinf.nb, newdata = new.data, type = "zero")
# Linear predictor
new.data$linear.count <- log(new.data$count)
# Plot predicted response against chronic variable
ggplot(new.data, aes(x = chronic, y = response)) +
geom_line(color = "blue", size = 1)
# Plot predicted count against chronic variable
ggplot(new.data, aes(x = chronic, y = count)) +
geom_line(color = "lightblue", size = 1)
# Plot both predicted response and count together
new.data %>%
pivot_longer(cols = c(count, response),
names_to = "type", values_to = "visits") %>%
ggplot(aes(x = chronic, y = visits, color = type)) +
geom_line(size = 1)
# Plot linear predictor
ggplot(new.data, aes(x = chronic, y = linear.count)) +
geom_line(color = "black", size = 1.5) +
ylab("linear predictor count")
# Plot probability of excess zero
ggplot(new.data, aes(x = chronic, y = pi)) +
geom_line(color = "red", size = 1.5) +
ylab("probability of excess zero")
# Use emmeans
library(emmeans)
emmip(m.zeroinf.nb, ~ chronic,
at = list(
hospital = mean(dat$hospital),
health = "poor",
chronic = 0:8,
gender = "female",
school = mean(dat$school),
insurance = "no"
),
lin.pred = FALSE, mode ="count", CIs = TRUE) +
ylab("count")