# ========================================================
# SENSITIVITY ANALYSES IN R
# Canadian Social Connection Survey (CSCS), 2021 wave
# ========================================================
# Question: among adults in Canada, is living with a dog
# associated with lower loneliness?
# The analysis plan was written first (see the walkthrough
# From a Research Question to an Analysis Plan). This script
# fits the primary model, then repeats it with one decision
# changed at a time and collects every estimate in a table.
# ========================================================


# ========================================================
# SECTION 1: PACKAGES AND DATA
# ========================================================

# install.packages(c("sandwich", "lmtest", "EValue", "mice"))
library(sandwich)   # vcovHC(): standard errors for unequal spread
library(lmtest)     # coeftest() and coefci()
library(MASS)       # polr(): ordinal logistic regression
library(splines)    # ns(): natural splines
library(EValue)     # evalues.OLS(): E-values
library(mice)       # mice(): multiple imputation

github_url <- "https://raw.githubusercontent.com/jorgeandr3s/heal/main/cscs/public_data/CSCS2025_full_cleaned_deidentified_data_and_metadata.RData"
load(url(github_url))
data <- data[data$SURVEY_collection_year == 2021, ]
dim(data)


# ========================================================
# SECTION 2: PREPARE THE VARIABLES, AS PLANNED
# ========================================================

study <- data.frame(
  dogs       = data$GEO_housing_live_with_dogs,
  loneliness = data$LONELY_ucla_loneliness_scale_score,
  age        = data$DEMO_age,
  household  = data$GEO_housing_household_size,
  children   = data$GEO_housing_live_with_children)

# Exposure: lives with at least one dog (1) or none (0)
study$dog <- NA
study$dog[study$dogs == 0] <- 0
study$dog[study$dogs >= 1] <- 1

# Confounders from the causal diagram
study$alone <- NA                         # lives alone
study$alone[study$household == 0] <- 1
study$alone[study$household >= 1] <- 0
study$kids <- NA                          # lives with children
study$kids[study$children == 0] <- 0
study$kids[study$children >= 1] <- 1
study$gender <- NA                        # a non-answer stays missing
study$gender[data$DEMO_gender == "Man"] <- "Man"
study$gender[data$DEMO_gender == "Woman"] <- "Woman"
study$gender[data$DEMO_gender == "Non-binary"] <- "Non-binary"
study$disability <- NA                    # chronic condition or disability
study$disability[data$DEMO_identity_disability == "Not selected"] <- 0
study$disability[data$DEMO_identity_disability ==
  "People with chronic health problems or disabilities"] <- 1
income_level <- as.numeric(data$DEMO_household_income)  # 1 to 27
study$income <- NA
study$income[income_level <= 8] <- "1: under $40k"
study$income[income_level >= 9  & income_level <= 13] <- "2: $40k to $79k"
study$income[income_level >= 14 & income_level <= 17] <- "3: $80k to $119k"
study$income[income_level >= 18 & income_level <= 26] <- "4: $120k or more"


# ========================================================
# SECTION 3: THE PRIMARY ANALYSIS
# ========================================================

primary <- lm(loneliness ~ dog + age + gender + income +
                alone + kids + disability, data = study)
nobs(primary)                                # people in the model
coeftest(primary, vcov. = vcovHC(primary))   # HC3 standard errors
coefci(primary, parm = "dog", vcov. = vcovHC(primary))

# --- 3.1 A helper that adds one estimate to a results table
add_result <- function(table, label, model, term = "dog") {
  ci <- coefci(model, parm = term, vcov. = vcovHC(model))
  new_row <- data.frame(analysis = label,
                        estimate = round(coef(model)[[term]], 3),
                        lower    = round(ci[1], 3),
                        upper    = round(ci[2], 3),
                        n        = nobs(model))
  print(new_row, row.names = FALSE)   # show the new row
  rbind(table, new_row)
}
results <- add_result(NULL, "Primary", primary)
results


# ========================================================
# SECTION 4: CODING DECISIONS
# ========================================================

