# HSCI 410 Lesson 7, Activity 7.3: convergent, discriminant and known-groups evidence
# Run-along script for the narrated walkthrough. It reads the CSCS 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 keep the 2021 wave
load(url("https://raw.githubusercontent.com/jorgeandr3s/heal/main/cscs/public_data/CSCS2025_full_cleaned_deidentified_data_and_metadata.RData"))   # CSCS data, read from the web
data <- data[data$SURVEY_collection_year == 2021, ]   # one row per person
nrow(data)

# 2. Scores supplied with the file (they match the Section 1 scoring)
social    <- data$LONELY_dejong_social_loneliness_sub_scale_score      # 0 to 3
emotional <- data$LONELY_dejong_emotional_loneliness_sub_scale_score   # 0 to 3
val <- data.frame(total     = data$LONELY_dejong_emotional_social_loneliness_scale_score,
                  social    = social,
                  emotional = emotional,
                  ucla      = data$LONELY_ucla_loneliness_scale_score,
                  support   = data$PSYCH_zimet_multidimensional_social_support_scale_score,
                  anxiety   = data$WELLNESS_gad_score)
val <- na.omit(val)          # people with every score
nrow(val)

# 3. Convergent and discriminant evidence
round(cor(val), 2)           # correlations between every pair of measures

# 4. Known groups: fewer close friends, more social loneliness
friends <- data$CONNECTION_social_num_close_friends_grouped
friends[friends == "Presented but no response"] <- NA
friends <- factor(friends, levels = c("None", "1–2", "3–4", "5 or more"))
table(friends)               # people in each group
round(tapply(social, friends, mean, na.rm = TRUE), 2)   # mean social score
