# /******************************************************************
# Scale ID:           SocialCohesion
# Scale Name:         Neighbourhood Social Cohesion Scale
# TILDA Variables:    Neighb_unsatisfied
# Dataset:            TILDA Wave 2,4,6r,7
# Author:
# Institution:        The Irish Longitudinal Study on Ageing (TILDA)
#
# Description:
# Generates satisfaction scoring for the Neighbourhood Social Cohesion scale.
#
# Version:      1.0
# Date:         2026-09-01
# Language:     R
# ******************************************************************/
#
# Assumption: the working data frame is called `data`.


# ---------------------------------------------------------------
# Recode source response/error values exactly as in the Stata file
# ---------------------------------------------------------------

ngh_items <- paste0("SCQNghBH", 1:9)

for (v in ngh_items) {
  x <- data[[v]]
  x[!is.na(x) & x == -99]  <- NA_real_
  x[!is.na(x) & x == -812] <- 2
  x[!is.na(x) & x == -823] <- 3
  x[!is.na(x) & x == -834] <- 4
  x[!is.na(x) & x == -845] <- 5
  x[!is.na(x) & x == -856] <- 6
  x[!is.na(x) & x == -867] <- 7
  data[[v]] <- x
}

# Item 5: value 8 is missing
data$SCQNghBH5[!is.na(data$SCQNghBH5) & data$SCQNghBH5 == 8] <- NA_real_


# ---------------------------------------------------------------
# Reverse negatively worded items
# Reverse formula: 8 - original response
# ---------------------------------------------------------------

data$SCQNghBH2rev <- 8 - data$SCQNghBH2
data$SCQNghBH3rev <- 8 - data$SCQNghBH3
data$SCQNghBH5rev <- 8 - data$SCQNghBH5
data$SCQNghBH7rev <- 8 - data$SCQNghBH7

attr(data$SCQNghBH2rev, "label") <-
  "There is no problem with vandalism and graffiti in this area"
attr(data$SCQNghBH3rev, "label") <-
  "I have never felt lonely living in this area"
attr(data$SCQNghBH5rev, "label") <-
  "People feel safe walking alone after dark in this area"
attr(data$SCQNghBH7rev, "label") <-
  "People in this area will always treat you fairly"


# ---------------------------------------------------------------
# Dichotomise neighbourhood items
# 0 = Agree
# 1 = Disagree
# ---------------------------------------------------------------

cohesion_items <- c(
  "SCQNghBH1",
  "SCQNghBH2rev",
  "SCQNghBH3rev",
  "SCQNghBH4",
  "SCQNghBH5rev",
  "SCQNghBH6",
  "SCQNghBH7rev",
  "SCQNghBH8",
  "SCQNghBH9"
)

for (v in cohesion_items) {
  x <- data[[v]]
  out <- rep(NA_real_, length(x))
  out[!is.na(x) & x >= 0 & x <= 3] <- 0
  out[!is.na(x) & x >= 4] <- 1
  data[[v]] <- out
  attr(data[[v]], "labels") <- c("Agree" = 0, "Disagree" = 1)
}


# ---------------------------------------------------------------
# Total scale
# Higher scores indicate greater neighbourhood dissatisfaction
# Ordinary addition preserves the Stata complete-case behaviour.
# ---------------------------------------------------------------

data$Neighb_unsatisfied <-
  data$SCQNghBH1 +
  data$SCQNghBH2rev +
  data$SCQNghBH3rev +
  data$SCQNghBH4 +
  data$SCQNghBH5rev +
  data$SCQNghBH6 +
  data$SCQNghBH7rev +
  data$SCQNghBH8 +
  data$SCQNghBH9

attr(data$Neighb_unsatisfied, "label") <-
  "Neighbourhood social cohesion dissatisfaction score"