# --- 4.1 Blank dog counts treated as zero
table(blank = is.na(study$dogs))
study_b0 <- study
study_b0$dog[is.na(study_b0$dogs)] <- 0
m_blank0 <- update(primary, data = study_b0)
results <- add_result(results, "Blank dog counts as 0", m_blank0)

# --- 4.2 Implausible answers excluded
implausible <- (study$dogs >= 5 & !is.na(study$dogs)) |
  (study$household >= 15 & !is.na(study$household))
sum(implausible)
m_plausible <- update(primary, data = study[!implausible, ])
results <- add_result(results, "Implausible answers excluded",
                      m_plausible)

# --- 4.3 Number of dogs in place of any dog
study$dog_count <- NA
study$dog_count[study$dogs == 0] <- "0"
study$dog_count[study$dogs == 1] <- "1"
study$dog_count[study$dogs >= 2] <- "2+"
table(study$dog_count)
m_count <- update(primary, . ~ . - dog + dog_count)
coeftest(m_count, vcov. = vcovHC(m_count))[c("dog_count1", "dog_count2+"), ]

# --- 4.4 Loneliness as lonely (6 or more) or not
study$lonely <- NA
study$lonely[study$loneliness <= 5] <- 0
study$lonely[study$loneliness >= 6] <- 1
mean(study$lonely, na.rm = TRUE)
m_binary <- glm(lonely ~ dog + age + gender + income +
                  alone + kids + disability,
                data = study, family = binomial)
exp(coef(m_binary)["dog"])                 # odds ratio
exp(confint.default(m_binary, "dog"))      # 95% CI

# --- 4.5 Income non-response as its own category
study_inc <- study
study_inc$income[income_level == 27] <- "5: no response"
m_income <- update(primary, data = study_inc)
results <- add_result(results, "Income non-response as a category",
                      m_income)


# ========================================================
# SECTION 5: MODEL CHOICE
# ========================================================

# --- 5.1 Ordinal logistic regression: only the order counts
study$loneliness_ord <- factor(study$loneliness, ordered = TRUE)
m_ordinal <- polr(loneliness_ord ~ dog + age + gender + income +
                    alone + kids + disability,
                  data = study, Hess = TRUE)
exp(coef(m_ordinal)["dog"])                # odds ratio
exp(confint.default(m_ordinal, "dog"))     # 95% CI

# --- 5.2 Age as a smooth curve (natural spline, 4 df)
m_spline <- update(primary, . ~ . - age + ns(age, df = 4))
results <- add_result(results, "Age as a natural spline", m_spline)

# --- 5.3 Inverse probability weighting
model_vars <- all.vars(formula(primary))
cc <- study[complete.cases(study[, model_vars]), ]
nrow(cc)
ps_model <- glm(dog ~ age + gender + income + alone + kids +
                  disability, data = cc, family = binomial)
cc$ps <- fitted(ps_model)                  # propensity score
cc$weight <- NA
cc$weight[cc$dog == 1] <- 1 / cc$ps[cc$dog == 1]
cc$weight[cc$dog == 0] <- 1 / (1 - cc$ps[cc$dog == 0])
summary(cc$weight)
hist(cc$weight, breaks = 40, col = "grey85",
     main = "Inverse probability weights", xlab = "Weight")

# Balance: confounder means by group, before and after weighting
check_balance <- function(x) {
  c(no_dog = mean(x[cc$dog == 0]),
    dog = mean(x[cc$dog == 1]),
    no_dog_weighted = weighted.mean(x[cc$dog == 0], cc$weight[cc$dog == 0]),
    dog_weighted = weighted.mean(x[cc$dog == 1], cc$weight[cc$dog == 1]))
}
round(rbind(age = check_balance(cc$age),
            alone = check_balance(cc$alone),
            kids = check_balance(cc$kids)), 3)

m_ipw <- lm(loneliness ~ dog, data = cc, weights = weight)
results <- add_result(results, "Inverse probability weighting",
                      m_ipw)


