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


############# This file produces results analyzing the moderating effect of categorical moderators #############

###### Main effect for FFM #############################

data_FFM_emg <- data_FFM %>% filter(DV == 1 & INCLUDE_IN_ANALYSIS == 1)
FFM_order <- c("E","A","C","N","O")

# Collectivism. Median split is used.
data_FFM_emg <- data_FFM_emg %>% group_by(IV) %>% mutate(S = median(Collectivism, na.rm = TRUE), split_on = Collectivism > S) %>% ungroup()
emg_mod_coll <- BF_analysis(data_FFM_emg, mod = "split_on", IV_name = "IV")$mod_result
# data_FFM_eff %>% filter(Collectivism_Code == 1) %>% select(Country)
format_file_mods(emg_mod_coll, order = FFM_order, write = FALSE)


# 
# Type of organization.
# 1 - Social Service, 2 - Education, 3 - Business, 5 - Military, 6 - Mixed
emg_mod_org <- BF_analysis(data_FFM_emg, mod = "mod_organization", IV_name = "IV")$mod_result
type_of_organization <- format_file_mods(emg_mod_org, order = FFM_order, write = FALSE)
type_of_organization

# 
# # Hierarchical level.
# # 1 - Lower level supervisors, 2 - Middle managers, 3 - Upper level managers, 4 - Mixed or unclear
# emg_mod_hier <- BF_analysis(data_FFM_emg, mod = "mod_hierarchicallevel", IV_name = "IV")$mod_result
# hierarchical_level <- format_file_mods(emg_mod_hier, order = FFM_order, write = TRUE, filename = "emg_hier.csv")
# 
# # Study setting.
# # 1 - Laboratory, 2 - Organization, 3 - Classroom
emg_mod_setting <- BF_analysis(data_FFM_emg, mod = "mod_setting", IV_name = "IV")$mod_result
study_setting <- format_file_mods(emg_mod_setting, order = FFM_order, write = FALSE)
study_setting 
# 
# # Social complexity.
# # 1 - Low complexity, 2 - High complexity
emg_mod_complexity <- BF_analysis(data_FFM_emg, mod = "mod_socialcomplexityoftask", IV_name = "IV")$mod_result
social_complexity <- format_file_mods(emg_mod_complexity, order = FFM_order, write = FALSE)
social_complexity
# # Length of interaction.
# # 1 - < 20 mins, 2 - > 20 mins, one interaction, 3 - More than one interaction, 4 - Mixed or not applicable
# # Category 4 not presented because many emergence studies happened to be not involving interaction contexts.
emg_mod_length_interaction <- BF_analysis(data_FFM_emg, mod = "mod_lengthofinteraction", IV_name = "IV")$mod_result
length_interaction <- format_file_mods(emg_mod_length_interaction, order = FFM_order, write = FALSE)
length_interaction
# 
# # Basis for appointing leader.
# # 1 - Random, 2 - Qualification, 3 - Emerged/Leaderless group, 4 - Mixed or unclear
emg_mod_appointleader <- BF_analysis(data_FFM_emg, mod = "mod_basisforlaboratoryleadership", IV_name = "IV")$mod_result
basis_appointment <- format_file_mods(emg_mod_appointleader, order = FFM_order, write = FALSE)
basis_appointment
# 
# # Measure of emergence.
# # 1 - Election, 2 - Questionnaire, 3 - Rank, 4 - Mixed or unclear
emg_mod_emgmeasure <- BF_analysis(data_FFM_emg, mod = "mod_emergencemeasure", IV_name = "IV")$mod_result
emg_measure <- format_file_mods(emg_mod_emgmeasure, order = FFM_order, write = FALSE)
emg_measure
# 
# # Publication type. In the original data file, theses and dissertations are separated. Merged into one.
data_FFM_emg$Pub <- ifelse(data_FFM_emg$`Publication code` == "J", "J", "D/T")
emg_mod_publication_type <- BF_analysis(data_FFM_emg, mod = "Pub", IV_name = "IV")$mod_result
publication_type <- format_file_mods(emg_mod_publication_type, order = FFM_order, write = FALSE)
publication_type
# 
# # Validated personality measure.
# # Yes - Validated FFM scale (See a list from Judge et al., 2013), Translation of these scales, Scales derived from these validated scales
# # No - None of the above
data_FFM_emg$VALID_IV <- ifelse(data_FFM_emg$mod_VALID_FFM %in% c(1, 2, 3), "Yes", "No")
emg_mod_IV_validated <- BF_analysis(data_FFM_emg, mod = "VALID_IV", IV_name = "IV")$mod_result
validated_IV <- format_file_mods(emg_mod_IV_validated, order = FFM_order, write = FALSE)
validated_IV
# 
# # Rating source of personality.
# # 1 - Leader self-report, 2 - Non self-report, 3 - Mixed
emg_mod_IVRater <- BF_analysis(data_FFM_emg, mod = "mod_IV_RATER", IV_name = "IV")$mod_result
IV_rater <- format_file_mods(emg_mod_IVRater, order = FFM_order, write = FALSE)
IV_rater
# 
# # Rating source of emergence
# # 1 - Leader self-report, 3 - Skip-level supervisor, 4 - Observer, 5 - Objective, 7 - Peer, 6 - Mixed or Unclear
emg_mod_DVRater <- BF_analysis(data_FFM_emg, mod = "mod_DV_RATER_SOURCE", IV_name = "IV")$mod_result
DV_rater <- format_file_mods(emg_mod_DVRater, order = FFM_order, write = FALSE)
DV_rater
# 
# # Common source
emg_mod_commonsource <- BF_analysis(data_FFM_emg, mod = "mod_common_source", IV_name = "IV")$mod_result
common_source <- format_file_mods(emg_mod_commonsource, order = FFM_order, write = FALSE)
common_source
# 
# # Research design
emg_mod_design <- BF_analysis(data_FFM_emg, mod = "mod_design", IV_name = "IV")$mod_result
research_design <- format_file_mods(emg_mod_design, order = FFM_order, write = FALSE)
research_design


#### Effectiveness ####
data_FFM_eff <- data_FFM %>% filter(INCLUDE_AS_JUDGE_OP == 1)
data_FFM_eff <- data_FFM_eff %>% group_by(IV) %>% mutate(S = median(Collectivism, na.rm = TRUE), split_on = Collectivism > S) %>% ungroup()

eff_mod_coll <- BF_analysis(data_FFM_eff, mod = "split_on", IV_name = "IV")$mod_result
# data_FFM_eff %>% filter(Collectivism_Code == 1) %>% select(Country)
format_file_mods(eff_mod_coll, order = FFM_order, write = FALSE)



# Type of organization.
# 1 - Social Service, 2 - Education, 3 - Business, 5 - Military, 6 - Mixed
eff_mod_org <- BF_analysis(data_FFM_eff, mod = "mod_organization", IV_name = "IV")$mod_result
type_of_organization_eff <- format_file_mods(eff_mod_org, order = FFM_order, write = FALSE)
type_of_organization_eff

# Hierarchical level.
# 1 - Lower level supervisors, 2 - Middle managers, 3 - Upper level managers, 4 - Mixed or unclear
eff_mod_hier <- BF_analysis(data_FFM_eff, mod = "mod_hierarchicallevel", IV_name = "IV")$mod_result
hierarchical_level_eff <- format_file_mods(eff_mod_hier, order = FFM_order, write = FALSE)
hierarchical_level_eff

# Study setting.
# 1 - Laboratory, 2 - Organization, 3 - Classroom
eff_mod_setting <- BF_analysis(data_FFM_eff, mod = "mod_setting", IV_name = "IV")$mod_result
study_setting_eff <- format_file_mods(eff_mod_setting, order = FFM_order, write = FALSE)
study_setting_eff

# Social complexity.
# 1 - Low complexity, 2 - High complexity
eff_mod_complexity <- BF_analysis(data_FFM_eff, mod = "mod_socialcomplexityoftask", IV_name = "IV")$mod_result
social_complexity_eff <- format_file_mods(eff_mod_complexity, order = FFM_order, write = FALSE)
social_complexity_eff

# Publication type. In the original data file, theses and dissertations are separated. Merged into one.
data_FFM_eff$Pub <- ifelse(data_FFM_eff$`Publication code` == "J", "J", "D/T")
eff_mod_publication_type <- BF_analysis(data_FFM_eff, mod = "Pub", IV_name = "IV")$mod_result
publication_type_eff <- format_file_mods(eff_mod_publication_type, order = FFM_order, write = FALSE)
publication_type_eff

# Validated personality measure.
# Yes - Validated FFM scale (See a list from Judge et al., 2013), Translation of these scales, Scales derived from these validated scales
# No - None of the above
data_FFM_eff$VALID_IV <- ifelse(data_FFM_eff$mod_VALID_FFM %in% c(1, 2, 3), "Yes", "No")
eff_mod_IV_validated <- BF_analysis(data_FFM_eff, mod = "VALID_IV", IV_name = "IV")$mod_result
validated_IV_eff <- format_file_mods(eff_mod_IV_validated, order = FFM_order, write = FALSE)
validated_IV_eff

# Rating source of personality.
# 1 - Leader self-report, 2 - Non self-report, 3 - Mixed
eff_mod_IVRater <- BF_analysis(data_FFM_eff, mod = "mod_IV_RATER", IV_name = "IV")$mod_result
IV_rater_eff <- format_file_mods(eff_mod_IVRater, order = FFM_order, write = FALSE)
IV_rater_eff

# Rating source of effectiveness.
# 1 - Leader self-report, 3 - Skip-level supervisor, 4 - Observer, 5 - Objective, 7 - Peer, 6 - Mixed or Unclear
eff_mod_DVRater <- BF_analysis(data_FFM_eff, mod = "mod_DV_RATER_SOURCE", IV_name = "IV")$mod_result
DV_rater_eff <- format_file_mods(eff_mod_DVRater, order = FFM_order, write = FALSE)
DV_rater_eff

# Common source
eff_mod_commonsource <- BF_analysis(data_FFM_eff, mod = "mod_common_source", IV_name = "IV")$mod_result
common_source_eff <- format_file_mods(eff_mod_commonsource, order = FFM_order, write = FALSE)
common_source_eff

# Research design
eff_mod_design <- BF_analysis(data_FFM_eff, mod = "mod_design", IV_name = "IV")$mod_result
research_design_eff <- format_file_mods(eff_mod_design, order = FFM_order, write = FALSE)
research_design_eff

###### Main effect for HEXACO ######
data_HEXACO_emg <- data_HEXACO %>% filter(DV == 1 & INCLUDE_TYPE == 1)
HEXACO_order <- c("H","E","X","A","C","O")
format_file(HEX_analysis(data_HEXACO_emg, IV_name = "IV")$overall)


data_HEXACO_eff <- data_HEXACO %>% filter(INCLUDE_AS_JUDGE_OP == 1)
format_file(HEX_analysis(data_HEXACO_eff, IV_name = "IV")$overall)


