# HSCI 410 Lesson 4, Activity 4.4: Poisson and negative binomial regression for counts
# Run-along script for the narrated walkthrough. It reads the course data
# directly from the course website, so there is nothing to set up: open it
# in RStudio and run each part with Ctrl+Enter when the narration asks.

# 1. Read the data and look at the counts
library(MASS)                           # glm.nb() fits the negative binomial model (part 5)
phaa <- read.csv("https://heal-learn.ca/r-activities/data/phaa_outcomes.csv")   # course data, read from the website
phaa <- na.omit(phaa)                   # keep the 795 people with no missing values
phaa$smoker <- factor(phaa$smoker, levels = c("No", "Yes"))   # "No" = reference
table(phaa$urgent_visits)    # how many people had 0, 1, 2, ... visits
mean(phaa$urgent_visits)     # the average count
var(phaa$urgent_visits)      # the variance (spread) of the counts

# 2. Poisson regression: incidence rate ratios (IRRs)
pois <- glm(urgent_visits ~ age + smoker, data = phaa, family = poisson)
summary(pois)
exp(cbind(IRR = coef(pois), confint(pois)))   # IRRs with 95% CIs
exp(10 * coef(pois)["age"])                   # IRR for ten years of age

# 3. Different follow-up times: an offset turns counts into rates
fu <- read.csv("https://heal-learn.ca/r-activities/data/phaa_followup.csv")   # course data, read from the website
fu$smoker <- factor(fu$smoker, levels = c("No", "Yes"))   # GP visits over 0 to 10 years
gp <- glm(gp_visits ~ age + smoker + offset(log(fu_years)), data = fu, family = poisson)
round(exp(cbind(IRR = coef(gp), confint(gp))), 3)

# 4. Overdispersion checks for the Poisson model
sum(residuals(pois, type = "pearson")^2) / df.residual(pois)   # dispersion ratio
sum(phaa$urgent_visits == 0)                                   # zeros observed
sum(dpois(0, fitted(pois)))                                    # zeros expected

# 5. Negative binomial regression
nb <- glm.nb(urgent_visits ~ age + smoker, data = phaa)
summary(nb)
exp(cbind(IRR = coef(nb), confint(nb)))       # IRRs with 95% CIs
sum(dnbinom(0, mu = fitted(nb), size = nb$theta))   # zeros expected by NB