# ========================================================
# SECTION 6: THE ADJUSTMENT SET
# ========================================================

# --- 6.1 No adjustment, for reference only
m_unadjusted <- lm(loneliness ~ dog, data = cc)
coef(m_unadjusted)["dog"]

# --- 6.2 An extended set: relationship, education, province
study$relationship <- as.character(data$DEMO_relationship_status)
study$relationship[study$relationship ==
                     "Presented but no response"] <- NA
edu_level <- as.numeric(data$DEMO_education)   # 1 to 9 in order
study$education <- NA
study$education[edu_level == 1] <- "1: high school or less"
study$education[edu_level >= 2 & edu_level <= 4] <- "2: college or trades"
study$education[edu_level >= 5] <- "3: university degree"
study$province <- as.character(data$GEO_province)
study$province[study$province == "Presented but no response"] <- NA

ext_vars <- c(model_vars, "relationship", "education", "province")
ext_sample <- study[complete.cases(study[, ext_vars]), ]
m_same <- update(primary, data = ext_sample)
m_extended <- update(m_same, . ~ . + relationship + education +
                       province)
coef(m_same)["dog"]
results <- add_result(results, "Extended adjustment set",
                      m_extended)

# --- 6.3 Which added variable moves the estimate?
coef(update(m_same, . ~ . + relationship))["dog"]
coef(update(m_same, . ~ . + education))["dog"]
coef(update(m_same, . ~ . + province))["dog"]
round(tapply(ext_sample$dog, ext_sample$province, mean), 2)

# --- 6.4 What not to do: adjust for a consequence of loneliness
study$depression <- data$WELLNESS_phq_score
dep_sample <- study[!is.na(study$depression), ]
coef(update(primary, data = dep_sample))["dog"]
coef(update(primary, . ~ . + depression, data = dep_sample))["dog"]


# ========================================================
# SECTION 7: UNMEASURED CONFOUNDING
# ========================================================

# --- 7.1 The E-value for the primary estimate
se_dog <- sqrt(vcovHC(primary)["dog", "dog"])
evalues.OLS(est = coef(primary)[["dog"]], se = se_dog,
            sd = sd(cc$loneliness))

# --- 7.2 A negative control exposure: living with fish
study$fish <- NA
study$fish[data$GEO_housing_live_with_fish == 0] <- 0
study$fish[data$GEO_housing_live_with_fish >= 1] <- 1
table(fish = study$fish, dog = study$dog)
m_fish <- update(primary, . ~ . + fish)
coeftest(m_fish, vcov. = vcovHC(m_fish))[c("dog", "fish"), ]

# --- 7.3 Personality, measured for part of the sample
study$extraversion      <- data$PSYCH_ten_item_personality_inventory_extraversion_score
study$stability         <- data$PSYCH_ten_item_personality_inventory_emotional_stability_score
study$agreeableness     <- data$PSYCH_ten_item_personality_inventory_agreeableness_score
study$conscientiousness <- data$PSYCH_ten_item_personality_inventory_conscientiousness_score
study$openness          <- data$PSYCH_ten_item_personality_inventory_openness_score
pers_vars <- c("extraversion", "stability", "agreeableness",
               "conscientiousness", "openness")
pers_sample <- study[complete.cases(study[, c(model_vars, pers_vars)]), ]
nrow(pers_sample)
m_pers <- update(primary, data = pers_sample)
m_pers_adj <- update(m_pers, . ~ . + extraversion + stability +
                       agreeableness + conscientiousness + openness)
results <- add_result(results, "Personality subsample", m_pers)
results <- add_result(results, "Personality subsample, adjusted",
                      m_pers_adj)


# ========================================================
# SECTION 8: MISSING DATA
# ========================================================

colSums(is.na(study[, model_vars]))
sum(complete.cases(study[, model_vars]))

mi_data <- study[, model_vars]
mi_data$gender <- factor(mi_data$gender)
mi_data$income <- factor(mi_data$income)
imp <- mice(mi_data, m = 40, maxit = 10, seed = 2021,
            printFlag = FALSE)
