# Scale ID: 			CIDI-Anxiety
# Scale Name: 		Composite International Diagnostic Interview – Short Form (Anxiety)
# TILDA Variables: 	MHcidi_anxiety
# Dataset:      		TILDA Waves 2,3,4,5,6r
# Author:
# Institution:  		The Irish Longitudinal Study on Ageing (TILDA)
# 
# Description:
# Cleans and codes up summed score from the Depression subscale of the Composite International Diagnostic Interview.
# 
# Version:      1.0
# Date:         2026-09-01
# Language:     R
#
# Assumption: the working data frame is called `data`.

# Boolean scoring follows the Stata conditions, with special-code missing
# overrides applied afterward.

data$gad_1 <- (
  (data$mh301 == 1 | data$mh302 == 1) &
  (
    data$mh304m >= 6 | data$mh304y >= 1 |
    data$mh305m >= 6 | data$mh305y >= 1 |
    data$mh304 == 3 | data$mh305 == 3
  )
)
attr(data$gad_1, "label") <- "period of worry lasting at least 6 months"

data$gad_a <- data$mh306 == 1 & data$mh307 == 1 &
              (data$mh308 == 5 | data$mh310 == 1)
attr(data$gad_a, "label") <- "severe worry"

data$gad_b <- data$mh309 == 1 | data$mh311 == 1 | data$mh312 == 1
attr(data$gad_b, "label") <- "difficult to control worry"

gad_c_items <- paste0("mh", 314:320)
data$gad_c <- rowSums(
  sapply(gad_c_items, function(v) data[[v]] == 1),
  na.rm = TRUE
)
attr(data$gad_c, "label") <- "number of additional anxiety symptoms"

data$gad_diagnosis <- as.numeric(
  data$gad_1 & data$gad_a & data$gad_b & data$gad_c >= 3
)

# Missing rules from the Stata source.
c1 <- (data$mh301 == -1) |
      ((is.na(data$mh301) | data$mh301 >= 98) &
       (is.na(data$mh302) | data$mh302 != 1)) |
      (is.na(data$mh302) | data$mh302 >= 98) |
      (is.na(data$mh303) | data$mh303 >= 99) |
      (is.na(data$mh304) | data$mh304 >= 98) |
      (is.na(data$mh305) | data$mh305 >= 98)

c2 <- (is.na(data$mh306) | data$mh306 >= 98) |
      (is.na(data$mh307) | data$mh307 >= 98) |
      ((is.na(data$mh308) | data$mh308 >= 98) &
       (is.na(data$mh310) | data$mh310 >= 98))

c3 <- (is.na(data$mh309) | data$mh309 >= 98) &
      (is.na(data$mh311) | data$mh311 >= 98) &
      (is.na(data$mh312) | data$mh312 >= 98)

data$gad_diagnosis[c1 %in% TRUE | c2 %in% TRUE | c3 %in% TRUE] <- NA_real_

attr(data$gad_diagnosis, "label") <-
  "Fulfills criteria for Generalised Anxiety Disorder in the last 12 months"

data$gad_1 <- NULL
data$gad_a <- NULL
data$gad_b <- NULL
data$gad_c <- NULL

names(data)[names(data) == "gad_diagnosis"] <- "MHcidi_anxiety"

attr(data$MHcidi_anxiety, "labels") <- c("No" = 0, "Yes" = 1)
attr(data$MHcidi_anxiety, "note") <- paste(
  "fulfills DSM criteria for Generalised Anxiety Disorder based on an episode lasting at least 6 months.",
  "This scale is designed to detect clinically significant cases of Generalized Anxiety Disorder;",
  "it is not necessarily sensitive to other types of anxiety disorder.",
  "Proxies or individuals who answer DK/RF to early questions are routed past additional questions",
  "required to fulfill criteria for GAD and are subsequently coded as missing."
)
