diff_cols <- c("difficulty_schl_services", "difficulty_hsptl_services",
"difficulty_id_services", "difficulty_police_services")
respect_cols <- c("feel_rspect_schl", "feel_rspect_hsptl", "feel_rspect_id")
df <- train_set %>%
mutate(
# 1. Overall Service Utilization (1 = Yes, 0 = No)
# Checks if any of the columns are NOT "no contact/answer"
service_utilization = as.integer(if_any(all_of(diff_cols), ~ . != "no contact/answer" & !is.na(.))),
# 2. Convert text to numbers for Difficulty (creates temporary columns)
across(all_of(diff_cols),
~ case_when(
. == "very easy" ~ 1,
. == "easy" ~ 2,
. == "difficult" ~ 3,
. == "very difficult" ~ 4,
TRUE ~ NA_real_ # Everything else, including "no contact", becomes NA
), .names = "{.col}_num"),
# 3. Convert text to numbers for Respect (creates temporary columns)
across(all_of(respect_cols),
~ case_when(
. == "not at all" ~ 1,
. == "a little bit" ~ 2,
. == "somewhat" ~ 3,
. == "a lot" ~ 4,
TRUE ~ NA_real_
), .names = "{.col}_num")
) %>%
# 4. Calculate the row means, ignoring NAs
rowwise() %>%
mutate(
avg_difficulty = mean(c_across(ends_with("_num") & contains("difficulty")), na.rm = TRUE),
avg_respect = mean(c_across(ends_with("_num") & contains("rspect")), na.rm = TRUE)
) %>%
ungroup() %>%
# Drop the temporary numeric columns to keep the dataframe clean
select(-ends_with("_num"), -starts_with("diff"), -starts_with("feel"))
df <- df %>%
mutate(
avg_diff_model = if_else(is.na(avg_difficulty), 0, avg_difficulty),
avg_resp_model = if_else(is.na(avg_respect), 0, avg_respect)
) %>% select(-avg_difficulty, -avg_respect)
##------Trust Transformation-------##
df_mlvl <- df
##----Creation of country level variables have information on the whole dataset----------##
df_mlvl$service_utilization <- as.factor(ifelse(df_mlvl$service_utilization == 1, "yes", "no"))
#df_mlvl <- df_mlvl %>% mutate(across(where(is.numeric), ~as.numeric(scale(.))))
df_mlvl$service_utilization <- ifelse(df_mlvl$service_utilization == "yes", 1, 0)
col_num <- colnames(df_mlvl %>% select_if(~class(.) == "numeric"))
df_mlvl$Interpersonal_Index <- rowMeans(df_mlvl[, c("trst_relative", "trst_neighbours", "trst_people_other_religion", "trst_other_people_know")], na.rm=TRUE)
df_mlvl$Institutional_Index <- rowMeans(df_mlvl[, c("trst_pres", "trst_police", "trst_courts")], na.rm=TRUE)
df_mlvl <- df_mlvl %>%
mutate(
language_profile = case_when(
lang_home == "colonial" & lang_interview == "colonial" ~ "Consistent Colonial",
lang_home == "indigeneous/others" & lang_interview == "indigeneous/others" ~ "Consistent Indigenous",
(lang_home == "colonial" & lang_interview == "indigeneous/others") |
(lang_home == "indigeneous/others" & lang_interview == "colonial") ~ "Mixed/Code-Switcher",
TRUE ~ NA_character_
)
)
civic_cols <- c("attnd_cmty_meeting", "raise_issue", "protest_march")
df_mlvl <- df_mlvl %>%
mutate(
# 1. Convert text to numbers using case_when (creates temporary columns)
across(all_of(civic_cols),
~ case_when(
. == "Active" ~ 3,
. == "Willing" ~ 2,
. == "Resistant" ~ 1,
TRUE ~ NA_real_ # "No Answer" and anything else becomes NA
), .names = "{.col}_num")
) %>%
# 2. Calculate the row means, ignoring NAs
rowwise() %>%
mutate(
civic_participation_score = mean(c_across(ends_with("_num") & contains(c("attnd", "raise", "protest"))), na.rm = TRUE)
) %>%
ungroup() %>%
# 3. Clean up by dropping the temporary numeric columns
select(-ends_with("_num"))
df_mlvl$lang_policy <- fct_collapse(
df_mlvl$indigenous_lang_integration,
"no integration" = "no integration",
"limited integration" = "limited integration",
"moderate or extensive" = c("moderate integration", "extensive integration")
)
#####_______FIN______###
train_set <- df_mlvl %>% mutate(across(where(~class(.) == "character"), ~as.factor(.))) %>%
select(hesitancy, language_profile, avg_diff_model, avg_resp_model, service_utilization,
Interpersonal_Index, Institutional_Index, civic_participation_score, gndr, age, urban_rural, lvl_edu, religion,
lpi,ctry, lang_policy)