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


cor_FFM_model <- 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.12, 0.15, 0.02, 0.04, 0.12, 1,
    0.32, 0.04, 0.22, 0.12, 0.28, 0.17, 1,
    0.11, 0.13, 0.12, 0.11, 0.16, 0.52, 0.39, 1))


colnames(cor_FFM_model) <- c("E", "A", "C", "ES", "O", "CON", "IS", "Eff")

N_FFM_model <- c(22478, 
                 22675, 20018, 
                 23902, 21686, 22904, 
                 24919, 21869, 23289, 22922,
                 1058, 1054, 1041, 1069, 1091,
                 1174, 1170, 1041, 1016, 1091, 26295,
                 37449, 34663, 33537, 19868, 15641, 1960, 1605)


OB_model <- 'CON ~ a1 * C + a2 * E + a3 * ES + a4 * A
             IS ~ b1 * C + b2 * E + b3 * ES + b5 * O
             Eff ~ c1 * CON + c2 * IS
             CON ~~ IS'

fit_OB_model <- sem(OB_model, sample.cov = cor_FFM_model, 
                    sample.nobs = harmonic.mean(c(N_FFM_model)))
harmonic.mean(c(N_FFM_model))
summary(fit_OB_model, standardized = TRUE, fit.measures = TRUE)
modificationindices(fit_OB_model, sort = TRUE)

set.seed(2023)
### Mediation for Extraversion
MCMed("a2 * c1", object = fit_OB_model)  # CON
MCMed("b2 * c2", object = fit_OB_model)  # IS

### Mediation for Agreeableness
MCMed("a4 * c1", object = fit_OB_model)  # CON

### Mediation for Conscientiousness
MCMed("a1 * c1", object = fit_OB_model)  # CON
MCMed("b1 * c2", object = fit_OB_model)  # IS

### Mediation for Emotional Stability
MCMed("a3 * c1", object = fit_OB_model)  # CON
MCMed("b3 * c2", object = fit_OB_model)  # IS

### Mediation for Openness
MCMed("b5 * c2", object = fit_OB_model)  # IS



OB_model_x <- 'CON ~ a1 * C + a2 * E + a3 * ES + a4 * A
               IS ~ b1 * C + b2 * E + b3 * ES + b5 * O
               Eff ~ c1 * CON + c2 * IS + c3 * ES
               CON ~~ IS'

fit_OB_model_x <- sem(OB_model_x, sample.cov = cor_FFM_model, 
                      sample.nobs = harmonic.mean(c(N_FFM_model)))
summary(fit_OB_model_x, standardized = TRUE, fit.measures = TRUE)
