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


# Leadership Emergence - Main Effect --------------------------------------
data_FFM_emg <- data_FFM %>% filter(DV == 1 & INCLUDE_IN_ANALYSIS == 1)
FFM_emg_results <- BF_analysis(data_FFM_emg, IV_name = "IV", 
                               var_names = list(rxx = "rxx", ryy = "ryy", rxy = "rxy", N = "N"))
format_file(FFM_emg_results$overall[c("E","A","C","N","O")])


# Leadership Emergence - Multiple R ---------------------------------------
## We use our own intercorrelations and EMG-FFM correlations now!
cor_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.22, 0.11, 0.15, 0.16, 0.16, 1))

colnames(cor_FFM_emg) <- c("E", "A", "C", "ES", "O", "Emg")


## Our corresponding N for intercorrelations, and EMG-FFM N ------------------
N_FFM_emg <- c(24919, 
               23902, 22478, 
               23289, 21686, 22675, 
               22922, 22904, 20018, 21869,
               19870, 13921, 25288, 15358, 13679)

emg_model <- 'Emg ~ E + A + C + ES + O'
emg_fit <- sem(emg_model, sample.cov = cor_FFM_emg, 
               sample.nobs = psych::harmonic.mean(N_FFM_emg))  # 20202.03
summary(emg_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
summary(emg_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.065299249)



# Leadership Effectiveness Unit Performance --------------------------------------
data_FFM_unit <- data_FFM %>% filter(DV == 3 & INCLUDE_IN_ANALYSIS == 1)
FFM_unit_results <- BF_analysis(data_FFM_unit, IV_name = "IV", 
                               var_names = list(rxx = "rxx", ryy = "ryy", rxy = "rxy", N = "N"))
format_file(FFM_unit_results$overall[c("E","A","C","N","O")]) 

## We use our own intercorrelations and PERF-FFM correlations now!
cor_perf_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.12, 0.10, 0.15, 0.17, 0.23, 1))

colnames(cor_perf_eff) <- c("E", "A", "C", "ES", "O", "Perf")


## Our corresponding N for intercorrelations, and PERF-FFM N ------------------
N_perf_eff <- c(24919, 
               23902, 22478, 
               23289, 21686, 22675, 
               22922, 22904, 20018, 21869,
               3220, 2917, 1800, 1828, 2043)

perf_model <- 'Perf ~ E + A + C + ES + O'
perf_fit <- sem(perf_model, sample.cov = cor_perf_eff, 
                sample.nobs = psych::harmonic.mean(N_perf_eff))  # 5580.291
summary(perf_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
summary(perf_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.07443807)

# Leadership Effectiveness - Main Effect ---------------------------------------
data_FFM_eff <- data_FFM %>% filter(INCLUDE_AS_JUDGE_OP == 1)
FFM_eff_results <- BF_analysis(data_FFM_eff, IV_name = "IV", 
                               var_names = list(rxx = "rxx", ryy = "ryy", rxy = "rxy", N = "N"))

format_file(FFM_eff_results$overall[c("E","A","C","N","O")]) 


## We use our own intercorrelations and EFF-FFM correlations now!
cor_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.11, 0.13, 0.12, 0.11, 0.16, 1))

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


## Our corresponding N for intercorrelations, and EFF-FFM N ------------------
N_FFM_eff <- c(24919, 
               23902, 22478, 
               23289, 21686, 22675, 
               22922, 22904, 20018, 21869,
               37449, 34663, 33537, 19868, 15641)

eff_model <- 'Eff ~ E + A + C + ES + O'
eff_fit <- sem(eff_model, sample.cov = cor_FFM_eff, 
               sample.nobs = psych::harmonic.mean(N_FFM_eff))  # 23356.71
summary(eff_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)
summary(eff_fit, fit.measures = TRUE, standardized = TRUE, rsquare = TRUE)[[6]]
sqrt(0.03978918)