fits <- with(imp, lm(loneliness ~ dog + age + gender + income +
                       alone + kids + disability))
pooled <- summary(pool(fits), conf.int = TRUE)
dog_row <- pooled[pooled$term == "dog", ]
dog_row[, c("estimate", "2.5 %", "97.5 %")]
results <- rbind(results, data.frame(
  analysis = "Multiple imputation (40 datasets)",
  estimate = round(dog_row$estimate, 3),
  lower    = round(dog_row[["2.5 %"]], 3),
  upper    = round(dog_row[["97.5 %"]], 3),
  n        = nrow(mi_data)))


# ========================================================
# SECTION 9: SAMPLES AND SUBGROUPS
# ========================================================

# --- 9.1 People who live with others, and people who live alone
m_others <- update(primary, . ~ . - alone,
                   data = subset(study, alone == 0))
m_alone <- update(primary, . ~ . - alone,
                  data = subset(study, alone == 1))
subgroups <- add_result(NULL, "Lives with others", m_others)
subgroups <- add_result(subgroups, "Lives alone", m_alone)
subgroups

# --- 9.2 An interaction term tests whether they differ
m_int <- update(primary, . ~ . + dog:alone)
coeftest(m_int, vcov. = vcovHC(m_int))[c("dog", "dog:alone"), ]

# --- 9.3 Men and women
m_men <- update(primary, . ~ . - gender,
                data = subset(study, gender == "Man"))
m_women <- update(primary, . ~ . - gender,
                  data = subset(study, gender == "Woman"))
subgroups <- add_result(subgroups, "Men", m_men)
subgroups <- add_result(subgroups, "Women", m_women)
subgroups
m_int_g <- update(primary, . ~ . + dog:gender)
coeftest(m_int_g, vcov. = vcovHC(m_int_g))["dog:genderWoman", ]

# --- 9.4 The cross-sectional sample without cohort members
study$cohort <- data$SURVEY_cohort_participant
table(cohort = study$cohort, dog = study$dog)
m_no_cohort <- update(primary, data = subset(study, cohort == FALSE))
results <- add_result(results, "Without cohort members",
                      m_no_cohort)


# ========================================================
# SECTION 10: MEDIATION (EXPLORATORY)
# ========================================================

# Neighbours known by name: 1 = none, 2 = 1-2, 3 = 3-4, 4 = 5+
study$neighbours <- data$CONNECTION_neighbours_name_num_num
med_sample <- study[complete.cases(study[, c(model_vars, "neighbours")]), ]
m_a <- lm(neighbours ~ dog + age + gender + income + alone +
            kids + disability, data = med_sample)
m_total <- update(primary, data = med_sample)
m_direct <- update(m_total, . ~ . + neighbours)
coef(m_a)["dog"]                          # path a
coef(m_direct)[c("neighbours", "dog")]    # path b, direct effect
coef(m_total)["dog"]                      # total effect
indirect <- coef(m_a)[["dog"]] * coef(m_direct)[["neighbours"]]
indirect

# Bootstrap confidence interval for the indirect effect
set.seed(2021)
boot_indirect <- numeric(1000)
for (i in 1:1000) {
  rows <- sample(nrow(med_sample), replace = TRUE)
  boot_data <- med_sample[rows, ]
  a_path <- coef(update(m_a, data = boot_data))[["dog"]]
  b_path <- coef(update(m_direct, data = boot_data))[["neighbours"]]
  boot_indirect[i] <- a_path * b_path
}
quantile(boot_indirect, c(0.025, 0.975))


# ========================================================
# SECTION 11: OTHER MEASURES OF LONELINESS
# ========================================================

standardize <- function(x) (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)

