# Scale ID: 			GAD-7
# Scale Name: 		Generalised Anxiety Disorder 7 Item
# TILDA Variables: 	GAD7
# Dataset:      		TILDA Waves 6
# Author:
# Institution:  		The Irish Longitudinal Study on Ageing (TILDA)
# 
# Description:
# Cleans and codes up summed score measuring Generalised Anxiety Disorder.
# 
# Version:      1.0
# Date:         2026-09-01
# Language:     R

# Assumption: the working data frame is called `data`.

# Recode the special missing values used in the source.
# The Stata source applies mvdecode to Anxiety*; the score itself uses SCQAnxiety1-7.
gad_items <- paste0("SCQAnxiety", 1:7)

for (v in gad_items) {
  data[[v]][data[[v]] %in% c(-99, -812, -823, -834)] <- NA_real_
}

data$GAD7 <- NA_real_
data$GAD7[!is.na(data$in_scq) & data$in_scq == 1] <- 0

for (v in gad_items) {
  data$GAD7 <- data$GAD7 + (data[[v]] - 1)
  data$GAD7[data[[v]] == 99 |
            data[[v]] == 98 |
            (!is.na(data[[v]]) & data[[v]] < 0)] <- NA_real_
}

attr(data$GAD7, "label") <- "Generalised Anxiety Scale (GAD7)"
attr(data$GAD7, "note") <- paste(
  "Anxiety-GAD-7 Ordinal. Questions are all 3 2 1 0, ie coded score minus 1,",
  "higher score more anxiety"
)
