source("../Utilities/Utilities-Main.R")

### HH + FFM: Effectiveness ------------------
#### Effectiveness for HH + FFM ------------------
data_HEXACO_eff <- data_HEXACO %>% filter(INCLUDE_AS_JUDGE_OP == 1)

HEXACO_eff_results <- HEX_analysis(data_HEXACO_eff, IV_name = "IV", 
                                   var_names = list(rxx = "rxx", ryy = "ryy", rxy = "rxy", N = "N"))
format_file(HEXACO_eff_results$overall)



cor_HH_FFM_eff <- lav_matrix_lower2full(
                         c(1,
                           0.26, 1,
                           0.25, 0.35,	1,
                           0.35, 0.35,	0.43,	1,
                           0.43, 0.27,	0.17,	0.19,	1,
                           -0.09, 0.54, 0.23,	0.13,	0.07,	1,
                           0.11, 0.13, 0.12, 0.11, 0.16, 0.25, 1))
colnames(cor_HH_FFM_eff) <- c("E", "A", "C", "ES", "O", "HH", "Eff")

cor_HH_FFM_emg <- lav_matrix_lower2full(
  c(1,
    0.26, 1,
    0.25, 0.35,	1,
    0.35, 0.35,	0.43,	1,
    0.43, 0.27,	0.17,	0.19,	1,
    -0.09, 0.54, 0.23,	0.13,	0.07,	1,
    0.22, 0.11, 0.15, 0.16, 0.16, 0.01, 1))
colnames(cor_HH_FFM_emg) <- c("E", "A", "C", "ES", "O", "HH", "Emg")



N_HH_FFM_eff <- c(24919,
                  23902, 22478,
                  23289, 21686, 22675,
                  22922, 22904, 20018, 21869,
                  12636, 15692, 14445, 14341, 10768,
                  37449, 34663, 33537, 19868, 15641, 1045)

N_HH_FFM_emg <- c(24919,
                  23902, 22478,
                  23289, 21686, 22675,
                  22922, 22904, 20018, 21869,
                  12636, 15692, 14445, 14341, 10768,
                  19870, 13921, 25288, 15358, 13769, 2186)

emg_HH_FFM_model <- 'Emg ~ E + A + C + ES + O + HH'
emg_HH_FFM_fit <- sem(emg_HH_FFM_model, sample.cov = cor_HH_FFM_emg, 
                      sample.nobs = harmonic.mean(N_HH_FFM_emg))  # 13340.36
summary(emg_HH_FFM_fit, fit.measures = TRUE, standardized = TRUE)
summary(emg_HH_FFM_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.06542970)



eff_HH_FFM_model <- 'Eff ~ E + A + C + ES + O + HH'
eff_HH_FFM_fit <- sem(eff_HH_FFM_model, sample.cov = cor_HH_FFM_eff, 
                      sample.nobs = harmonic.mean(N_HH_FFM_eff))  # 10639.14
summary(eff_HH_FFM_fit, fit.measures = TRUE, standardized = TRUE)
summary(eff_HH_FFM_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.09716575)

#### Effectiveness for FFM ------------------
eff_FFM_model <- 'Eff ~ E + A + C + ES + O'
eff_FFM_fit <- sem(eff_FFM_model, sample.cov = cor_HH_FFM_eff, 
                   sample.nobs = psych::harmonic.mean(N_HH_FFM_eff[-c(11:15)]))  # 10005.31
summary(eff_FFM_fit, fit.measures = TRUE, standardized = TRUE)
summary(eff_FFM_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.03978918)

