# Scale ID: 			MHC-SF
# Scale Name: 		Mental Health Continuum -- Short Form (MHC-SF)||Flourishing-Languishing Scale
# TILDA Variables: 	FLSocialWellbeing; FLPsychologicalWellbeing; FLEmotionalWellbeing; FLFlourishLangTotal
# Dataset:      		TILDA Waves 7
# Author:       		Brendan O'Maoileidgh
# Institution:  		The Irish Longitudinal Study on Ageing (TILDA)
# 
# Description:
# Generates total and subscale scoring for the Flourish Languish Scale.
# 
# Version:      1.0
# Date:         2026-09-01
# Language:     R
#
# Assumption: the working data frame is called `data`.

fl_items <- paste0("FlourLang", 1:14)
special_missing <- c(-99, -812, -823, -834, -845, -856, -867, -1)

for (v in fl_items) {
  data[[v]][data[[v]] %in% special_missing] <- NA_real_
  data[[paste0(v, "_recoded")]] <- data[[v]] - 1
}

rec <- paste0(fl_items, "_recoded")

# Stata egen rowtotal() ignores missing components and returns 0 if all are missing.
data$FLFlourishLangTotal <- rowSums(data[rec], na.rm = TRUE)
data$FLEmotionalWellbeing <- rowSums(data[rec[1:3]], na.rm = TRUE)
data$FLSocialWellbeing <- rowSums(data[rec[4:8]], na.rm = TRUE)
data$FLPsychologicalWellbeing <- rowSums(data[rec[9:14]], na.rm = TRUE)

attr(data$FLFlourishLangTotal, "label") <-
  "Total Scores of the Flourishing-Languishing Scale"
attr(data$FLEmotionalWellbeing, "label") <-
  "Scores of Emotion factor of Flourishing-Languishing Scale"
attr(data$FLPsychologicalWellbeing, "label") <-
  "Scores of Psychological Factor of Flourishing-Languishing scale"
attr(data$FLSocialWellbeing, "label") <-
  "Scores of Social factor of Flourishing-Languishing scale"

summary(data$FLFlourishLangTotal)
summary(data$FLEmotionalWellbeing)
summary(data$FLPsychologicalWellbeing)
summary(data$FLSocialWellbeing)
