# =============================================================================
# HSCI 410 Lesson 6: Exploratory Data Analysis and Visualization -- ANSWER KEY
# Author: Kiffer G. Card
# -----------------------------------------------------------------------------
# Page:  modules/HSCI_410_Lesson_6_Exploratory_Data_Analysis_and_Visualization.html
# Data:  Section 1 uses the anscombe data frame that comes with R. Section 2
#        reads the Canadian Social Connection Survey (CSCS) file from the web
#        with load(url(...)) and keeps the 2021 wave. Sections 3 and 4 read the
#        briefing file built in Section 2, cscs_brief.csv, directly from
#        https://heal-learn.ca/r-activities/data/ (an internet connection is
#        needed).
#
# This script reproduces every code block on the page, in page order, and then
# answers each numbered activity question with a printed result. It reads the
# data from the web, so it runs from any folder, from top to bottom in one R
# session. Plots appear in the Plots pane, and the three saved figures
# (briefing_base_r.png, loneliness_age_gender.png and briefing_figure.png) are
# written to R's current folder, which getwd() shows.
#
# Packages: ggplot2, ggExtra and patchwork. Install them once with
#   install.packages(c("ggplot2", "ggExtra", "patchwork"))
# Data preparation uses base R only (bracket recoding and na.omit()).
# =============================================================================

options(width = 70)


# =============================================================================
# SECTION 1 ACTIVITY: Anscombe's quartet in R (Activity 6.1 on the lesson page)
# =============================================================================
anscombe                          # four small datasets that come with R
round(colMeans(anscombe), 2)      # the mean of each column
round(sapply(anscombe, sd), 2)    # the standard deviation of each column

round(cor(anscombe$x1, anscombe$y1), 3)   # correlation in set 1
round(cor(anscombe$x2, anscombe$y2), 3)   # set 2
round(cor(anscombe$x3, anscombe$y3), 3)   # set 3
round(cor(anscombe$x4, anscombe$y4), 3)   # set 4
coef(lm(y1 ~ x1, data = anscombe))        # intercept and slope, set 1
coef(lm(y3 ~ x3, data = anscombe))        # set 3
coef(lm(y4 ~ x4, data = anscombe))        # set 4

par(mfrow = c(2, 2))    # four plots in one window: two rows, two columns
plot(anscombe$x1, anscombe$y1, main = "Set 1", xlab = "x", ylab = "y")
abline(3, 0.5, col = "red")       # add the line y = 3 + 0.5x
plot(anscombe$x2, anscombe$y2, main = "Set 2", xlab = "x", ylab = "y")
abline(3, 0.5, col = "red")
plot(anscombe$x3, anscombe$y3, main = "Set 3", xlab = "x", ylab = "y")
abline(3, 0.5, col = "red")
plot(anscombe$x4, anscombe$y4, main = "Set 4", xlab = "x", ylab = "y")
abline(3, 0.5, col = "red")
par(mfrow = c(1, 1))    # back to one plot per window

# --- Answers ---------------------------------------------------------------
# Q1. All four sets share: mean x = 9.0, mean y = 7.5, SD x = 3.32,
#     SD y = 2.03, r = 0.816 (0.817 in set 4) and the line y = 3.00 + 0.500x.
#     r = 0.816 is a strong positive straight-line association: larger x tends
#     to go with larger y, although the points do not lie exactly on a line.
#     An analyst who saw only these numbers would conclude that the four sets
#     show the same strong, positive, straight-line relationship, with y rising
#     by about 0.5 for each one-unit increase in x.
cat("\nQ1. Intercept and slope in each set:\n")
fits <- sapply(1:4, function(i) coef(lm(anscombe[[paste0("y", i)]] ~ anscombe[[paste0("x", i)]])))
colnames(fits) <- paste("Set", 1:4); rownames(fits) <- c("intercept", "slope")
print(round(fits, 3))
# Q2. Set 1: points scattered evenly around a rising straight line. Set 2: a
#     smooth arch, strong but curved. Set 3: almost exactly a line, with one
#     point far above it (row 3). Set 4: ten points at x = 8 and one point at
#     x = 19 that creates the whole relationship. The line is a fair summary
#     only for set 1. The plots show the form of each relationship and the
#     single points that drive it; Pearson's r measures only straight-line
#     association, so set 2 would need a curve (a squared term for x).
cat("\nQ2. Set 2 is exactly quadratic; a squared term fits it almost perfectly:\n")
print(round(summary(lm(y2 ~ x2 + I(x2^2), data = anscombe))$r.squared, 4))
# Q3. Your own code: refit set 3 without row 3, the point far above the line.
coef(lm(y3 ~ x3, data = anscombe[-3, ]))
cor(anscombe$x3[-3], anscombe$y3[-3])
#     Without row 3 the line is y = 4.01 + 0.345x and r = 0.9999966 (almost
#     exactly 1): one point raises the slope from 0.345 to 0.500 and lowers r
#     to 0.816. Do not simply remove it: check it against the source records
#     and the codebook for an entry error. An error is corrected or set to
#     missing; a genuine value is kept, and the results are reported with and
#     without it as a sensitivity analysis, with the decision in the methods.
#     Identical summaries can hide a curve, an outlier or a single influential
#     point, so the analyst looks at plots before fitting any model.

