setwd("C:/Users/irina/Documents/DND/EOHI/eohi2")
options(scipen = 999)
df <- read.csv("eohi2.csv")
library(psych)
library(dplyr)
library(knitr)
# Your named variable sets (replace df with your actual dataframe name)
present_pref_vars <- c("present_pref_read", "present_pref_music", "present_pref_tv", "present_pref_nap", "present_pref_travel")
past_5_pref_vars <- c("past_5_pref_read", "past_5_pref_music", "past_5_pref_TV", "past_5_pref_nap", "past_5_pref_travel")
past_10_pref_vars <- c("past_10_pref_read", "past_10_pref_music", "past_10_pref_TV", "past_10_pref_nap", "past_10_pref_travel")
fut_5_pref_vars <- c("fut_5_pref_read", "fut_5_pref_music", "fut_5_pref_TV", "fut_5_pref_nap", "fut_5_pref_travel")
fut_10_pref_vars <- c("fut_10_pref_read", "fut_10_pref_music", "fut_10_pref_TV", "fut_10_pref_nap", "fut_10_pref_travel")
present_pers_vars <- c("present_pers_extravert", "present_pers_critical", "present_pers_dependable", "present_pers_anxious", "present_pers_complex")
past_5_pers_vars <- c("past_5_pers_extravert", "past_5_pers_critical", "past_5_pers_dependable", "past_5_pers_anxious", "past_5_pers_complex")
past_10_pers_vars <- c("past_10_pers_extravert", "past_10_pers_critical", "past_10_pers_dependable", "past_10_pers_anxious", "past_10_pers_complex")
fut_5_pers_vars <- c("fut_5_pers_extravert", "fut_5_pers_critical", "fut_5_pers_dependable", "fut_5_pers_anxious", "fut_5_pers_complex")
fut_10_pers_vars <- c("fut_10_pers_extravert", "fut_10_pers_critical", "fut_10_pers_dependable", "fut_10_pers_anxious", "fut_10_pers_complex")
present_val_vars <- c("present_val_obey", "present_val_trad", "present_val_opinion", "present_val_performance", "present_val_justice")
past_5_val_vars <- c("past_5_val_obey", "past_5_val_trad", "past_5_val_opinion", "past_5_val_performance", "past_5_val_justice")
past_10_val_vars <- c("past_10_val_obey", "past_10_val_trad", "past_10_val_opinion", "past_10_val_performance", "past_10_val_justice")
fut_5_val_vars <- c("fut_5_val_obey", "fut_5_val_trad", "fut_5_val_opinion", "fut_5_val_performance", "fut_5_val_justice")
fut_10_val_vars <- c("fut_10_val_obey", "fut_10_val_trad", "fut_10_val_opinion", "fut_10_val_performance", "fut_10_val_justice")
all_scales <- list(
"Present_Preferences" = present_pref_vars,
"Past5_Preferences" = past_5_pref_vars,
"Past10_Preferences" = past_10_pref_vars,
"Fut5_Preferences" = fut_5_pref_vars,
"Fut10_Preferences" = fut_10_pref_vars,
"Present_Personality" = present_pers_vars,
"Past5_Personality" = past_5_pers_vars,
"Past10_Personality" = past_10_pers_vars,
"Fut5_Personality" = fut_5_pers_vars,
"Fut10_Personality" = fut_10_pers_vars,
"Present_Values" = present_val_vars,
"Past5_Values" = past_5_val_vars,
"Past10_Values" = past_10_val_vars,
"Fut5_Values" = fut_5_val_vars,
"Fut10_Values" = fut_10_val_vars
)
# Reliability analysis loop
alpha_results <- list()
total_results <- data.frame()
item_stats_results <- data.frame()
alpha_drop_results <- data.frame()
for(scale_name in names(all_scales)) {
vars <- all_scales[[scale_name]]
scale_data <- df %>% select(all_of(vars))
# Only run if there is more than one column present
if(ncol(scale_data) > 1) {
alpha_out <- psych::alpha(scale_data, check.keys = TRUE)
alpha_results[[scale_name]] <- alpha_out
# Print full output for diagnostics
cat("\n----------", scale_name, "----------\n")
print(alpha_out$total)
print(alpha_out$item.stats)
print(alpha_out$alpha.drop)
# Prepare results for CSV export
scale_total <- alpha_out$total
scale_items <- alpha_out$item.stats
scale_alpha_drop <- alpha_out$alpha.drop
# Add scale name to each row
scale_total$Scale <- scale_name
scale_items$Scale <- scale_name
scale_alpha_drop$Scale <- scale_name
# Combine results by type
total_results <- rbind(total_results, scale_total)
item_stats_results <- rbind(item_stats_results, scale_items)
alpha_drop_results <- rbind(alpha_drop_results, scale_alpha_drop)
}
}
# Create HTML report
html_content <- "
Reliability Analysis Results
Reliability Analysis Results
Generated on: " %+% Sys.time() %+% "
"
# Add total statistics
html_content <- html_content %+% "Overall Scale Statistics
| Scale | Raw Alpha | Std Alpha | G6(SMC) | Average r | S/N | ASE | Mean | SD | Median r |
"
for(i in 1:nrow(total_results)) {
row <- total_results[i,]
html_content <- html_content %+% sprintf("| %s | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f |
",
row$Scale, row$raw_alpha, row$std.alpha, row$G6.smc, row$average_r, row$S.N, row$ase, row$mean, row$sd, row$median_r)
}
html_content <- html_content %+% "
"
# Add item statistics
html_content <- html_content %+% "Item Statistics
| Scale | Item | n | Raw r | Std r | r.cor | r.drop | Mean | SD |
"
for(i in 1:nrow(item_stats_results)) {
row <- item_stats_results[i,]
html_content <- html_content %+% sprintf("| %s | %s | %.0f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f |
",
row$Scale, rownames(row), row$n, row$raw.r, row$std.r, row$r.cor, row$r.drop, row$mean, row$sd)
}
html_content <- html_content %+% "
"
# Add alpha if dropped
html_content <- html_content %+% "Alpha If Item Dropped
| Scale | Item | Raw Alpha | Std Alpha | G6(SMC) | Average r | S/N | Alpha SE | Var r | Med r |
"
for(i in 1:nrow(alpha_drop_results)) {
row <- alpha_drop_results[i,]
html_content <- html_content %+% sprintf("| %s | %s | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f | %.4f |
",
row$Scale, rownames(row), row$raw_alpha, row$std.alpha, row$G6.smc, row$average_r, row$S.N, row$alpha.se, row$var.r, row$med.r)
}
html_content <- html_content %+% "
"
# Write HTML file
writeLines(html_content, "reliability_analysis_report.html")
# Check for reversed items
cat("\n=== REVERSED ITEMS CHECK ===\n")
for(scale_name in names(all_scales)) {
vars <- all_scales[[scale_name]]
scale_data <- df %>% select(all_of(vars))
if(ncol(scale_data) > 1) {
alpha_out <- alpha_results[[scale_name]]
# Check if any items were reversed by looking at the keys
if(!is.null(alpha_out$keys)) {
keys_df <- as.data.frame(alpha_out$keys)
reversed_items <- rownames(keys_df)[keys_df[,1] < 0]
if(length(reversed_items) > 0) {
cat(scale_name, "reversed items:", paste(reversed_items, collapse = ", "), "\n")
} else {
cat(scale_name, ": No items reversed\n")
}
} else {
cat(scale_name, ": No keys available\n")
}
}
}