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

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

# Set 1: DGEN_past_5.10_mean (3 variables)
data$DGEN_past_5.10_mean <- rowMeans(data[, c(
  "X5_10DGEN_past_pref", "X5_10DGEN_past_pers", "X5_10DGEN_past_val"
)], na.rm = TRUE)

# Set 2: DGEN_fut_5.10_mean (3 variables)
data$DGEN_fut_5.10_mean <- rowMeans(data[, c(
  "X5_10DGEN_fut_pref", "X5_10DGEN_fut_pers", "X5_10DGEN_fut_val"
)], na.rm = TRUE)

# Set 3: DGENpast_global_mean (9 variables)
data$DGENpast_global_mean <- rowMeans(data[, c(
  "DGEN_past_5_Pref", "DGEN_past_10_Pref", "X5_10DGEN_past_pref",
  "DGEN_past_5_Pers", "DGEN_past_10_Pers", "X5_10DGEN_past_pers",
  "DGEN_past_5_Val", "DGEN_past_10_Val", "X5_10DGEN_past_val"
)], na.rm = TRUE)

# Set 4: DGENfut_global_mean (9 variables)
data$DGENfut_global_mean <- rowMeans(data[, c(
  "DGEN_fut_5_Pref", "DGEN_fut_10_Pref", "X5_10DGEN_fut_pref",
  "DGEN_fut_5_Pers", "DGEN_fut_10_Pers", "X5_10DGEN_fut_pers",
  "DGEN_fut_5_Val", "DGEN_fut_10_Val", "X5_10DGEN_fut_val"
)], na.rm = TRUE)

# Set 5: DGEN_5_global_mean (6 variables)
data$DGEN_5_global_mean <- rowMeans(data[, c(
  "DGEN_past_5_Pref", "DGEN_past_5_Pers", "DGEN_past_5_Val",
  "DGEN_fut_5_Pref", "DGEN_fut_5_Pers", "DGEN_fut_5_Val"
)], na.rm = TRUE)

# Set 6: DGEN_10_global_mean (6 variables)
data$DGEN_10_global_mean <- rowMeans(data[, c(
  "DGEN_past_10_Pref", "DGEN_past_10_Pers", "DGEN_past_10_Val",
  "DGEN_fut_10_Pref", "DGEN_fut_10_Pers", "DGEN_fut_10_Val"
)], na.rm = TRUE)

# Set 7: DGEN_5.10_global_mean (6 variables)
data$DGEN_5.10_global_mean <- rowMeans(data[, c(
  "X5_10DGEN_past_pref", "X5_10DGEN_past_pers", "X5_10DGEN_past_val",
  "X5_10DGEN_fut_pref", "X5_10DGEN_fut_pers", "X5_10DGEN_fut_val"
)], na.rm = TRUE)

# Save the data
write.csv(data, "eohi2.csv", row.names = FALSE)

# ===== QA CODE: Check first 5 rows =====
cat("\n=== QUALITY ASSURANCE: Checking calculations for first 5 rows ===\n\n")

for (i in 1:min(5, nrow(data))) {
  cat("--- Row", i, "---\n")
  
  # Set 1: DGEN_past_5.10_mean
  calc1 <- mean(as.numeric(data[i, c(
    "X5_10DGEN_past_pref", "X5_10DGEN_past_pers", "X5_10DGEN_past_val"
  )]), na.rm = TRUE)
  cat("DGEN_past_5.10_mean: Calculated =", calc1, "| Stored =", data$DGEN_past_5.10_mean[i], 
      "| Match:", isTRUE(all.equal(calc1, data$DGEN_past_5.10_mean[i])), "\n")
  
  # Set 2: DGEN_fut_5.10_mean
  calc2 <- mean(as.numeric(data[i, c(
    "X5_10DGEN_fut_pref", "X5_10DGEN_fut_pers", "X5_10DGEN_fut_val"
  )]), na.rm = TRUE)
  cat("DGEN_fut_5.10_mean: Calculated =", calc2, "| Stored =", data$DGEN_fut_5.10_mean[i], 
      "| Match:", isTRUE(all.equal(calc2, data$DGEN_fut_5.10_mean[i])), "\n")
  
  # Set 3: DGENpast_global_mean
  calc3 <- mean(as.numeric(data[i, c(
    "DGEN_past_5_Pref", "DGEN_past_10_Pref", "X5_10DGEN_past_pref",
    "DGEN_past_5_Pers", "DGEN_past_10_Pers", "X5_10DGEN_past_pers",
    "DGEN_past_5_Val", "DGEN_past_10_Val", "X5_10DGEN_past_val"
  )]), na.rm = TRUE)
  cat("DGENpast_global_mean: Calculated =", calc3, "| Stored =", data$DGENpast_global_mean[i], 
      "| Match:", isTRUE(all.equal(calc3, data$DGENpast_global_mean[i])), "\n")
  
  # Set 4: DGENfut_global_mean
  calc4 <- mean(as.numeric(data[i, c(
    "DGEN_fut_5_Pref", "DGEN_fut_10_Pref", "X5_10DGEN_fut_pref",
    "DGEN_fut_5_Pers", "DGEN_fut_10_Pers", "X5_10DGEN_fut_pers",
    "DGEN_fut_5_Val", "DGEN_fut_10_Val", "X5_10DGEN_fut_val"
  )]), na.rm = TRUE)
  cat("DGENfut_global_mean: Calculated =", calc4, "| Stored =", data$DGENfut_global_mean[i], 
      "| Match:", isTRUE(all.equal(calc4, data$DGENfut_global_mean[i])), "\n")
  
  # Set 5: DGEN_5_global_mean
  calc5 <- mean(as.numeric(data[i, c(
    "DGEN_past_5_Pref", "DGEN_past_5_Pers", "DGEN_past_5_Val",
    "DGEN_fut_5_Pref", "DGEN_fut_5_Pers", "DGEN_fut_5_Val"
  )]), na.rm = TRUE)
  cat("DGEN_5_global_mean: Calculated =", calc5, "| Stored =", data$DGEN_5_global_mean[i], 
      "| Match:", isTRUE(all.equal(calc5, data$DGEN_5_global_mean[i])), "\n")
  
  # Set 6: DGEN_10_global_mean
  calc6 <- mean(as.numeric(data[i, c(
    "DGEN_past_10_Pref", "DGEN_past_10_Pers", "DGEN_past_10_Val",
    "DGEN_fut_10_Pref", "DGEN_fut_10_Pers", "DGEN_fut_10_Val"
  )]), na.rm = TRUE)
  cat("DGEN_10_global_mean: Calculated =", calc6, "| Stored =", data$DGEN_10_global_mean[i], 
      "| Match:", isTRUE(all.equal(calc6, data$DGEN_10_global_mean[i])), "\n")
  
  # Set 7: DGEN_5.10_global_mean
  calc7 <- mean(as.numeric(data[i, c(
    "X5_10DGEN_past_pref", "X5_10DGEN_past_pers", "X5_10DGEN_past_val",
    "X5_10DGEN_fut_pref", "X5_10DGEN_fut_pers", "X5_10DGEN_fut_val"
  )]), na.rm = TRUE)
  cat("DGEN_5.10_global_mean: Calculated =", calc7, "| Stored =", data$DGEN_5.10_global_mean[i], 
      "| Match:", isTRUE(all.equal(calc7, data$DGEN_5.10_global_mean[i])), "\n\n")
}

cat("=== QA CHECK COMPLETE ===\n")
