options(scipen = 999)

library(dplyr)

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

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

# Display means and standard deviations of non-standardized variables for manual checking
print(round(mean(df$ehiDGEN_5_mean, na.rm = TRUE), 5))
print(round(sd(df$ehiDGEN_5_mean, na.rm = TRUE), 5))
print(round(mean(df$ehiDGEN_10_mean, na.rm = TRUE), 5))
print(round(sd(df$ehiDGEN_10_mean, na.rm = TRUE), 5))
print(round(mean(df$ehi5_global_mean, na.rm = TRUE), 5))
print(round(sd(df$ehi5_global_mean, na.rm = TRUE), 5))
print(round(mean(df$ehi10_global_mean, na.rm = TRUE), 5))
print(round(sd(df$ehi10_global_mean, na.rm = TRUE), 5))

# Calculate means and standard deviations for standardization
mean_DGEN_5 <- mean(df$ehiDGEN_5_mean, na.rm = TRUE)
sd_DGEN_5 <- sd(df$ehiDGEN_5_mean, na.rm = TRUE)

mean_DGEN_10 <- mean(df$ehiDGEN_10_mean, na.rm = TRUE)
sd_DGEN_10 <- sd(df$ehiDGEN_10_mean, na.rm = TRUE)

mean_DS_5 <- mean(df$ehi5_global_mean, na.rm = TRUE)
sd_DS_5 <- sd(df$ehi5_global_mean, na.rm = TRUE)

mean_DS_10 <- mean(df$ehi10_global_mean, na.rm = TRUE)
sd_DS_10 <- sd(df$ehi10_global_mean, na.rm = TRUE)

# Create standardized variables
df$stdDGEN_5 <- (df$ehiDGEN_5_mean - mean_DGEN_5) / sd_DGEN_5
df$stdDGEN_10 <- (df$ehiDGEN_10_mean - mean_DGEN_10) / sd_DGEN_10
df$stdDS_5 <- (df$ehi5_global_mean - mean_DS_5) / sd_DS_5
df$stdDS_10 <- (df$ehi10_global_mean - mean_DS_10) / sd_DS_10

# Check that variables have been standardized
print(round(mean(df$stdDGEN_5, na.rm = TRUE), 5))
print(round(sd(df$stdDGEN_5, na.rm = TRUE), 5))
print(round(mean(df$stdDGEN_10, na.rm = TRUE), 5))
print(round(sd(df$stdDGEN_10, na.rm = TRUE), 5))
print(round(mean(df$stdDS_5, na.rm = TRUE), 5))
print(round(sd(df$stdDS_5, na.rm = TRUE), 5))
print(round(mean(df$stdDS_10, na.rm = TRUE), 5))
print(round(sd(df$stdDS_10, na.rm = TRUE), 5))

# Calculate mean of standardized variables
df$stdEHI_mean <- rowMeans(df[, c("stdDGEN_5", "stdDGEN_10", "stdDS_5", "stdDS_10")], na.rm = TRUE)