# Scale ID: 			CIDI-Depression
# Scale Name: 		Composite International Diagnostic Interview – Short Form (Depression)
# TILDA Variables: 	MHcidi_depression
# 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`.

count_values_12 <- function(vars) {
  rowSums(sapply(vars, function(v) data[[v]] %in% c(1, 2)), na.rm = TRUE)
}

data$mdd_a_1new <- count_values_12(c("mh101","mh102","mh103"))
data$mdd_a_1new[data$mdd_a_1new %in% c(1,2)] <- 0
data$mdd_a_1new[data$mdd_a_1new == 3] <- 1
attr(data$mdd_a_1new, "label") <- "endorses 1st set of stem questions"

data$mdd_a_2new <- count_values_12(c("mh120","mh121","mh122"))
data$mdd_a_2new[data$mdd_a_2new %in% c(1,2)] <- 0
data$mdd_a_2new[data$mdd_a_2new == 3] <- 1
data$mdd_a_2new[data$mdd_a_2new == 1 & data$mdd_a_1new == 1] <- 0
attr(data$mdd_a_2new, "label") <- "endorses 2nd set of stem questions"

data$mdd_symptom_count_new <- count_values_12(
  c("mh104","mh105","mh108","mh110","mh111","mh112","mh113")
)
data$mdd_symptom_count_new[
  data$mdd_symptom_count_new == 5 &
  !is.na(data$mh103) & data$mh103 == 98
] <- 0
attr(data$mdd_symptom_count_new, "label") <-
  "number of additional symptoms from first set"

# Count values 1 or 2 across mdd_a_2new plus the six source symptom items.
data$mdd_symptom_count_2new <- rowSums(cbind(
  data$mdd_a_2new %in% c(1,2),
  sapply(c("mh123","mh126","mh128","mh129","mh130","mh131"),
         function(v) data[[v]] %in% c(1,2))
), na.rm = TRUE)
attr(data$mdd_symptom_count_2new, "label") <-
  "number of additional symptoms from second set"

data$mdd_diagnosis1 <- as.numeric(
  data$mdd_a_1new != 0 & data$mdd_symptom_count_new >= 3
)
attr(data$mdd_diagnosis1, "label") <-
  "fulfills first set of criteria for diagnosis of MD episode"

data$mdd_diagnosis2 <- as.numeric(
  data$mdd_a_2new != 0 & data$mdd_symptom_count_2new >= 3
)
attr(data$mdd_diagnosis2, "label") <-
  "fulfills second set of criteria for diagnosis of MD episode"

data$MHcidi_depression <- as.numeric(
  data$mdd_diagnosis1 != 0 | data$mdd_diagnosis2 != 0
)

first_bad <-
  is.na(data$mh101) | data$mh101 == -1 | data$mh101 >= 98 |
  is.na(data$mh102) | data$mh102 >= 98 |
  is.na(data$mh103) | data$mh103 >= 98

second_bad <-
  is.na(data$mh120) | data$mh120 == -1 | data$mh120 >= 98 |
  is.na(data$mh121) | data$mh121 >= 98 |
  is.na(data$mh122) | data$mh122 >= 98

data$MHcidi_depression[first_bad & second_bad] <- NA_real_

attr(data$MHcidi_depression, "label") <-
  "Fulfills criteria for a major depressive episode in the last 12 months"
attr(data$MHcidi_depression, "labels") <- c("No" = 0, "Yes" = 1)
attr(data$MHcidi_depression, "note") <- paste(
  "Major depressive episode in the last 12 months.",
  "Individuals who answer DK/RF to early symptoms are routed past additional",
  "questions required to fulfill criteria and are subsequently coded as missing."
)

data[c(
  "mdd_a_1new","mdd_a_2new","mdd_symptom_count_new",
  "mdd_symptom_count_2new","mdd_diagnosis1","mdd_diagnosis2"
)] <- NULL
