# =============================================================================
# HSCI 488 Lesson 8: AI for Data Analysis and Coding in R -- LESSON SCRIPT
# Author: Kiffer G. Card
# -----------------------------------------------------------------------------
# Page:  modules/HSCI_488_Lesson_8_AI_for_Data_Analysis_and_Coding_in_R.html
# Data:  Canadian Social Connection Survey (CSCS), 2021 wave, public
#        de-identified file loaded from the course GitHub repository (an
#        internet connection is needed).
#
# This script reproduces every code block on the lesson page, in page order.
# The lesson shows code that an AI assistant proposed, the checks applied to
# it, and the corrected code. Blocks marked "AS PROPOSED" contain the errors
# discussed on the page and are kept here so that you can see the errors for
# yourself. One block stops with an error message; it is wrapped in try() so
# that the rest of the script still runs from top to bottom.
#
# Packages: dplyr, readr and ggplot2. Install them once with
#   install.packages(c("dplyr", "readr", "ggplot2"))
# Files written to the working directory:
#   cscs2021_loneliness_clean.csv, ucla_distribution_by_age.png,
#   screen_positive_by_age_group.png
# =============================================================================

options(width = 70)
library(dplyr)     # verbs for data frames: filter, select, mutate, summarise
library(readr)     # write_csv() and read_csv()
library(ggplot2)   # plots


# =============================================================================
# SECTION 2, STEP 1: load the survey and keep the 2021 wave
# =============================================================================
github_url <- "https://raw.githubusercontent.com/jorgeandr3s/heal/main/cscs/public_data/CSCS2025_full_cleaned_deidentified_data_and_metadata.RData"
load(url(github_url))          # creates a data frame called data

cscs <- data |>
  filter(SURVEY_collection_year == 2021)   # keep the 2021 wave only

nrow(cscs)    # rows: one per respondent
ncol(cscs)    # columns: one per variable


# =============================================================================
# SECTION 2, STEP 2: code proposed BEFORE the assistant saw the data dictionary
# The assistant guessed the column names. R stops with an error message.
# (try() lets the script continue; at the console you would run the inner line.)
# =============================================================================
try(
  lonely <- cscs |>
    select(age, gender, province, ucla_score)
)


# =============================================================================
# SECTION 2, STEP 3: the same request, with the data dictionary in the prompt
# =============================================================================
lonely <- cscs |>
  select(age           = DEMO_age,
         gender        = DEMO_gender,
         province      = GEO_province,
         companionship = LONELY_ucla_loneliness_scale_companionship,
         left_out      = LONELY_ucla_loneliness_scale_left_out,
         isolated      = LONELY_ucla_loneliness_scale_isolated,
         ucla_survey   = LONELY_ucla_loneliness_scale_score)

glimpse(lonely)   # one line per column: name, type, first values


# =============================================================================
# SECTION 2, STEP 4: find the missing values, then recode the survey's
# "Presented but no response" code to NA in gender and province
# =============================================================================
colSums(is.na(lonely))                  # NA count in each column
table(lonely$gender, useNA = "ifany")   # every answer, NA included

lonely <- lonely |>
  mutate(gender   = na_if(as.character(gender),
                          "Presented but no response"),
         province = na_if(as.character(province),
                          "Presented but no response"))

table(lonely$gender, useNA = "ifany")   # the 4 codes are now NA
sum(is.na(lonely$province))             # 50 codes became NA
summary(lonely$age)                     # plausible range for adults?


# =============================================================================
# SECTION 3, CHECK 1: scoring the three UCLA items
# AS PROPOSED (contains an error): as.numeric() on a factor
# =============================================================================
lonely <- lonely |>
  mutate(ucla_score = as.numeric(companionship) +
                      as.numeric(left_out) +
                      as.numeric(isolated))

table(lonely$ucla_score, useNA = "ifany")   # every value, NA included

# Second method: compare with the score the survey team computed
lonely |>
  count(ours_missing   = is.na(ucla_score),
        survey_missing = is.na(ucla_survey))

# Look at the rows where the two disagree
lonely |>
  filter(!is.na(ucla_score), is.na(ucla_survey)) |>
  select(companionship, left_out, isolated, ucla_score) |>
  head(3)

levels(lonely$companionship)   # the order of the levels decides the numbers

# CORRECTED: score each answer explicitly; anything else becomes NA
score_item <- function(x) {
  x <- as.character(x)
  case_when(x == "Hardly Ever"      ~ 1,
            x == "Some of the time" ~ 2,
            x == "Often"            ~ 3,
            TRUE                    ~ NA_real_)
}

lonely <- lonely |>
  mutate(ucla_score = score_item(companionship) +
                      score_item(left_out) +
                      score_item(isolated))

