10.5 Practice
The practice problems for Chapter 10 ask you to fit binomial and Poisson regressions, check for overdispersion, and compare models with information criteria.
Easy problems
10E1. If a logistic regression coefficient is 0.7, what is the odds ratio? What is the probability when the linear predictor is 0.7?
10E2. If a Poisson regression coefficient is 0.3, what is the multiplicative effect on the expected count?
10E3. What is overdispersion? How do you detect it?
10E4. What is the difference between a zero-inflated Poisson and a hurdle model?
Medium problems
10M1. Fit a logistic regression with two predictors and an interaction. Interpret the results on the probability scale.
# 10M1: Logistic regression with interaction
set.seed(42)
n <- 300
x1 <- runif(n, 0, 10)
x2 <- runif(n, 0, 10)
# True relationship with interaction
logit*p <- -2 + 0.3 * x1 + 0.2 _ x2 + 0.05 _ x1 _ x2
p_true <- 1 / (1 + exp(-logit_p))
y <- rbinom(n, 1, p_true)
m <- glm(y ~ x1 * x2, family = binomial)
cat("Logistic regression with interaction:\n")
cat(" x1:", round(coef(m)[2], 3), "\n")
cat(" x2:", round(coef(m)[3], 3), "\n")
cat(" x1:x2:", round(coef(m)[4], 3), "\n")
# Predicted probabilities at different x2 values
cat("\nPredicted P(y=1) at x1=5, varying x2:\n")
for (x2*val in c(2, 5, 8)) {
logit <- coef(m)[1] + coef(m)[2] * 5 + coef(m)[3] _ x2_val + coef(m)[4] _ 5 _ x2_val
cat(sprintf(" x2 = %d: p = %.3f\n", x2_val, 1/(1+exp(-logit))))
}10M2. Fit a Poisson regression. Check for overdispersion. If present, fit a negative binomial model and compare.
# 10M2: Poisson vs negative binomial
set.seed(42)
n <- 100
x <- runif(n, 0, 10)
# Overdispersed counts
lambda <- exp(0.5 + 0.2 _ x)
y <- rpois(n, lambda _ rgamma(n, shape = 2, rate = 2))
# Poisson
m_pois <- glm(y ~ x, family = poisson)
# Check overdispersion
cat("Overdispersion check:\n")
cat(" Mean:", round(mean(y), 2), " Variance:", round(var(y), 2), "\n")
cat(" Deviance/df:", round(deviance(m_pois) / df.residual(m_pois), 2), "\n")
# Negative binomial
library(MASS)
m_nb <- glm.nb(y ~ x)
cat("\nModel comparison:\n")
cat(" Poisson AIC:", round(AIC(m_pois), 1), "\n")
cat(" NegBin AIC: ", round(AIC(m_nb), 1), "\n")Hard problems
10H1. Simulate zero-inflated data. Fit a standard Poisson and observe the poor fit. Then fit a hurdle model and compare.
# 10H1: Zero-inflated data and hurdle model
set.seed(42)
n <- 200
x <- runif(n, 0, 10)
# Zero-inflated: 40% structural zeros
is_zero <- rbinom(n, 1, 0.4)
lambda <- exp(0.3 + 0.15 * x)
y_pois <- rpois(n, lambda)
y <- ifelse(is_zero, 0, y_pois)
cat("Zero-inflated data:\n")
cat(" Zeros:", sum(y == 0), "/", n, "\n")
cat(" Proportion zeros:", round(mean(y == 0), 3), "\n")
# Standard Poisson (wrong)
m_pois <- glm(y ~ x, family = poisson)
# Hurdle model (two-part)
m_hurdle <- glm(I(y > 0) ~ x, family = binomial)
m_count <- glm(y[y > 0] ~ x[y > 0], family = poisson)
cat("\nStandard Poisson:\n")
cat(" Intercept:", round(coef(m_pois)[1], 3), " Slope:", round(coef(m_pois)[2], 3), "\n")
cat("\nHurdle model:\n")
cat(" Part 1 (y > 0): intercept =", round(coef(m_hurdle)[1], 3),
" slope =", round(coef(m_hurdle)[2], 3), "\n")
cat(" Part 2 (count): intercept =", round(coef(m_count)[1], 3),
" slope =", round(coef(m_count)[2], 3), "\n")
cat("\nThe hurdle model separates the zero process from the count process.\n")10H2. Fit a binomial regression with aggregated data (multiple trials per observation). Compare with the individual-level model. Do they give the same results?
10H3. Use posterior predictive checks to compare a Poisson and negative binomial model. Which reproduces the data better?