# =============================================================================
# SECTION 2 ACTIVITY: quick looks in base R (Activity 6.2 on the lesson page)
# Run-along script: r-activities/run-along/HSCI_410_Lesson_6_Activity_6_2.R. It reads
# the CSCS file from the web, so there is nothing to set up.
# =============================================================================
# 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,083 respondents are kept, and 962 of the 4,045 are left out for missing data.

# 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")
# Very good is the most common rating (1,024); far fewer people rated their
# mental health as poor (230) or fair (467).

# 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?

# --- Answer, question 1 ------------------------------------------------------
# Loneliness: a ceiling. The bar at 9, the highest score the scale allows, holds
#   270 people, more than the 179 at 8; the summary shows only a maximum of 9.
# Household size: top-coding. A real distribution does not rise again at its
#   maximum, yet 116 people sit at exactly 20, because totals above 20 were
#   recorded as 20, so 20 means "20 or more"; the summary (median 3, mean 4.463,
#   maximum 20) did not reveal it.
# Check: the codebook and the source questions confirm what 20 means; sum()
#   gives the number of people affected (116).
# Decide: used as a count, 20 cannot be trusted and is set to missing with a
#   bracket recode (below); used in groups with a top group (0, 1, 2 to 4,
#   5 or more), it is kept as "5 or more" and matters much less.
# Report: the methods state the problem, the decision and the number affected
#   (116), and the analysis is repeated both ways as a sensitivity analysis if
#   the decision could change a result.
data$household <- data$GEO_housing_household_size
data$household[data$household == 20] <- NA
print(summary(data$household))
cat("\nGrouped household size, with a top group of 5 or more:\n")
print(table(cut(data$GEO_housing_household_size, c(-1, 0, 1, 4, Inf),
                labels = c("0", "1", "2 to 4", "5 or more"))))

# --- Checks behind the reading ----------------------------------------------
cat("\nHousehold size is the sum of eight 'live with' questions, capped at 20:\n")
live_with <- c("GEO_housing_live_with_partner", "GEO_housing_live_with_children",
               "GEO_housing_live_with_grandkids", "GEO_housing_live_with_parent",
               "GEO_housing_live_with_in_laws", "GEO_housing_live_with_siblings",
               "GEO_housing_live_with_roommate", "GEO_housing_live_with_other")
total <- rowSums(data[, live_with], na.rm = TRUE)
hh <- data$GEO_housing_household_size
print(table(capped_sum_matches = pmin(total, 20)[!is.na(hh)] == hh[!is.na(hh)]))
print(c(at_20 = sum(hh == 20, na.rm = TRUE),
        total_above_20 = sum(total > 20 & hh == 20, na.rm = TRUE),
        largest_total = max(total[!is.na(hh) & hh == 20]),
        living_alone = sum(hh == 0, na.rm = TRUE)))
cat("\nHours worked per week: heaping and implausible values:\n")
hours <- data$WORK_hours_per_week
print(c(answered = sum(!is.na(hours)), at_39 = sum(hours == 39, na.rm = TRUE),
        at_40 = sum(hours == 40, na.rm = TRUE), at_41 = sum(hours == 41, na.rm = TRUE),
        at_35 = sum(hours == 35, na.rm = TRUE), above_112 = sum(hours > 112, na.rm = TRUE),
        maximum = max(hours, na.rm = TRUE)))
