# HSCI 410 Lesson 6, Activity 6.2: quick looks in base R
# 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 survey 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"))   # survey data, read from the web
data <- data[data$SURVEY_collection_year == 2021, ]   # the 2021 wave: one row per person
nrow(data)                                             # how many people?

# 2. The briefing file: short names, bracket recodes and complete cases
data$loneliness <- data$LONELY_ucla_loneliness_scale_score   # 3 to 9, higher = lonelier
data$support    <- data$PSYCH_zimet_multidimensional_social_support_scale_score   # 1 to 7
data$depression <- data$WELLNESS_phq_score                   # 0 to 6
data$anxiety    <- data$WELLNESS_gad_score                   # 0 to 6
data$life_sat   <- data$WELLNESS_life_satisfaction_num       # 1 to 10
data$age        <- data$DEMO_age                             # years
data$gender <- as.character(data$DEMO_gender)
data$gender[data$gender == "Presented but no response"] <- NA
data$gender <- factor(data$gender, levels = c("Woman", "Man", "Non-binary"))
data$mental_health <- as.character(data$WELLNESS_self_rated_mental_health)
data$mental_health[data$mental_health == "Presented but no response"] <- NA
data$mental_health <- factor(data$mental_health,
  levels = c("Poor", "Fair", "Good", "Very good", "Excellent"))
data$age_group[data$age < 30] <- "16 to 29"
data$age_group[data$age >= 30 & data$age < 45] <- "30 to 44"
data$age_group[data$age >= 45 & data$age < 65] <- "45 to 64"
data$age_group[data$age >= 65] <- "65 and older"
data$age_group <- factor(data$age_group,
  levels = c("16 to 29", "30 to 44", "45 to 64", "65 and older"))
brief <- na.omit(data[, c("loneliness", "support", "depression", "anxiety",
                          "life_sat", "age", "age_group", "gender", "mental_health")])
c(kept = nrow(brief), left_out = nrow(data) - nrow(brief))   # people kept and left out

# 3. One variable: a histogram and a bar chart
summary(brief$loneliness)                  # a numeric summary first
table(brief$loneliness)                    # the counts the bars will show
hist(brief$loneliness, breaks = seq(2.5, 9.5, by = 1),   # one bar for each score
     main = "Loneliness (UCLA 3-item scale)",
     xlab = "Loneliness score (3 to 9)", ylab = "Number of people")
table(brief$mental_health)                 # the counts the bars will show
barplot(table(brief$mental_health), main = "Self-rated mental health",
        xlab = "Rating", ylab = "Number of people")

# 4. A problem that cleaning left behind: household size
summary(data$GEO_housing_household_size)   # people each respondent lives with
hist(data$GEO_housing_household_size, breaks = seq(-0.5, 20.5, by = 1),
     main = "Household size",
     xlab = "Number of people the respondent lives with", ylab = "Number of people")
sum(data$GEO_housing_household_size == 20, na.rm = TRUE)   # how many at the top value?

# 5. Two variables: boxplots of loneliness by age group
boxplot(loneliness ~ age_group, data = brief, main = "Loneliness by age group",
        xlab = "Age group", ylab = "Loneliness score (3 to 9)")
tapply(brief$loneliness, brief$age_group, median)   # the thick line in each box
tapply(brief$loneliness, brief$age_group, IQR)      # the length of each box

# 6. Two numeric variables: solid points, then jitter and transparency
plot(brief$support, brief$loneliness,                  # every person as one point
     xlab = "Social support (1 to 7)", ylab = "Loneliness score (3 to 9)")
set.seed(2021)                                         # the same jitter every time
plot(jitter(brief$support), jitter(brief$loneliness),  # nudge each point a little
     pch = 16, col = rgb(0, 0, 0, 0.15),               # small grey see-through dots
     xlab = "Social support (1 to 7)", ylab = "Loneliness score (3 to 9)")
pairs(brief[, c("loneliness", "support", "depression", "age")],
      pch = 16, col = rgb(0, 0, 0, 0.1))               # every pair in one grid

# 7. Two plots side by side, saved to a file
png("briefing_base_r.png", width = 1600, height = 800, res = 200)   # open a file
par(mfrow = c(1, 2))                       # one row, two plots
hist(brief$loneliness, breaks = seq(2.5, 9.5, by = 1),
     main = "A. Loneliness", xlab = "Loneliness score", ylab = "Number of people")
boxplot(loneliness ~ age_group, data = brief,
        main = "B. Loneliness by age group", xlab = "Age group", ylab = "Loneliness score")
par(mfrow = c(1, 1))                       # back to one plot per window
dev.off()                                  # close the file, which saves it
