# HSCI 410 Lesson 1, Activity 1.4: processing variables and unconditional associations
# 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 raw file and build the cleaned version (as in Activity 1.2, part 6)
library(dplyr)
library(tidyr)
bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
bp_clean <- bp_raw |>
  drop_na(systolic, diastolic, age) |>
  mutate(age_c3 = cut(age, c(0, 35, 55, Inf), labels = c("young", "middle", "older")),
         htn = factor(systolic >= 140 | diastolic >= 90, levels = c(FALSE, TRUE),
                      labels = c("normotensive", "hypertensive")))
c(raw = nrow(bp_raw), clean = nrow(bp_clean))

# 2. Outcomes: a count for a Poisson model, a skewed continuous outcome
visits_a <- c(0, 2, 4, 3, 2, 6, 1, 3, 2, 3)    # clinic visits in a year, clinic A
visits_b <- c(0, 1, 1, 0, 9, 2, 0, 12, 1, 0)   # clinic visits in a year, clinic B
c(mean = mean(visits_a), var = var(visits_a))
c(mean = mean(visits_b), var = var(visits_b))
set.seed(4104)
los <- round(rlnorm(1000, meanlog = 1.2, sdlog = 0.8), 1)   # simulated length of stay (days)
summary(los)                     # before the log transformation
summary(log(los))                # after it
par(mfrow = c(1, 2)); hist(los, breaks = 40); hist(log(los), breaks = 30)

# 3. Predictors: missing values and sparse categories
colSums(is.na(bp_raw))                         # number missing per variable
round(100 * colMeans(is.na(bp_raw)), 1)        # percentage missing per variable
bp_clean <- bp_clean |>
  mutate(age_dec = cut(age, c(17, 29, 39, 49, 59, 69, 79, Inf),
                       labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80+")))
table(bp_clean$age_dec, bp_clean$htn)
bp_clean <- bp_clean |>
  mutate(age_dec6 = cut(age, c(17, 29, 39, 49, 59, 69, Inf),
                        labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70+")))
table(bp_clean$age_dec6, bp_clean$htn)

# 4. Multilevel data: measurements within people within centres (typed in)
bp_long <- tribble(
  ~centre, ~person, ~visit, ~sbp,
  "A", "A01", 1, 132,   "A", "A01", 2, 128,   "A", "A02", 1, 141,
  "A", "A02", 2, 139,   "A", "A02", 3, 137,   "B", "B01", 1, 118,
  "B", "B01", 2, 121,   "B", "B02", 1, 150,   "B", "B03", 1, 126,
  "B", "B03", 2, 124)
count(bp_long, centre, person)                          # measurements per person
bp_long |> distinct(centre, person) |> count(centre)    # people per centre
n_distinct(bp_long$person)                              # people overall

# 5. Unconditional associations, one pair of variable types at a time
cor.test(bp_clean$age, bp_clean$systolic)               # two continuous variables
plot(systolic ~ age, data = bp_clean)                   # scatterplot
fit1 <- lm(systolic ~ age, data = bp_clean)
cbind(estimate = coef(fit1), confint(fit1))
tapply(bp_clean$systolic, bp_clean$smoker, mean)        # continuous by two groups
fit2 <- lm(systolic ~ smoker, data = bp_clean)
cbind(estimate = coef(fit2), confint(fit2))
tapply(bp_clean$systolic, bp_clean$age_c3, mean)        # three or more groups
summary(aov(systolic ~ age_c3, data = bp_clean))
tab <- table(smoker = bp_clean$smoker, htn = bp_clean$htn)   # two categorical
tab
round(100 * prop.table(tab, margin = 1), 1)              # row percentages
chisq.test(tab)
chisq.test(tab)$expected                                 # expected counts if unrelated
