setwd("C:/Users/irina/Documents/DND/EOHI/eohi2")

options(scipen = 999)

df <- read.csv("eohi2.csv")

library(psych)
library(dplyr)
library(knitr)

# variable sets
present_pref_vars <- c("present_pref_read", "present_pref_music", "present_pref_tv", "present_pref_nap", "present_pref_travel")
past_5_pref_vars  <- c("past_5_pref_read", "past_5_pref_music", "past_5_pref_tv", "past_5_pref_nap", "past_5_pref_travel")
past_10_pref_vars <- c("past_10_pref_read", "past_10_pref_music", "past_10_pref_tv", "past_10_pref_nap", "past_10_pref_travel")
fut_5_pref_vars   <- c("fut_5_pref_read", "fut_5_pref_music", "fut_5_pref_tv", "fut_5_pref_nap", "fut_5_pref_travel")
fut_10_pref_vars  <- c("fut_10_pref_read", "fut_10_pref_music", "fut_10_pref_tv", "fut_10_pref_nap", "fut_10_pref_travel")

present_pers_vars <- c("present_pers_extravert", "present_pers_critical", "present_pers_dependable", "present_pers_anxious", "present_pers_complex")
past_5_pers_vars  <- c("past_5_pers_extravert", "past_5_pers_critical", "past_5_pers_dependable", "past_5_pers_anxious", "past_5_pers_complex")
past_10_pers_vars <- c("past_10_pers_extravert", "past_10_pers_critical", "past_10_pers_dependable", "past_10_pers_anxious", "past_10_pers_complex")
fut_5_pers_vars   <- c("fut_5_pers_extravert", "fut_5_pers_critical", "fut_5_pers_dependable", "fut_5_pers_anxious", "fut_5_pers_complex")
fut_10_pers_vars  <- c("fut_10_pers_extravert", "fut_10_pers_critical", "fut_10_pers_dependable", "fut_10_pers_anxious", "fut_10_pers_complex")

present_val_vars <- c("present_val_obey", "present_val_trad", "present_val_opinion", "present_val_performance", "present_val_justice")
past_5_val_vars  <- c("past_5_val_obey", "past_5_val_trad", "past_5_val_opinion", "past_5_val_performance", "past_5_val_justice")
past_10_val_vars <- c("past_10_val_obey", "past_10_val_trad", "past_10_val_opinion", "past_10_val_performance", "past_10_val_justice")
fut_5_val_vars   <- c("fut_5_val_obey", "fut_5_val_trad", "fut_5_val_opinion", "fut_5_val_performance", "fut_5_val_justice")
fut_10_val_vars  <- c("fut_10_val_obey", "fut_10_val_trad", "fut_10_val_opinion", "fut_10_val_performance", "fut_10_val_justice")

all_scales <- list(
  "Present_Preferences" = present_pref_vars,
  "Past5_Preferences"   = past_5_pref_vars,
  "Past10_Preferences"  = past_10_pref_vars,
  "Fut5_Preferences"    = fut_5_pref_vars,
  "Fut10_Preferences"   = fut_10_pref_vars,
  "Present_Personality" = present_pers_vars,
  "Past5_Personality"   = past_5_pers_vars,
  "Past10_Personality"  = past_10_pers_vars,
  "Fut5_Personality"    = fut_5_pers_vars,
  "Fut10_Personality"   = fut_10_pers_vars,
  "Present_Values"      = present_val_vars,
  "Past5_Values"        = past_5_val_vars,
  "Past10_Values"       = past_10_val_vars,
  "Fut5_Values"         = fut_5_val_vars,
  "Fut10_Values"        = fut_10_val_vars
)

# reliability 
reliability_results <- data.frame(Scale=character(), Alpha=double(), Omega=double(), stringsAsFactors=FALSE)

for(scale_name in names(all_scales)) {
  vars <- all_scales[[scale_name]]
  if(!all(vars %in% names(df))) next
  dat <- df[, vars]
  alpha_val <- tryCatch({psych::alpha(dat)$total$raw_alpha}, error=function(e) NA)
  omega_val <- tryCatch({psych::omega(dat, nfactors=1)$omega.tot}, error=function(e) NA)
  reliability_results <- rbind(reliability_results, data.frame(Scale=scale_name, Alpha=alpha_val, Omega=omega_val))
}

print(kable(reliability_results, digits=3, caption="Internal Consistency (Alpha & Omega)"))

# ICC test-retest
df$pref_present  <- rowMeans(df[, present_pref_vars], na.rm=TRUE)
df$pref_past5    <- rowMeans(df[, past_5_pref_vars], na.rm=TRUE)
df$pref_past10   <- rowMeans(df[, past_10_pref_vars], na.rm=TRUE)
df$pref_fut5     <- rowMeans(df[, fut_5_pref_vars], na.rm=TRUE)
df$pref_fut10    <- rowMeans(df[, fut_10_pref_vars], na.rm=TRUE)
pref_mat <- df[, c("pref_present","pref_past5","pref_past10","pref_fut5","pref_fut10")]

df$pers_present  <- rowMeans(df[, present_pers_vars], na.rm=TRUE)
df$pers_past5    <- rowMeans(df[, past_5_pers_vars], na.rm=TRUE)
df$pers_past10   <- rowMeans(df[, past_10_pers_vars], na.rm=TRUE)
df$pers_fut5     <- rowMeans(df[, fut_5_pers_vars], na.rm=TRUE)
df$pers_fut10    <- rowMeans(df[, fut_10_pers_vars], na.rm=TRUE)
pers_mat <- df[, c("pers_present","pers_past5","pers_past10","pers_fut5","pers_fut10")]

df$val_present  <- rowMeans(df[, present_val_vars], na.rm=TRUE)
df$val_past5    <- rowMeans(df[, past_5_val_vars], na.rm=TRUE)
df$val_past10   <- rowMeans(df[, past_10_val_vars], na.rm=TRUE)
df$val_fut5     <- rowMeans(df[, fut_5_val_vars], na.rm=TRUE)
df$val_fut10    <- rowMeans(df[, fut_10_val_vars], na.rm=TRUE)
val_mat <- df[, c("val_present","val_past5","val_past10","val_fut5","val_fut10")]

icc_pref <- psych::ICC(pref_mat)
icc_pers <- psych::ICC(pers_mat)
icc_val  <- psych::ICC(val_mat)

print(kable(icc_pref$results["Single_raters_absolute", , drop=FALSE], caption="Test–Retest ICC: Preferences"))
print(kable(icc_pers$results["Single_raters_absolute", , drop=FALSE], caption="Test–Retest ICC: Personality"))
print(kable(icc_val$results["Single_raters_absolute", , drop=FALSE], caption="Test–Retest ICC: Values"))
