# Load required libraries
library(Hmisc)
library(knitr)
library(dplyr)
library(corrr)
library(broom)
library(purrr)
library(tidyr)
library(tibble)
library(boot)

options(scipen = 999)

setwd("C:/Users/irina/Documents/DND/EOHI/eohi1")
# Load data
df1 <- read.csv("exp1.csv")

# Keep only required columns for the analysis
bs_vars   <- c("bs_28", "bs_easy", "bs_hard")
eohi_vars <- c(
  "eohi_pref","eohi_pers","eohi_val","eohi_life","eohi_mean",
  "eohiDGEN_pref","eohiDGEN_pers","eohiDGEN_val","eohiDGEN_life","eohiDGEN_mean"
)
cal_vars  <- c("cal_selfActual","cal_global")

df1 <- df1 %>% dplyr::select(dplyr::all_of(c(bs_vars, eohi_vars, cal_vars)))

# --- Brier score correlations vs EOHIs and Calibration ---

# Variables
bs_vars   <- c("bs_28", "bs_easy", "bs_hard")
eohi_vars <- c(
  "eohi_pref","eohi_pers","eohi_val","eohi_life","eohi_mean",
  "eohiDGEN_pref","eohiDGEN_pers","eohiDGEN_val","eohiDGEN_life","eohiDGEN_mean"
)
cal_vars  <- c("cal_selfActual","cal_global")

# Helper: tidy correlation (Pearson), pairwise complete
corr_tidy <- function(df, x_vars, y_vars) {
  grid <- expand.grid(x = x_vars, y = y_vars, stringsAsFactors = FALSE)
  results <- purrr::pmap_dfr(grid, function(x, y) {
    xv <- df[[x]]; yv <- df[[y]]
    ok <- is.finite(xv) & is.finite(yv)
    if (sum(ok) < 3) {
      return(tibble::tibble(var_x = x, var_y = y, n = sum(ok), r = NA_real_, p = NA_real_))
    }
    ct <- suppressWarnings(cor.test(xv[ok], yv[ok], method = "pearson"))
    tibble::tibble(var_x = x, var_y = y, n = sum(ok), r = unname(ct$estimate), p = ct$p.value)
  })
  dplyr::arrange(results, var_x, var_y)
}

# Compute correlations
corr_bs_eohi <- corr_tidy(df1, bs_vars, eohi_vars)
corr_bs_cal  <- corr_tidy(df1, bs_vars, cal_vars)

# Wide r-only tables (optional)
to_wide <- function(d) {
  tidyr::pivot_wider(d, id_cols = var_x, names_from = var_y, values_from = r)
}
wide_bs_eohi <- to_wide(corr_bs_eohi)
wide_bs_cal  <- to_wide(corr_bs_cal)

# Display
print("Correlations: Brier vs EOHIs (r, p, n)")
print(corr_bs_eohi)
print("Correlations: Brier vs Calibration (r, p, n)")
print(corr_bs_cal)

# If you want to export CSVs, uncomment:
# write.csv(corr_bs_eohi, "corr_bs_eohi.csv", row.names = FALSE)
# write.csv(corr_bs_cal,  "corr_bs_cal.csv",  row.names = FALSE)
# write.csv(wide_bs_eohi, "corr_bs_eohi_wide.csv", row.names = FALSE)
# write.csv(wide_bs_cal,  "corr_bs_cal_wide.csv",  row.names = FALSE)