# HSCI 410 Lesson 8, Activity 8.2: moderation by age group and by age in years
# Run-along script for the narrated walkthrough. It reads the survey data
# directly from the web, 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 CSCS data and make the two age groups
github_url <- "https://raw.githubusercontent.com/jorgeandr3s/heal/main/cscs/public_data/CSCS2025_full_cleaned_deidentified_data_and_metadata.RData"
load(url(github_url))                      # survey data, read from the web
data <- data[data$SURVEY_collection_year == 2021, ]   # the 2021 wave
med_data <- data.frame(
  support    = data$PSYCH_zimet_multidimensional_social_support_scale_score,
  loneliness = data$LONELY_ucla_loneliness_scale_score,
  depression = data$WELLNESS_phq_score,    # used in Section 3
  age        = data$DEMO_age)
med_data <- na.omit(med_data)              # the same people in every model
med_data$age_group <- NA
med_data$age_group[med_data$age < 50] <- "Under 50"
med_data$age_group[med_data$age >= 50] <- "50 and over"
med_data$age_group <- factor(med_data$age_group,
                             levels = c("Under 50", "50 and over"))
table(med_data$age_group)                  # "Under 50" is the reference

# 2. Adjusting for age group, then letting the slope differ
model_main <- lm(loneliness ~ support + age_group, data = med_data)
coef(model_main)                           # one support slope for everyone
model_int <- lm(loneliness ~ support * age_group, data = med_data)
summary(model_int)
anova(model_main, model_int)               # does the interaction improve the model?

# 3. The slope of support in each age group, and a plot
coef(model_int)["support"]                                     # Under 50
coef(model_int)["support"] + coef(model_int)["support:age_group50 and over"]   # 50 and over
plot(jitter(med_data$support), jitter(med_data$loneliness),
     xlab = "Social support (1 to 7)", ylab = "UCLA loneliness score (3 to 9)",
     pch = 16, col = rgb(0, 0, 0, 0.10))
abline(lm(loneliness ~ support,
          data = med_data[med_data$age_group == "Under 50", ]),
       col = "steelblue", lwd = 3)
abline(lm(loneliness ~ support,
          data = med_data[med_data$age_group == "50 and over", ]),
       col = "firebrick", lwd = 3)
legend("topright", legend = c("Under 50", "50 and over"),
       col = c("steelblue", "firebrick"), lwd = 3)

# 4. Age in years: centre both variables, then fit the interaction
med_data$age_c     <- med_data$age - mean(med_data$age)
med_data$support_c <- med_data$support - mean(med_data$support)
round(c(mean_age = mean(med_data$age), mean_support = mean(med_data$support)), 2)
model_raw <- lm(loneliness ~ support * age, data = med_data)       # uncentred
model_cen <- lm(loneliness ~ support_c * age_c, data = med_data)   # centred
round(summary(model_raw)$coefficients[, 1:3], 4)   # estimate, SE, t value
round(summary(model_cen)$coefficients[, 1:3], 4)

# 5. Simple slopes of support at ages 30, 50 and 70
b <- coef(model_cen)
ages <- c(30, 50, 70)
slopes <- b["support_c"] + b["support_c:age_c"] * (ages - mean(med_data$age))
names(slopes) <- paste("age", ages)
round(slopes, 3)
