source("../Utilities/Utilities-Main.R")
library(lavaan)
library(psych)
## Conditional Effect for Collectivism - Effectiveness -----------------------
data_FFM <- data_FFM %>% group_by(IV) %>% mutate(S = median(Collectivism, na.rm = TRUE), split_on = Collectivism > S) %>% ungroup()
data_FFM_eff <- data_FFM %>% filter(INCLUDE_AS_JUDGE_OP == 1)

eff_J_O <- data_FFM_eff %>% filter(IV == 1)
eff_J_C <- data_FFM_eff %>% filter(IV == 2)
eff_J_E <- data_FFM_eff %>% filter(IV == 3)
eff_J_A <- data_FFM_eff %>% filter(IV == 4)
eff_J_N <- data_FFM_eff %>% filter(IV == 5)


data_FFM_eff %>% filter(!is.na(Collectivism)) %>% group_by(IV) %>% summarize(N_count = sum(N), k_count = length(`Article ID`))  # 12732 - 18935

### What if we treat collectivism as a categorical moderator?
eff_mod_coll <- BF_analysis(data_FFM_eff, mod = "split_on", IV_name = "IV")$mod_result
## Low: E/A/C/N/O - .14, .12, .12, .08, .12
## High: E/A/C/N/O - .24, .19, .20, .20, .20
format_file_mods(eff_mod_coll, order = c("E","A","C","N","O"), write = FALSE)


# Assume fixed FFM intercorrelations, use conditional rho
# cor_FFM_eff_lowcol <- 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.11, 0.09, 0.11, 0.06, 0.10, 1))
# cor_FFM_eff_highcol <- 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.20, 0.16, 0.15, 0.13, 0.17, 1))
# N_FFM_eff_lowcol  <- c(24919,
#                        23902, 22478,
#                        23289, 21686, 22675,
#                        22922, 22904, 20018, 21869,
#                        12732, 16948, 18935, 17034, 17886)
# N_FFM_eff_highcol  <- N_FFM_eff_lowcol


## Assume fixed FFM intercorrelations, use categorical mods
cor_FFM_eff_lowcol <- 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.14, 0.12, 0.12, 0.08, 0.12, 1))
cor_FFM_eff_highcol <- 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.24, 0.19, 0.20, 0.20, 0.20, 1))
N_FFM_eff_lowcol  <- c(24919,
                       23902, 22478,
                       23289, 21686, 22675,
                       22922, 22904, 20018, 21869,
                       15785, 14626, 13406, 15115, 10422)
N_FFM_eff_highcol  <- c(24919,
                        23902, 22478,
                        23289, 21686, 22675,
                        22922, 22904, 20018, 21869,
                        3150, 2408, 3542, 2771, 2310)


### Assume FFM intercorrelations vary across collectivism as well
# cor_FFM_eff_lowcol <- lav_matrix_lower2full(
#   c(1,
#     0.28, 1,
#     0.19, 0.28,	1,
#     0.32, 0.37,	0.43,	1,
#     0.40, 0.20,	0.07,	0.14,	1,
#     0.11, 0.09, 0.11, 0.06, 0.10, 1))
# cor_FFM_eff_highcol <- lav_matrix_lower2full(
#   c(1,
#     0.31, 1,
#     0.27, 0.38,	1,
#     0.38, 0.34,	0.43,	1,
#     0.42, 0.32,	0.23,	0.21,	1,
#     0.20, 0.16, 0.15, 0.13, 0.17, 1))


colnames(cor_FFM_eff_lowcol) <- c("E", "A", "C", "ES", "O", "Eff")
colnames(cor_FFM_eff_highcol) <- c("E", "A", "C", "ES", "O", "Eff")



eff_model_lowcol <- 'Eff ~ E + A + C + ES + O'
eff_fit_lowcol <- sem(eff_model_lowcol, sample.cov = cor_FFM_eff_lowcol, 
                      sample.nobs = harmonic.mean(N_FFM_eff_lowcol))
summary(eff_fit_lowcol, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
summary(eff_fit_lowcol, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.03392037)  ## If we use categorical mod
# sqrt(0.02337987) ## If we use conditional rho

eff_model_highcol <- 'Eff ~ E + A + C + ES + O'
eff_fit_highcol <- sem(eff_model_highcol, sample.cov = cor_FFM_eff_highcol, 
                       sample.nobs = harmonic.mean(N_FFM_eff_highcol))
summary(eff_fit_highcol, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
summary(eff_fit_highcol, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.09737494)  ## If we use categorical mod
# sqrt(0.06321359) ## If we use conditional rho



