options(scipen = 999)
setwd("C:/Users/irina/Documents/DND/EOHI/eohi1")

# Load data
ehi1 <- read.csv("ehi1.csv")

# Calculate mean scores for EHI variables

# 1. Preferences mean
ehi1$ehi_pref_mean <- rowMeans(ehi1[, c("ehi_pref_read", "ehi_pref_music", 
                                         "ehi_pref_tv", "ehi_pref_nap", 
                                         "ehi_pref_travel")], na.rm = TRUE)

# 2. Personality mean
ehi1$ehi_pers_mean <- rowMeans(ehi1[, c("ehi_pers_extravert", "ehi_pers_critical", 
                                         "ehi_pers_dependable", "ehi_pers_anxious", 
                                         "ehi_pers_complex")], na.rm = TRUE)

# 3. Values mean
ehi1$ehi_val_mean <- rowMeans(ehi1[, c("ehi_val_obey", "ehi_val_trad", 
                                        "ehi_val_opinion", "ehi_val_performance", 
                                        "ehi_val_justice")], na.rm = TRUE)

# 4. Life satisfaction mean
ehi1$ehi_life_mean <- rowMeans(ehi1[, c("ehi_life_ideal", "ehi_life_excellent", 
                                         "ehi_life_satisfied", "ehi_life_important", 
                                         "ehi_life_change")], na.rm = TRUE)

# 5. Global mean (all 20 variables)
ehi1$ehi_global_mean <- rowMeans(ehi1[, c("ehi_pref_read", "ehi_pref_music", 
                                           "ehi_pref_tv", "ehi_pref_nap", 
                                           "ehi_pref_travel",
                                           "ehi_pers_extravert", "ehi_pers_critical", 
                                           "ehi_pers_dependable", "ehi_pers_anxious", 
                                           "ehi_pers_complex",
                                           "ehi_val_obey", "ehi_val_trad", 
                                           "ehi_val_opinion", "ehi_val_performance", 
                                           "ehi_val_justice",
                                           "ehi_life_ideal", "ehi_life_excellent", 
                                           "ehi_life_satisfied", "ehi_life_important", 
                                           "ehi_life_change")], na.rm = TRUE)

# QA: Verify mean calculations
cat("\n=== QUALITY ASSURANCE CHECK ===\n")
cat("Verifying EHI mean calculations\n\n")

cat("--- FIRST 5 ROWS: PREFERENCES MEAN ---\n")
for (i in 1:5) {
  vals <- c(ehi1$ehi_pref_read[i], ehi1$ehi_pref_music[i], 
            ehi1$ehi_pref_tv[i], ehi1$ehi_pref_nap[i], 
            ehi1$ehi_pref_travel[i])
  calc_mean <- mean(vals, na.rm = TRUE)
  actual_mean <- ehi1$ehi_pref_mean[i]
  match <- abs(calc_mean - actual_mean) < 1e-10
  cat(sprintf("Row %d: [%g, %g, %g, %g, %g] → Calculated: %.5f | Actual: %.5f %s\n",
              i, vals[1], vals[2], vals[3], vals[4], vals[5], 
              calc_mean, actual_mean, ifelse(match, "✓", "✗")))
}

cat("\n--- FIRST 5 ROWS: PERSONALITY MEAN ---\n")
for (i in 1:5) {
  vals <- c(ehi1$ehi_pers_extravert[i], ehi1$ehi_pers_critical[i], 
            ehi1$ehi_pers_dependable[i], ehi1$ehi_pers_anxious[i], 
            ehi1$ehi_pers_complex[i])
  calc_mean <- mean(vals, na.rm = TRUE)
  actual_mean <- ehi1$ehi_pers_mean[i]
  match <- abs(calc_mean - actual_mean) < 1e-10
  cat(sprintf("Row %d: [%g, %g, %g, %g, %g] → Calculated: %.5f | Actual: %.5f %s\n",
              i, vals[1], vals[2], vals[3], vals[4], vals[5], 
              calc_mean, actual_mean, ifelse(match, "✓", "✗")))
}

cat("\n--- FIRST 5 ROWS: VALUES MEAN ---\n")
for (i in 1:5) {
  vals <- c(ehi1$ehi_val_obey[i], ehi1$ehi_val_trad[i], 
            ehi1$ehi_val_opinion[i], ehi1$ehi_val_performance[i], 
            ehi1$ehi_val_justice[i])
  calc_mean <- mean(vals, na.rm = TRUE)
  actual_mean <- ehi1$ehi_val_mean[i]
  match <- abs(calc_mean - actual_mean) < 1e-10
  cat(sprintf("Row %d: [%g, %g, %g, %g, %g] → Calculated: %.5f | Actual: %.5f %s\n",
              i, vals[1], vals[2], vals[3], vals[4], vals[5], 
              calc_mean, actual_mean, ifelse(match, "✓", "✗")))
}

