options(scipen = 999) library(dplyr) setwd("C:/Users/irina/Documents/DND/EOHI/eohi1") df <- read.csv("ehi1.csv") data <- df %>% select(eohiDGEN_mean, ehi_global_mean, demo_sex, demo_age_1, edu3, AOT_total, CRT_correct, CRT_int, bs_28, bs_easy, bs_hard, cal_selfActual, cal_global) %>% filter(demo_sex != "Prefer not to say") print(colSums(is.na(data))) print(sapply(data, class)) # Create dummy variable for sex (0 = Male, 1 = Female) data$sex_dummy <- ifelse(data$demo_sex == "Female", 1, 0) # Verify the dummy coding print(table(data$demo_sex, data$sex_dummy)) #descriptives # Descriptives for age print(summary(data$demo_age_1)) print(sd(data$demo_age_1, na.rm = TRUE)) # Center demo_age_1 (subtract the mean) data$age_centered <- data$demo_age_1 - mean(data$demo_age_1, na.rm = TRUE) # Verify the centering print(summary(data$age_centered)) # Descriptives for sex (frequency table) print(table(data$demo_sex)) print(prop.table(table(data$demo_sex))) # Descriptives for sex dummy variable print(table(data$sex_dummy)) # Convert edu3 to numeric factor for correlations (1, 2, 3) # First ensure edu3 is a factor, then convert to numeric data$edu3 <- factor(data$edu3, levels = c("HS_TS", "C_Ug", "grad_prof"), ordered = TRUE) data$edu_num <- as.numeric(data$edu3) # Check the numeric conversion print(table(data$edu_num, useNA = "ifany")) # Verify the conversion print(table(data$edu3, data$edu_num, useNA = "ifany")) ####correlation matrix #### # Select numeric variables for correlation matrix numeric_vars <- data %>% select(eohiDGEN_mean, ehi_global_mean, sex_dummy, age_centered, edu_num, AOT_total, CRT_correct, CRT_int, bs_28, bs_easy, bs_hard, cal_selfActual, cal_global) # Create correlation matrix cor_matrix <- cor(numeric_vars, use = "complete.obs") # Print correlation matrix print("Correlation Matrix:") print(round(cor_matrix, 3)) # Save correlation matrix to CSV write.csv(cor_matrix, "correlation_matrix.csv", row.names = TRUE) print("Correlation matrix saved to correlation_matrix.csv")