# --- 11.1 Three measures of the same construct
study$ucla_z <- standardize(study$loneliness)
study$dejong_z <- standardize(data$LONELY_dejong_emotional_social_loneliness_scale_score)
past_week <- as.numeric(data$LONELY_direct)   # 1 to 5; 6 = no response
past_week[past_week == 6] <- NA
study$week_z <- standardize(past_week)
measures <- add_result(NULL, "UCLA scale (primary)",
                       update(primary, ucla_z ~ .))
measures <- add_result(measures, "De Jong Gierveld scale",
                       update(primary, dejong_z ~ .))
measures <- add_result(measures, "Lonely in the past week",
                       update(primary, week_z ~ .))

# --- 11.2 The two De Jong Gierveld subscales
study$emotional_z <- standardize(data$LONELY_dejong_emotional_loneliness_sub_scale_score)
study$social_z <- standardize(data$LONELY_dejong_social_loneliness_sub_scale_score)
measures <- add_result(measures, "Emotional loneliness",
                       update(primary, emotional_z ~ .))
measures <- add_result(measures, "Social loneliness",
                       update(primary, social_z ~ .))

# --- 11.3 Related constructs: social isolation and support
study$isolation_z <- standardize(data$LONELY_steptoe_isolation_index_score)
study$support_z <- standardize(data$PSYCH_zimet_multidimensional_social_support_scale_score)
measures <- add_result(measures, "Social isolation",
                       update(primary, isolation_z ~ .))
measures <- add_result(measures, "Social support",
                       update(primary, support_z ~ .))
measures


# ========================================================
# SECTION 12: BRINGING IT TOGETHER
# ========================================================

results

# --- 12.1 A forest plot of every estimate on the UCLA scale
k <- nrow(results)
par(mar = c(4.5, 15, 1, 1))
plot(results$estimate, k:1, xlim = c(-0.4, 0.1), pch = 16,
     yaxt = "n", ylab = "",
     xlab = "Difference in loneliness score")
segments(results$lower, k:1, results$upper, k:1)
abline(v = 0, lty = 2)
abline(v = results$estimate[1], col = "grey60")
axis(2, at = k:1, labels = results$analysis, las = 1,
     cex.axis = 0.75)
par(mar = c(5.1, 4.1, 4.1, 2.1))

# --- 12.2 A small specification curve: 16 combinations
specs <- expand.grid(blank_as_0 = c(FALSE, TRUE),
                     drop_implausible = c(FALSE, TRUE),
                     extended_set = c(FALSE, TRUE),
                     age_spline = c(FALSE, TRUE))
specs$estimate <- NA
specs$lower <- NA
specs$upper <- NA
for (i in 1:nrow(specs)) {
  s <- study
  if (specs$blank_as_0[i]) s$dog[is.na(s$dogs)] <- 0
  if (specs$drop_implausible[i]) s <- s[!implausible, ]
  f <- loneliness ~ dog + age + gender + income + alone + kids +
    disability
  if (specs$extended_set[i]) f <- update(f, . ~ . + relationship +
                                           education + province)
  if (specs$age_spline[i]) f <- update(f, . ~ . - age + ns(age, df = 4))
  m <- lm(f, data = s)
  ci <- coefci(m, parm = "dog", vcov. = vcovHC(m))
  specs$estimate[i] <- coef(m)[["dog"]]
  specs$lower[i] <- ci[1]
  specs$upper[i] <- ci[2]
}
specs <- specs[order(specs$estimate), ]
summary(specs$estimate)
sum(specs$upper < 0)                       # intervals below zero
plot(1:16, specs$estimate, ylim = c(-0.4, 0.1), pch = 16,
     col = ifelse(specs$extended_set, "firebrick", "black"),
     xlab = "Specification, sorted by estimate",
     ylab = "Difference in loneliness score")
segments(1:16, specs$lower, 1:16, specs$upper,
         col = ifelse(specs$extended_set, "firebrick", "black"))
abline(h = 0, lty = 2)
legend("topleft", c("Minimal set", "Extended set"), pch = 16,
       col = c("black", "firebrick"), bty = "n")

# --- 12.3 Save the table for a supplement
write.csv(results, "sensitivity_results.csv", row.names = FALSE)
