# HSCI 410 Lesson 3, Activity 3.3: fit, read and check a logistic regression
# 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.
# install.packages(c("lmtest", "ResourceSelection", "pROC"))   # run once, if not yet installed
library(lmtest); library(ResourceSelection); library(pROC)

# 1. Read the data and create the yes-or-no outcome
phaa <- read.csv("https://heal-learn.ca/r-activities/data/phaa_survey_clean.csv")   # course data, read from the website
phaa$smoker <- factor(phaa$smoker, levels = c("No", "Yes"))   # "No" = reference
phaa$gender <- factor(phaa$gender, levels = c("Man", "Woman", "Non-binary"))
phaa$elevated_bp <- factor(ifelse(phaa$systolic_bp >= 120, "Yes", "No"),
                           levels = c("No", "Yes"))   # "Yes" = 120 mmHg or more
table(phaa$hypertension)       # the original variable: too few events
cc <- na.omit(phaa[, c("elevated_bp", "age", "gender", "smoker", "bmi", "dep_score")])
table(cc$elevated_bp)          # events among the people in the model

# 2. Why not a straight line? lm() on the 0/1 outcome
cc$y01 <- as.numeric(cc$elevated_bp == "Yes")   # 1 = elevated, 0 = not
lpm <- lm(y01 ~ age + gender + smoker + bmi + dep_score, data = cc)
summary(fitted(lpm))           # the predicted "probabilities"
sum(fitted(lpm) < 0)           # how many are below 0?
bptest(lpm)                    # equal spread (Breusch-Pagan test)
shapiro.test(residuals(lpm))   # normality of the residuals
plot(lpm, which = 1)           # Residuals vs Fitted

# 3. Fit the logistic model and read the odds ratios
logit <- glm(elevated_bp ~ age + gender + smoker + bmi + dep_score,
             family = binomial, data = cc)
summary(logit)
round(exp(cbind(OR = coef(logit), confint(logit))), 2)   # odds ratios, 95% CIs
logit_ng <- update(logit, . ~ . - gender)    # the same model without gender
anova(logit_ng, logit, test = "LRT")         # gender as a whole

# 4. Ten years of age and predicted probabilities
round(exp(10 * coef(logit)[["age"]]), 2)     # odds ratio for 10 years of age
two <- data.frame(age = 50, gender = "Man", smoker = c("No", "Yes"),
                  bmi = 21, dep_score = 21)  # differ only in smoking
round(predict(logit, newdata = two, type = "response"), 3)

# 5. Check the model
table(cc$elevated_bp)[["Yes"]] / 6           # events per coefficient
null_model <- glm(elevated_bp ~ 1, family = binomial, data = cc)
anova(null_model, logit, test = "Chisq")     # better than no predictors?
logit_sq <- update(logit, . ~ . + I(age^2))
anova(logit, logit_sq, test = "Chisq")       # linearity on the log-odds scale
round(1 - logit$deviance / logit$null.deviance, 3)   # McFadden pseudo R-squared