hist(hours, breaks = seq(0, 140, by = 2), main = "Hours worked per week",
     xlab = "Hours", ylab = "Number of people")

# 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

# --- Answer, question 2 ------------------------------------------------------
cat("\nBoxplot statistics (whisker, quartile, median, quartile, whisker):\n")
bp <- boxplot(loneliness ~ age_group, data = brief, plot = FALSE)
stats <- bp$stats; colnames(stats) <- bp$names
print(stats)
print(table(points_beyond_whiskers_16_to_29 = bp$out[bp$group == 1]))
# Centre: medians 5 (16 to 29, 30 to 44) and 6 (45 to 64, 65 and older), so older
#   respondents tend to report a little more loneliness.
# Spread: the box (IQR) is longest for 45 to 64 (4 points, 4 to 8) and shortest
#   for 16 to 29 (1 point, 5 to 6, as most scored 5 or 6); in the three older
#   groups the whiskers reach 3 to 9, the whole scale, so differences between
#   groups are small next to the variation within each group.
# No, do not remove the points beyond the whiskers: the 1.5 x IQR rule flags
#   them, it does not show them to be wrong. In the narrow 16 to 29 box ordinary
#   scores of 3, 8 and 9 (212 people) fall beyond whiskers at 4 and 7. They are
#   possible answers, so check for impossible values, keep them and report the
#   decision; deleting them would bias the comparison.

# 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
# Nothing appears in the Plots pane: png() sent both plots to briefing_base_r.png
# in R's working directory, and dev.off() closed and saved it.

# --- Answer, question 3 (your own code) ----------------------------------------
plot(jitter(brief$depression), jitter(brief$loneliness), pch = 16, col = rgb(0, 0, 0, 0.15))
# Both scores take only seven whole values, so solid points would sit on top of
#   one another on a grid of 49 positions (overplotting). Jitter adds a small
#   random shift to where each point is drawn, and the see-through grey (opacity
#   0.15) makes crowded areas darker, so the plot shows where most people are.
# Jitter changes only the picture: brief, and any correlation or model, stay the same.
# Loneliness tends to be higher with more depressive symptoms: people with no
#   symptoms cluster at loneliness 3, the darkest areas at depression 2 and 3
#   are at loneliness 5 and 6, and at depression 6 loneliness piles up at 9.
cat("\nMean loneliness at each depression score, and the correlation:\n")
print(round(tapply(brief$loneliness, brief$depression, mean), 2))
print(round(cor(brief$depression, brief$loneliness), 3))
# Saving with code (lines 69 to 76 of the run-along script) is better than
#   exporting by hand: the code records exactly how the figure was made and
#   remakes it in one step if the data change.

# =============================================================================
# SECTION 3 ACTIVITY: the grammar of graphics with ggplot2 (Activity 6.3 on the lesson page)
# Run-along script: r-activities/run-along/HSCI_410_Lesson_6_Activity_6_3.R. It reads
# the briefing file built in Section 2 (cscs_brief.csv) from the course website.
# =============================================================================
brief <- read.csv("https://heal-learn.ca/r-activities/data/cscs_brief.csv")   # course data, read from the website
brief$age_group <- factor(brief$age_group,
  levels = c("16 to 29", "30 to 44", "45 to 64", "65 and older"))
brief$gender <- factor(brief$gender, levels = c("Woman", "Man", "Non-binary"))
brief$mental_health <- factor(brief$mental_health,
  levels = c("Poor", "Fair", "Good", "Very good", "Excellent"))
nrow(brief)                      # one row per person
# install.packages("ggplot2")    # once per computer, if not yet installed
library(ggplot2)

# One variable: a histogram, then bars of counts and bars of values
ggplot(brief, aes(x = loneliness)) +               # data and mapping
  geom_histogram(binwidth = 1,                     # one bar per score
                 fill = "#0B7B6B", colour = "white") +   # set, not mapped
  labs(title = "Loneliness scores",
       x = "Loneliness score (3 to 9)", y = "Number of people")
