Poisson Regression Exercises in R: 12 Practice Problems

Twelve practice problems on Poisson regression in R: fit, interpret, offset, rate models, overdispersion checks.

RInteractive R
library(dplyr)

  

Exercise 1: Fit Poisson GLM

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, lambda = exp(0.5 + 0.05 * 1:50))) glm(count ~ x, data = df, family = poisson)

  

Exercise 2: Coefficient interpretation

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, lambda = exp(0.5 + 0.05*1:50))) fit <- glm(count ~ x, data = df, family = poisson) exp(coef(fit)["x"]) # multiplicative effect per unit

  

Exercise 3: Predict counts

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, lambda = exp(0.5 + 0.05*1:50))) fit <- glm(count ~ x, data = df, family = poisson) predict(fit, newdata = data.frame(x = c(10, 30)), type = "response")

  

Exercise 4: With offset (rate model)

Show solution
RInteractive R
df <- tibble(events = rpois(50, 5), exposure = sample(50:200, 50, replace = TRUE), group = sample(c("a","b"), 50, replace = TRUE)) glm(events ~ group + offset(log(exposure)), data = df, family = poisson)

  

Exercise 5: Test overdispersion

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, 5)) fit <- glm(count ~ x, data = df, family = poisson) sum(residuals(fit, type = "pearson")^2) / fit$df.residual # >1 suggests overdispersion

  

Exercise 6: Negative binomial alternative

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rnbinom(50, size = 2, mu = 5)) MASS::glm.nb(count ~ x, data = df)

  

Exercise 7: Quasi-Poisson for overdispersion

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rnbinom(50, size = 1, mu = 5)) glm(count ~ x, data = df, family = quasipoisson)

  

Exercise 8: Zero-inflated Poisson

Show solution
RInteractive R
# library(pscl); zeroinfl(count ~ x, data = df)

  

Exercise 9: AIC

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, 5)) fit <- glm(count ~ x, data = df, family = poisson) AIC(fit)

  

Exercise 10: Residual diagnostics

Show solution
RInteractive R
df <- tibble(x = 1:50, count = rpois(50, 5)) fit <- glm(count ~ x, data = df, family = poisson) par(mfrow = c(2,2)); plot(fit); par(mfrow = c(1,1))

  

Exercise 11: Multiple predictors

Show solution
RInteractive R
df <- tibble(x1 = 1:50, x2 = rnorm(50), count = rpois(50, 5)) glm(count ~ x1 + x2, data = df, family = poisson)

  

Exercise 12: Interaction

Show solution
RInteractive R
df <- tibble(x = 1:50, group = sample(c("a","b"), 50, replace = TRUE), count = rpois(50, 5)) glm(count ~ x * group, data = df, family = poisson)

  

What to do next

  • Logistic-Regression-Exercises (shipped), binary GLM.
  • Linear-Regression-Exercises (shipped), continuous outcome.

Ready to earn the Poisson Regression Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz