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

#### FFM - Second Order meta-analysis
data_FFM$Country_New <- data_FFM$Country
data_FFM$Country_New[which(data_FFM$Country_New == "mixed")] <- "Mixed"

data_FFM$Country_New[which(data_FFM$Country %in% c("international sample", "Mixed (Swiss, Swedish, and USA)",
                                                   "USA and Canada","U.S.A & Canada",
                                                   "usa, switzerland, sweden",
                                                   "Mixed (6 Different Countries)"))] <- "Mixed"

data_FFM_emg <- data_FFM %>% filter(DV == 1 & INCLUDE_IN_ANALYSIS == 1)
data_FFM_eff <- data_FFM %>% filter(INCLUDE_AS_JUDGE_OP == 1)


# We check for values of ProVar (Or alternatively, r_rhorho) to see if level-3 variability exists.
# ProVar values are generally not large.
country_result_emg <- BF_analysis(data_FFM_emg, mod = "Country_New", IV_name = "IV")
calculation_secondary(country_result_emg$mod_result$C$results)   # 15.20%
calculation_secondary(country_result_emg$mod_result$A$results)   # 4.33%
calculation_secondary(country_result_emg$mod_result$N$results)   # 33.96%
calculation_secondary(country_result_emg$mod_result$O$results)   # 17.30%
calculation_secondary(country_result_emg$mod_result$E$results)   # 33.13%

country_result_eff <- BF_analysis(data_FFM_eff, mod = "Country_New", IV_name = "IV")
calculation_secondary(country_result_eff$mod_result$C$results)   # 3.48%
calculation_secondary(country_result_eff$mod_result$A$results)   # 5.79%
calculation_secondary(country_result_eff$mod_result$N$results)   # 1.61%
calculation_secondary(country_result_eff$mod_result$O$results)   # 15.73%
calculation_secondary(country_result_eff$mod_result$E$results)   # 2.66%


#### HEXACO - Second Order meta-analysis
data_HEXACO$Country_New <- data_HEXACO$Country
data_HEXACO$Country_New[which(data_HEXACO$Country_New == "mixed (USA & Canada)")] <- "mixed"
data_HEXACO$Country_New[which(data_HEXACO$Country_New == "Canada + US + UK")] <- "mixed"

data_HEXACO_emg <- data_HEXACO %>% filter(DV == 1 & INCLUDE_TYPE == 1)
data_HEXACO_eff <- data_HEXACO %>% filter(INCLUDE_AS_JUDGE_OP == 1)

country_result_emg_HEXACO <- HEX_analysis(data_HEXACO_emg, mod = "Country_New", IV_name = "IV")
calculation_secondary(country_result_emg_HEXACO$mod_result$H$results)   # US Only
calculation_secondary(country_result_emg_HEXACO$mod_result$E$results)   # 85.50%
calculation_secondary(country_result_emg_HEXACO$mod_result$X$results)   # US Only
calculation_secondary(country_result_emg_HEXACO$mod_result$A$results)   # US Only
calculation_secondary(country_result_emg_HEXACO$mod_result$C$results)   # US Only
calculation_secondary(country_result_emg_HEXACO$mod_result$O$results)   # US Only

country_result_eff_HEXACO <- HEX_analysis(data_HEXACO_eff, mod = "Country_New", IV_name = "IV")
calculation_secondary(country_result_eff_HEXACO$mod_result$H$results)   # 3.22%
calculation_secondary(country_result_eff_HEXACO$mod_result$E$results)   # US Only
calculation_secondary(country_result_eff_HEXACO$mod_result$X$results)   # US Only
calculation_secondary(country_result_eff_HEXACO$mod_result$A$results)   # US Only
calculation_secondary(country_result_eff_HEXACO$mod_result$C$results)   # US Only
calculation_secondary(country_result_eff_HEXACO$mod_result$O$results)   # US Only


##### HLM Analysis #####
#### FFM
####### Emergence #######
HLM_emg_null <- BF_analysis_HLM(data_FFM_emg, mods = NULL, 
                                cluster_var = "Country_New", 
                                ind_var = "Coding ID", IV_name = "IV")
# Proportion of level 3 variance
var.comp(HLM_emg_null$O$result)$results    # 0
var.comp(HLM_emg_null$C$result)$results    # 68.15%
var.comp(HLM_emg_null$E$result)$results    # 0
var.comp(HLM_emg_null$A$result)$results    # 35.65%
var.comp(HLM_emg_null$N$result)$results    # 0


####### Effectiveness #######
HLM_eff_null <- BF_analysis_HLM(data_FFM_eff, mods = NULL, 
                                cluster_var = "Country_New", 
                                ind_var = "Coding ID", IV_name = "IV")
var.comp(HLM_eff_null$O$result)$results    # 60.62%
var.comp(HLM_eff_null$C$result)$results    # 20.74%
var.comp(HLM_eff_null$E$result)$results    # 59.53%
var.comp(HLM_eff_null$A$result)$results    # 89.24%
var.comp(HLM_eff_null$N$result)$results    # 54.28%



#### HEXACO
HLM_eff_HEXACO_null <- HEX_analysis_HLM(data_HEXACO_eff, mods = NULL,
                                        cluster_var = "Country_New",
                                        ind_var = "Coding ID", IV_name = "IV")
var.comp(HLM_eff_HEXACO_null$H$result)$results    # 51.98%

HLM_emg_HEXACO_null <- HEX_analysis_HLM(data_HEXACO_emg, mods = NULL,
                                        cluster_var = "Country_New",
                                        ind_var = "Coding ID", IV_name = "IV")
var.comp(HLM_emg_HEXACO_null$H$result)$results    # 0