ggplot(brief, aes(x = mental_health)) +
  geom_bar() +                                     # counts the rows
  labs(x = "Self-rated mental health", y = "Number of people")
means <- aggregate(loneliness ~ age_group, data = brief, FUN = mean)
means                                              # one row per age group
ggplot(means, aes(x = age_group, y = loneliness)) +
  geom_col() +                                     # draws the values given
  labs(x = "Age group", y = "Mean loneliness score")

# A numeric variable across groups: boxplots with every person
set.seed(2021)                                     # the same jitter every time
ggplot(brief, aes(x = age_group, y = loneliness)) +
  geom_boxplot(outlier.shape = NA) +               # no separate outlier dots
  geom_jitter(width = 0.2, height = 0.2, alpha = 0.1) +   # every person
  labs(x = "Age group", y = "Loneliness score (3 to 9)")

# Two numeric variables with a loess smoother
set.seed(2021)
ggplot(brief, aes(x = age, y = loneliness)) +
  geom_jitter(height = 0.2, alpha = 0.1) +
  geom_smooth(method = "loess") +                  # a curve that can bend
  labs(x = "Age (years)", y = "Loneliness score (3 to 9)")

# One panel per group: facet_wrap() and facet_grid()
ggplot(brief, aes(x = loneliness)) +
  geom_histogram(binwidth = 1, fill = "#0B7B6B", colour = "white") +
  facet_wrap(~ age_group) +                        # one panel per age group
  labs(x = "Loneliness score (3 to 9)", y = "Number of people")
set.seed(2021)
ggplot(brief, aes(x = support, y = loneliness)) +
  geom_jitter(height = 0.2, alpha = 0.2) +
  geom_smooth(method = "lm") +                     # a straight line per panel
  facet_grid(gender ~ age_group) +                 # rows: gender; columns: age
  labs(x = "Social support (1 to 7)", y = "Loneliness score (3 to 9)")
table(brief$gender, brief$age_group)               # people in each panel

# Colour, a theme, and saving the figure
p <- ggplot(brief, aes(x = age_group, y = loneliness, fill = gender)) +
  geom_boxplot() +
  scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +   # Okabe-Ito
  labs(x = "Age group", y = "Loneliness score (3 to 9)", fill = "Gender") +
  theme_minimal(base_size = 12)                    # a plain theme
p                                                  # show the plot
ggsave("loneliness_age_gender.png", p, width = 7, height = 4.5, dpi = 300)

# --- Answers ---------------------------------------------------------------
cat("\nQ1. Quartiles of loneliness and the number of people in each age group:\n")
print(sapply(split(brief$loneliness, brief$age_group), quantile))
print(table(brief$age_group))
print(round(setNames(means$loneliness, means$age_group), 2))
# Q1. geom_boxplot(outlier.shape = NA) draws a box for each age group, with the
#     median, the quartiles and the whiskers, and leaves out separate outlier
#     dots because the jittered points already show everyone. geom_jitter(width
#     = 0.2, height = 0.2, alpha = 0.1) draws one point per person, shifted a
#     little at random sideways and up or down, so that the many people who
#     share a score do not sit on top of one another; transparency makes crowded
#     areas darker, and jitter changes only the picture, not the data. Unlike
#     four means between 5.39 and 6.01, the figure shows the spread (every group
#     spans 3 to 9; the 45 to 64 box runs from 4 to 8), the size of each group
#     (366 aged 65 and older against 1,079 aged 16 to 29) and the medians
#     (5, 5, 6 and 6).
cat("\nNarration check: the loess curve at selected ages, and people at the two ends:\n")
lo <- loess(loneliness ~ age, data = brief)   # the same smoother as geom_smooth()
ages <- c(16, 30, 55, 70, 80)
print(round(setNames(predict(lo, data.frame(age = ages)), paste("age", ages)), 2))
print(c(under_20 = sum(brief$age < 20), aged_80_plus = sum(brief$age >= 80)))
cat("\nQ2. Slope of loneliness on social support in each panel of the grid:\n")
slopes <- sapply(levels(brief$age_group), function(a) sapply(levels(brief$gender), function(g)
  coef(lm(loneliness ~ support, data = brief[brief$gender == g & brief$age_group == a, ]))[["support"]]))