table(lonely$ucla_score, useNA = "ifany")
all.equal(lonely$ucla_score, lonely$ucla_survey)   # second method agrees?

# Known-answer test: four invented people whose scores we can work out by hand
test <- data.frame(
  companionship = c("Hardly Ever", "Often", "Some of the time", "Often"),
  left_out      = c("Hardly Ever", "Often", "Some of the time",
                    "Presented but no response"),
  isolated      = c("Hardly Ever", "Often", "Often", "Often"))

test |>
  mutate(score = score_item(companionship) + score_item(left_out) +
                 score_item(isolated))
# Expected by hand: 3, 9, 7 and NA


# =============================================================================
# SECTION 3, CHECK 2: age groups
# AS PROPOSED (contains an error): cut() with its default right = TRUE
# =============================================================================
age_labels <- c("16 to 29", "30 to 44", "45 to 64", "65 and older")

lonely <- lonely |>
  mutate(age_group = cut(age, breaks = c(16, 30, 45, 65, Inf),
                         labels = age_labels))

table(lonely$age_group, useNA = "ifany")

lonely |>
  group_by(age_group) |>
  summarise(n = n(), youngest = min(age), oldest = max(age))

# CORRECTED: right = FALSE makes each group include its lower boundary
lonely <- lonely |>
  mutate(age_group = cut(age, breaks = c(16, 30, 45, 65, Inf),
                         labels = age_labels, right = FALSE))

lonely |>
  group_by(age_group) |>
  summarise(n = n(), youngest = min(age), oldest = max(age))


# =============================================================================
# SECTION 3: the cleaned file, with a row-count check after saving
# =============================================================================
clean <- lonely |>
  mutate(screen_positive = ucla_score >= 6) |>   # Connector threshold
  select(age, age_group, gender, province, ucla_score, screen_positive)

write_csv(clean, "cscs2021_loneliness_clean.csv")
nrow(read_csv("cscs2021_loneliness_clean.csv", show_col_types = FALSE))


# =============================================================================
# SECTION 3: the descriptive table, and a second method for its last column
# =============================================================================
table1 <- clean |>
  filter(!is.na(ucla_score)) |>
  group_by(age_group) |>
  summarise(n          = n(),
            mean_score = round(mean(ucla_score), 2),
            sd_score   = round(sd(ucla_score), 2),
            pct_6_plus = round(100 * mean(screen_positive), 1))
table1
sum(table1$n)   # respondents with a score

# Second method: the survey team's own yes/no version of the same cut-off
check <- table(clean$age_group, cscs$LONELY_ucla_loneliness_scale_score_y_n)
round(100 * prop.table(check, margin = 1), 1)


# =============================================================================
# SECTION 3: two plots (saved as PNG files)
# =============================================================================
# Plot 1: the distribution of scores, under 65 compared with 65 and older
dist <- clean |>
  filter(!is.na(ucla_score)) |>
  mutate(group = if_else(age >= 65, "65 and older", "Under 65")) |>
  count(group, ucla_score) |>
  group_by(group) |>
  mutate(pct = round(100 * n / sum(n), 1)) |>
  ungroup()
print(dist, n = 14)

p1 <- ggplot(dist, aes(x = factor(ucla_score), y = pct, fill = group)) +
  geom_col(position = "dodge") +
  labs(x = "UCLA Loneliness Scale score (3 to 9)",
       y = "Percent of age group", fill = NULL,
       title = "Loneliness scores, CSCS 2021") +
  theme_minimal()
ggsave("ucla_distribution_by_age.png", p1, width = 7, height = 4, dpi = 150)

# Plot 2: percent scoring 6 or higher in each age group
p2 <- ggplot(table1, aes(x = age_group, y = pct_6_plus)) +
  geom_col(fill = "#0B7B6B") +
  geom_text(aes(label = paste0("n = ", n)), vjust = -0.4) +
  labs(x = "Age group", y = "Percent scoring 6 or higher",
       title = "Screen-positive for loneliness, CSCS 2021") +
  theme_minimal()
ggsave("screen_positive_by_age_group.png", p2, width = 7, height = 4, dpi = 150)


# =============================================================================
# SECTION 4: a synthetic stand-in that can be shared with an AI assistant
# Invented values with the real column names and codes; no real record.
# =============================================================================
set.seed(488)
fake <- data.frame(
  age           = sample(16:100, 6, replace = TRUE),
  gender        = sample(c("Man", "Woman", "Non-binary",
                           "Presented but no response"), 6, replace = TRUE),
  companionship = sample(c("Hardly Ever", "Some of the time", "Often",
                           "Presented but no response"), 6, replace = TRUE))
fake