cat("\n--- FIRST 5 ROWS: LIFE SATISFACTION MEAN ---\n")
for (i in 1:5) {
  vals <- c(ehi1$ehi_life_ideal[i], ehi1$ehi_life_excellent[i], 
            ehi1$ehi_life_satisfied[i], ehi1$ehi_life_important[i], 
            ehi1$ehi_life_change[i])
  calc_mean <- mean(vals, na.rm = TRUE)
  actual_mean <- ehi1$ehi_life_mean[i]
  match <- abs(calc_mean - actual_mean) < 1e-10
  cat(sprintf("Row %d: [%g, %g, %g, %g, %g] → Calculated: %.5f | Actual: %.5f %s\n",
              i, vals[1], vals[2], vals[3], vals[4], vals[5], 
              calc_mean, actual_mean, ifelse(match, "✓", "✗")))
}

cat("\n--- FIRST 5 ROWS: GLOBAL MEAN (20 variables) ---\n")
for (i in 1:5) {
  vals <- c(ehi1$ehi_pref_read[i], ehi1$ehi_pref_music[i], 
            ehi1$ehi_pref_tv[i], ehi1$ehi_pref_nap[i], 
            ehi1$ehi_pref_travel[i],
            ehi1$ehi_pers_extravert[i], ehi1$ehi_pers_critical[i], 
            ehi1$ehi_pers_dependable[i], ehi1$ehi_pers_anxious[i], 
            ehi1$ehi_pers_complex[i],
            ehi1$ehi_val_obey[i], ehi1$ehi_val_trad[i], 
            ehi1$ehi_val_opinion[i], ehi1$ehi_val_performance[i], 
            ehi1$ehi_val_justice[i],
            ehi1$ehi_life_ideal[i], ehi1$ehi_life_excellent[i], 
            ehi1$ehi_life_satisfied[i], ehi1$ehi_life_important[i], 
            ehi1$ehi_life_change[i])
  calc_mean <- mean(vals, na.rm = TRUE)
  actual_mean <- ehi1$ehi_global_mean[i]
  match <- abs(calc_mean - actual_mean) < 1e-10
  cat(sprintf("Row %d: 20 values → Calculated: %.5f | Actual: %.5f %s\n",
              i, calc_mean, actual_mean, ifelse(match, "✓", "✗")))
}

# Overall QA check for all rows
cat("\n--- OVERALL QA CHECK (ALL ROWS) ---\n")

qa_checks <- list(
  list(vars = c("ehi_pref_read", "ehi_pref_music", "ehi_pref_tv", "ehi_pref_nap", "ehi_pref_travel"),
       target = "ehi_pref_mean", name = "Preferences"),
  list(vars = c("ehi_pers_extravert", "ehi_pers_critical", "ehi_pers_dependable", "ehi_pers_anxious", "ehi_pers_complex"),
       target = "ehi_pers_mean", name = "Personality"),
  list(vars = c("ehi_val_obey", "ehi_val_trad", "ehi_val_opinion", "ehi_val_performance", "ehi_val_justice"),
       target = "ehi_val_mean", name = "Values"),
  list(vars = c("ehi_life_ideal", "ehi_life_excellent", "ehi_life_satisfied", "ehi_life_important", "ehi_life_change"),
       target = "ehi_life_mean", name = "Life Satisfaction"),
  list(vars = c("ehi_pref_read", "ehi_pref_music", "ehi_pref_tv", "ehi_pref_nap", "ehi_pref_travel",
                "ehi_pers_extravert", "ehi_pers_critical", "ehi_pers_dependable", "ehi_pers_anxious", "ehi_pers_complex",
                "ehi_val_obey", "ehi_val_trad", "ehi_val_opinion", "ehi_val_performance", "ehi_val_justice",
                "ehi_life_ideal", "ehi_life_excellent", "ehi_life_satisfied", "ehi_life_important", "ehi_life_change"),
       target = "ehi_global_mean", name = "Global")
)

all_checks_passed <- TRUE

for (check in qa_checks) {
  calc_mean <- rowMeans(ehi1[, check$vars], na.rm = TRUE)
  actual_mean <- ehi1[[check$target]]
  discrepancies <- which(abs(calc_mean - actual_mean) > 1e-10)
  
  if (length(discrepancies) > 0) {
    cat(sprintf("FAIL: %s mean (n_vars = %d)\n", check$name, length(check$vars)))
    cat(sprintf("  Found %d discrepancies in rows: %s\n", 
                length(discrepancies), 
                paste(head(discrepancies, 10), collapse = ", ")))
    all_checks_passed <- FALSE
  } else {
    cat(sprintf("PASS: %s mean (n_vars = %d, n_rows = %d)\n", 
                check$name, length(check$vars), nrow(ehi1)))
  }
}

cat("\n")
if (all_checks_passed) {
  cat("*** ALL QA CHECKS PASSED ***\n")
} else {
  cat("*** SOME QA CHECKS FAILED - REVIEW ABOVE ***\n")
}

# Save updated dataset
write.csv(ehi1, "ehi1.csv", row.names = FALSE)
cat("\nDataset saved to ehi1.csv\n")