# /******************************************************************
# Scale ID:           IPAQ-E
# Scale Name:         IPAQ Environmental Module
# TILDA Variables:    IPAQE_density; IPAQE_access; IPAQE_infrastructure; IPAQE_safety
# Dataset:            TILDA Wave 6
# Author:
# Institution:        The Irish Longitudinal Study on Ageing (TILDA)
#
# Description:
# Generates scoring for the Penn State Worry Questionnaire.
#
# Version:      1.0
# Date:         2026-09-01
# Language:     R
# ******************************************************************/
#
# Assumption: the working data frame is called `data`.


# ---------------------------------------------------------------
# Recode no-response values
# ---------------------------------------------------------------

ipaqe_items <- paste0("SCQIPAQE", 1:7)

for (v in ipaqe_items) {
  data[[v]][!is.na(data[[v]]) & data[[v]] == -99] <- NA_real_
}

# Items 2-7: 5 = Don't know / Not sure
for (v in paste0("SCQIPAQE", 2:7)) {
  data[[v]][!is.na(data[[v]]) & data[[v]] == 5] <- NA_real_
}


# ---------------------------------------------------------------
# Residential density
# Item 1
# ---------------------------------------------------------------

data$IPAQE_density <- NA_real_

data$IPAQE_density[
  !is.na(data$SCQIPAQE1) & data$SCQIPAQE1 == 1
] <- 0

data$IPAQE_density[
  !is.na(data$SCQIPAQE1) &
  data$SCQIPAQE1 >= 2 &
  data$SCQIPAQE1 <= 5
] <- 1

attr(data$IPAQE_density, "label") <-
  "IPAQ-E residential density (0=detached, 1=other housing)"


# ---------------------------------------------------------------
# Access to destinations
# Items 2, 3 and 6
# Mean score range: 1-4
# ---------------------------------------------------------------

access_items <- c("SCQIPAQE2", "SCQIPAQE3", "SCQIPAQE6")

# Calculate row mean, then require all three items to be present
data$IPAQE_access <- rowMeans(data[access_items], na.rm = TRUE)

data$IPAQE_access_missing <- rowSums(is.na(data[access_items]))

data$IPAQE_access[
  data$IPAQE_access_missing > 0
] <- NA_real_

attr(data$IPAQE_access, "label") <-
  "IPAQ-E access to destinations mean score (1-4)"


# ---------------------------------------------------------------
# Neighbourhood infrastructure
# Items 4 and 5
# Mean score range: 1-4
# ---------------------------------------------------------------

infrastructure_items <- c("SCQIPAQE4", "SCQIPAQE5")

# Calculate row mean, then require both items to be present
data$IPAQE_infrastructure <- rowMeans(
  data[infrastructure_items],
  na.rm = TRUE
)

data$IPAQE_infrastructure_missing <- rowSums(
  is.na(data[infrastructure_items])
)

data$IPAQE_infrastructure[
  data$IPAQE_infrastructure_missing > 0
] <- NA_real_

attr(data$IPAQE_infrastructure, "label") <-
  "IPAQ-E neighbourhood infrastructure mean score (1-4)"


# ---------------------------------------------------------------
# Neighbourhood safety
# Item 7 - reverse scored
# ---------------------------------------------------------------

data$IPAQE_safety <- NA_real_

valid_safety <- !is.na(data$SCQIPAQE7) &
                data$SCQIPAQE7 >= 1 &
                data$SCQIPAQE7 <= 4

data$IPAQE_safety[valid_safety] <-
  5 - data$SCQIPAQE7[valid_safety]

attr(data$IPAQE_safety, "label") <-
  "IPAQ-E neighbourhood safety score (1-4)"


# ---------------------------------------------------------------
# Checks
# ---------------------------------------------------------------

summary(data[c(
  "IPAQE_density",
  "IPAQE_access",
  "IPAQE_infrastructure",
  "IPAQE_safety"
)])

table(data$IPAQE_density, useNA = "ifany")
table(data$IPAQE_safety, useNA = "ifany")


# ---------------------------------------------------------------
# Drop temporary variables
# ---------------------------------------------------------------

data$IPAQE_access_missing <- NULL
data$IPAQE_infrastructure_missing <- NULL
