# *****************************************************************
# Scale ID:         CESD-8
# Scale Name:       Centre for Epidemiological Studies Depression
#                   Scale (8-item)
# TILDA Variable:   MHcesd_capi_sf
# Dataset:          TILDA All Waves
# Author:
# Institution:      The Irish Longitudinal Study on Ageing (TILDA)
#
# Description:
# Creates the CES-D 8-item depression score from CAPI responses.
# Items are scored 0-3 and summed to produce a total score ranging
# from 0-24. Positive affect items are reverse scored.
#
# Citation:
# Briggs, R., Carey, D., O'Halloran, A.M., Kenny, R.A. and Kennelly,
# S.P. (2018). Validation of the 8-item CES-D scale in TILDA.
#
# Version:          1.0
# Date:             2026-06-01
# Language:         R
#
# *****************************************************************

posq <- c("mh006", "mh007", "mh011", "mh014", "mh018", "mh020")
negq <- c("mh012", "mh016")

df[ , c(posq, negq)] <- lapply(df[ , c(posq, negq)], function(x) {
  x[x %in% c(98, 99, -1)] <- NA
  x
})

df$MHcesd_capi_sf <-
  rowSums(df[ , posq] - 1, na.rm = FALSE) +
  rowSums(4 - df[ , negq], na.rm = FALSE)