print(round(slopes, 2))
# Q2. Every line slopes downward: people with more social support tend to report
#     less loneliness in every combination of gender and age group, and the lines
#     look steeper in the two older age groups. The grey band is the 95%
#     confidence interval for the line (the uncertainty about the average
#     loneliness at each level of support), not the spread of individual people.
#     The non-binary panels hold only 25, 26, 12 and 6 people, so their lines are
#     very uncertain and their bands wide. The briefing should show these panels,
#     state how many people each one holds, and draw no conclusion about
#     differences for non-binary respondents from them.
# Q3. Your own code: a second cue that survives black-and-white printing, and
#     the size of the saved file in pixels.
p + aes(linetype = gender) + labs(linetype = "Gender")
c(width = 7, height = 4.5) * 300
cat("\nQ3. The three fill colours as shades of grey (0 = black, 255 = white):\n")
print(round(colSums(col2rgb(c("#E69F00", "#56B4E9", "#009E73")) * c(0.299, 0.587, 0.114))))
# Q3. The Okabe-Ito colours were designed so that people with the common forms
#     of colour-vision deficiency, such as red-green deficiency, can tell the
#     groups apart; the default colours for three groups include a red and a
#     green. In greyscale the orange (162) and the sky blue (158) print as almost
#     the same grey, so the line type is a second cue: solid, short-dashed and
#     long-dashed outlines, shown together with the colours in one legend. In
#     ggsave(), width and height set the size in inches (7 by 4.5) and dpi the
#     resolution (300 dots per inch, the usual print standard). The second line
#     of your own code multiplies the width and the height by the resolution:
#     the file is 2,100 pixels wide and 1,350 pixels high, enough for a sharp
#     print at 7 by 4.5 inches.

# =============================================================================
# SECTION 4 ACTIVITY: many variables and a combined figure (Activity 6.4 on the lesson page)
# Run-along script: r-activities/run-along/HSCI_410_Lesson_6_Activity_6_4.R. It reads
# the briefing file (cscs_brief.csv) from the course website.
# =============================================================================
brief <- read.csv("https://heal-learn.ca/r-activities/data/cscs_brief.csv")   # course data, read from the website
brief$age_group <- factor(brief$age_group,
  levels = c("16 to 29", "30 to 44", "45 to 64", "65 and older"))   # order of the groups
nrow(brief)          # people with complete data in the 2021 wave
# install.packages(c("ggplot2", "ggExtra", "patchwork"))   # once, if R cannot find them
library(ggplot2)
library(ggExtra)     # ggMarginal(): histograms in the margins of a plot
library(patchwork)   # + and / combine ggplot2 plots into one figure

# A correlation matrix, and Spearman's rank correlation
vars <- c("loneliness", "support", "depression", "anxiety", "life_sat", "age")
cor_mat <- round(cor(brief[, vars]), 2)     # Pearson correlations, rounded
cor_mat
round(cor(brief$loneliness, brief$support, method = "spearman"), 2)

# The matrix as a heatmap
cor_long <- as.data.frame(as.table(cor_mat))   # one row for each pair
names(cor_long) <- c("var1", "var2", "r")
head(cor_long)
p_heat <- ggplot(cor_long, aes(x = var1, y = var2, fill = r)) +
  geom_tile(colour = "white") +              # one coloured square per pair
  geom_text(aes(label = r), size = 3) +      # the value on each square
  scale_fill_gradient2(low = "#2166AC", mid = "white", high = "#B2182B",
                       limits = c(-1, 1)) +  # blue negative, red positive
  labs(x = NULL, y = NULL, fill = "r")
p_heat

# A scatterplot with marginal histograms
set.seed(2021)
p_scatter <- ggplot(brief, aes(x = support, y = loneliness)) +
  geom_jitter(height = 0.2, width = 0, alpha = 0.15) +
  geom_smooth(method = "lm", colour = "#CC0033") +
  labs(x = "Social support (1 to 7)", y = "Loneliness score (3 to 9)")
ggMarginal(p_scatter, type = "histogram")    # a histogram on each axis
head(sort(table(brief$support), decreasing = TRUE), 3)   # the tallest spikes

