# HSCI 410 Lesson 7, Activity 7.1: prepare and score the six loneliness items
# 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 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))   # survey data, read from the web: a data frame called data
dim(data)               # rows (survey responses) and columns
data <- data[data$SURVEY_collection_year == 2021, ]   # one row per person
dim(data)

# 2. The six items, with short names
dj <- data.frame(
  emptiness = data$LONELY_dejong_emotional_social_loneliness_scale_emptiness,
  miss      = data$LONELY_dejong_emotional_social_loneliness_scale_miss,
  rejected  = data$LONELY_dejong_emotional_social_loneliness_scale_rejected,
  rely      = data$LONELY_dejong_emotional_social_loneliness_scale_rely,
  trust     = data$LONELY_dejong_emotional_social_loneliness_scale_trust,
  close     = data$LONELY_dejong_emotional_social_loneliness_scale_close)
table(dj$emptiness, useNA = "ifany")   # the answers are stored as text
table(dj$rely, useNA = "ifany")

# 3. Text to numbers: No = 1, More or less = 2, Yes = 3
lv <- c("No", "More or less", "Yes")        # match() gives each answer's place
dj$emptiness_n <- match(dj$emptiness, lv)   # any other answer becomes NA
dj$miss_n      <- match(dj$miss, lv)
dj$rejected_n  <- match(dj$rejected, lv)
dj$rely_n      <- match(dj$rely, lv)
dj$trust_n     <- match(dj$trust, lv)
dj$close_n     <- match(dj$close, lv)
table(dj$emptiness, dj$emptiness_n, useNA = "ifany")   # check

# 4. Reverse-code the positively worded items: (max + min) - code
dj$rely_r  <- 4 - dj$rely_n
dj$trust_r <- 4 - dj$trust_n
dj$close_r <- 4 - dj$close_n
table(dj$rely_n, dj$rely_r)   # check: 1 -> 3, 2 -> 2, 3 -> 1

# 5. One data frame of the six items, higher = lonelier
items <- data.frame(emptiness = dj$emptiness_n, miss = dj$miss_n,
                    rejected = dj$rejected_n, rely = dj$rely_r,
                    trust = dj$trust_r, close = dj$close_r)
items <- na.omit(items)   # people who answered all six items
nrow(items)

# 6. The published rule: 1 for "More or less" or the lonely answer
dj$emptiness_01 <- as.numeric(dj$emptiness_n >= 2)
dj$miss_01      <- as.numeric(dj$miss_n >= 2)
dj$rejected_01  <- as.numeric(dj$rejected_n >= 2)
dj$rely_01      <- as.numeric(dj$rely_r >= 2)
dj$trust_01     <- as.numeric(dj$trust_r >= 2)
dj$close_01     <- as.numeric(dj$close_r >= 2)

# 7. Two subscales (0 to 3), a total (0 to 6) and a bar chart
dj$emotional_score <- dj$emptiness_01 + dj$miss_01 + dj$rejected_01
dj$social_score    <- dj$rely_01 + dj$trust_01 + dj$close_01
dj$total_score     <- dj$emotional_score + dj$social_score
table(dj$total_score, useNA = "ifany")
barplot(table(dj$total_score), col = "grey80",
        xlab = "Total score (0 = not lonely, 6 = most lonely)",
        ylab = "Number of participants")

# 8. Check the total against the score supplied with the data
supplied <- data$LONELY_dejong_emotional_social_loneliness_scale_score
table(dj$total_score == supplied, useNA = "ifany")
