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

# Load the data
exp1_data <- read.csv("exp1.csv")

# Define the NPastDiff variables by domain
nPastDiff_pref <- c("NPastDiff_pref_read", "NPastDiff_pref_music", "NPastDiff_pref_tv", "NPastDiff_pref_nap", "NPastDiff_pref_travel")
nPastDiff_pers <- c("NPastDiff_pers_extravert", "NPastDiff_pers_critical", "NPastDiff_pers_dependable", "NPastDiff_pers_anxious", "NPastDiff_pers_complex")
nPastDiff_val <- c("NPastDiff_val_obey", "NPastDiff_val_trad", "NPastDiff_val_opinion", "NPastDiff_val_performance", "NPastDiff_val_justice")
nPastDiff_life <- c("NPastDiff_life_ideal", "NPastDiff_life_excellent", "NPastDiff_life_satisfied", "NPastDiff_life_important", "NPastDiff_life_change")

# Define the NFutDiff variables by domain
nFutDiff_pref <- c("NFutDiff_pref_read", "NFutDiff_pref_music", "NFutDiff_pref_tv", "NFutDiff_pref_nap", "NFutDiff_pref_travel")
nFutDiff_pers <- c("NFutDiff_pers_extravert", "NFutDiff_pers_critical", "NFutDiff_pers_dependable", "NFutDiff_pers_anxious", "NFutDiff_pers_complex")
nFutDiff_val <- c("NFutDiff_val_obey", "NFutDiff_val_trad", "NFutDiff_val_opinion", "NFutDiff_val_performance", "NFutDiff_val_justice")
nFutDiff_life <- c("NFutDiff_life_ideal", "NFutDiff_life_excellent", "NFutDiff_life_satisfied", "NFutDiff_life_important", "NFutDiff_life_change")

# Calculate domain means for NPastDiff
exp1_data$NPastDiff_pref_mean <- rowMeans(exp1_data[, nPastDiff_pref], na.rm = TRUE)
exp1_data$NPastDiff_pers_mean <- rowMeans(exp1_data[, nPastDiff_pers], na.rm = TRUE)
exp1_data$NPastDiff_val_mean <- rowMeans(exp1_data[, nPastDiff_val], na.rm = TRUE)
exp1_data$NPastDiff_life_mean <- rowMeans(exp1_data[, nPastDiff_life], na.rm = TRUE)

# Calculate domain means for NFutDiff
exp1_data$NFutDiff_pref_mean <- rowMeans(exp1_data[, nFutDiff_pref], na.rm = TRUE)
exp1_data$NFutDiff_pers_mean <- rowMeans(exp1_data[, nFutDiff_pers], na.rm = TRUE)
exp1_data$NFutDiff_val_mean <- rowMeans(exp1_data[, nFutDiff_val], na.rm = TRUE)
exp1_data$NFutDiff_life_mean <- rowMeans(exp1_data[, nFutDiff_life], na.rm = TRUE)

# Save the updated data
write.csv(exp1_data, "exp1.csv", row.names = FALSE)

# Display summary of the calculated domain means
cat("NPastDiff domain means summary:\n")
summary(exp1_data[, c("NPastDiff_pref_mean", "NPastDiff_pers_mean", "NPastDiff_val_mean", "NPastDiff_life_mean")])

cat("\nNFutDiff domain means summary:\n")
summary(exp1_data[, c("NFutDiff_pref_mean", "NFutDiff_pers_mean", "NFutDiff_val_mean", "NFutDiff_life_mean")])

# Show first few rows to verify calculations
cat("\nFirst 5 rows of calculated domain means:\n")
domain_means_cols <- c("NPastDiff_pref_mean", "NPastDiff_pers_mean", "NPastDiff_val_mean", "NPastDiff_life_mean",
                       "NFutDiff_pref_mean", "NFutDiff_pers_mean", "NFutDiff_val_mean", "NFutDiff_life_mean")
print(exp1_data[1:5, domain_means_cols])