# Four plots in one figure, saved for print
p_hist <- ggplot(brief, aes(x = loneliness)) +
  geom_histogram(binwidth = 1, fill = "#0B7B6B", colour = "white") +
  labs(x = "Loneliness score (3 to 9)", y = "Number of people")
p_box <- ggplot(brief, aes(x = age_group, y = loneliness)) +
  geom_boxplot(fill = "#E6F3F0") +
  labs(x = "Age group", y = "Loneliness score (3 to 9)")
figure <- (p_hist + p_box) / (p_scatter + p_heat) +   # + side by side, / stacked
  plot_annotation(tag_levels = "A",                     # label the panels A to D
    title = "Loneliness and social connection, CSCS 2021 (n = 3,083)")
figure
ggsave("briefing_figure.png", figure, width = 11, height = 8.5, dpi = 300)

# --- Answers ---------------------------------------------------------------
cat("\nQ1. Correlations with loneliness, strongest first (by size, whatever the sign):\n")
r_lonely <- cor_mat["loneliness", vars != "loneliness"]
print(r_lonely[order(-abs(r_lonely))])
# Q1. Depressive symptoms are most strongly related to loneliness (r = 0.47),
#     followed by anxiety (0.43) and social support (-0.40). Respondents with more
#     depressive symptoms tend to report more loneliness, a moderate positive
#     association, although many individuals depart from that trend. Spearman's
#     correlation uses ranks, so it is less affected by skew and a few extreme
#     values; it is close to the Pearson value (-0.36 against -0.40), so the
#     Pearson value is not being driven by a few extreme values.
# Q2. r = -0.40 shows only that people with more support tend to report less
#     loneliness, in a survey collected at one time. It cannot show which comes
#     first: loneliness could lower the support people perceive (reverse
#     causation), or poor health could reduce support and raise loneliness
#     (confounding), so the claim should be reworded as an association.
#     r = 0.07 rules out only a straight line; the loess curve in Section 3 dips
#     around age 30, rises into the fifties and falls after 70. Look at a
#     scatterplot of loneliness against age with a loess smoother.
# Spikes in the support margin (whole-number scores):
print(head(sort(table(brief$support), decreasing = TRUE), 3))
cat("\nQ3. Your own code, then plain labels for the heatmap:\n")
figure & theme_minimal()     # & applies the theme to every panel; + only to the last
print(c(width_px = 11 * 300, height_px = 8.5 * 300))
# Q3 caption. Loneliness and social connection among respondents to the 2021 wave
#     of the Canadian Social Connection Survey (n = 3,083 with complete data on all
#     variables shown; 962 of 4,045 respondents were excluded for missing data).
#     (A) Distribution of the three-item UCLA Loneliness Scale score (range 3 to 9;
#     higher scores mean more loneliness). (B) Loneliness score by age group; boxes
#     show the median and interquartile range, whiskers extend to the most extreme
#     values within 1.5 times the interquartile range, and points show values
#     beyond the whiskers. (C) Loneliness score against perceived social support
#     (range 1 to 7); points are jittered vertically and drawn with transparency,
#     and the line is a least-squares line with its 95% confidence band.
#     (D) Pearson correlations among loneliness, social support, depressive
#     symptoms (PHQ-2, 0 to 6), anxiety symptoms (GAD-2, 0 to 6), life satisfaction
#     (1 to 10) and age, from blue (negative) through white (zero) to red
#     (positive). The caption describes and makes no causal claim.
nice <- c(loneliness = "Loneliness", support = "Social support",
          depression = "Depression", anxiety = "Anxiety",
          life_sat = "Life satisfaction", age = "Age")
levels(cor_long$var1) <- nice[levels(cor_long$var1)]
levels(cor_long$var2) <- nice[levels(cor_long$var2)]
print(levels(cor_long$var1))
p_heat <- ggplot(cor_long, aes(x = var1, y = var2, fill = r)) +
  geom_tile(colour = "white") +
  geom_text(aes(label = r), size = 3) +
  scale_fill_gradient2(low = "#2166AC", mid = "white", high = "#B2182B",
                       limits = c(-1, 1)) +
  labs(x = NULL, y = NULL, fill = "Pearson r")
figure_final <- ((p_hist + p_box) / (p_scatter + p_heat) +
  plot_annotation(tag_levels = "A")) & theme_minimal()
figure_final
