Load and clean data

library(tidyverse)
library(janitor)
library(here)

set.seed(1234)

constraint_levels <- function(x) {
  filter(constraints, constraint == x) %>% 
    pull(levels_clean) %>%
    .[[1]]
}

constraints <- tribble(
  ~constraint, ~constraint_clean, ~levels_clean,
  "create_network", "Network creation", list("Network creation" = "T", "No network creation" = "F"),
  "select", "Selection", list("Selection" = "T", "No selection" = "F"),
  "disperse", "Dispersal", list("Dispersal" = "T", "No dispersal" = "F"),
  "compete", "Competition", list("Competition" = "T", "No competition" = "F"),
  "selectfor_d", "Selection for D", list("Selection" = "T", "No selection" = "F"),
  "catastrophe", "Catastrophe", list("Catastrophe" = "T", "No catastrophe" = "F")
)


# Tmy_raw <- read_csv(here("data", "raw_data", "My19ccLS.csv"),
#                     col_types = cols(`create_networkyn?` = col_factor(levels = c("T", "F")),
#                                      `selectyn?` = col_factor(levels = c("T", "F")),
#                                      `disperseyn?` = col_factor(levels = c("T", "F")),
#                                      `competeyn?` = col_factor(levels = c("T", "F")),
#                                      `selectforDyn?` = col_factor(levels = c("T", "F")),
#                                      `catastropheyn?` = col_factor(levels = c("T", "F"))))

saveRDS(constraints, here("data", "derived_data", "constraints.rds"))


BHL <- read_rds(here("data", "raw_data", "BHL.rds")) %>% 
  clean_names(case = "snake") %>%   # Get rid of invalid characters in column names
  rename_all(funs(str_remove_all(., "yn$"))) %>%  # Get rid of "yn" in column names
  # Clean up constraint values
  mutate(create_network = fct_recode(create_network, !!!constraint_levels("create_network")),
         select = fct_recode(select, !!!constraint_levels("select")),
         disperse = fct_recode(disperse, !!!constraint_levels("disperse")),
         compete = fct_recode(compete, !!!constraint_levels("compete")),
         selectfor_d = fct_recode(selectfor_d, !!!constraint_levels("selectfor_d")),
         catastrophe = fct_recode(catastrophe, !!!constraint_levels("catastrophe"))) %>% 
  mutate_at(vars(one_of(constraints$constraint)), funs(fct_inorder(.))) %>% 
  mutate(repp = factor(repp, levels = c(1, 2), labels = c("High", "Low"), ordered = TRUE))

saveRDS(BHL, here("data", "derived_data", "BHL.rds"))

BHL %>% 
  sample_frac(size = 0.1) %>% 
  saveRDS(., here("data", "derived_data", "BHL_small.rds"))


BLS <- read_rds(here("data", "raw_data", "BLS.rds")) %>% 
  clean_names(case = "snake") %>%   # Get rid of invalid characters in column names
  rename_all(funs(str_remove_all(., "yn$"))) %>%  # Get rid of "yn" in column names
  # Clean up constraint values
  mutate(create_network = fct_recode(create_network, !!!constraint_levels("create_network")),
         select = fct_recode(select, !!!constraint_levels("select")),
         disperse = fct_recode(disperse, !!!constraint_levels("disperse")),
         compete = fct_recode(compete, !!!constraint_levels("compete")),
         selectfor_d = fct_recode(selectfor_d, !!!constraint_levels("selectfor_d")),
         catastrophe = fct_recode(catastrophe, !!!constraint_levels("catastrophe"))) %>% 
  mutate_at(vars(one_of(constraints$constraint)), funs(fct_inorder(.)))

saveRDS(BLS, here("data", "derived_data", "BLS.rds"))

BLS %>% 
  sample_frac(size = 0.1) %>% 
  saveRDS(., here("data", "derived_data", "BLS_small.rds"))
LS0tCnRpdGxlOiAiQ2xlYW4gZGF0YSIKYXV0aG9yOiAiU3RldmVuIEwuIFBlY2sgYW5kIEFuZHJldyBIZWlzcyIKZGF0ZTogIkxhc3QgcnVuOiBgciBmb3JtYXQoU3lzLnRpbWUoKSwgJyVCICVlLCAlWScpYCIKb3V0cHV0OiAKICBodG1sX2RvY3VtZW50OgogICAgY29kZV9mb2xkaW5nOiBzaG93CmVkaXRvcl9vcHRpb25zOiAKICBjaHVua19vdXRwdXRfdHlwZTogY29uc29sZQotLS0KCiMgTG9hZCBhbmQgY2xlYW4gZGF0YQoKYGBge3IgY2xlYW4tZGF0YSwgd2FybmluZz1GQUxTRSwgbWVzc2FnZT1GQUxTRSwgY2FjaGU9VFJVRX0KbGlicmFyeSh0aWR5dmVyc2UpCmxpYnJhcnkoamFuaXRvcikKbGlicmFyeShoZXJlKQoKc2V0LnNlZWQoMTIzNCkKCmNvbnN0cmFpbnRfbGV2ZWxzIDwtIGZ1bmN0aW9uKHgpI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pLAojICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBgc2VsZWN0eW4/YCA9IGNvbF9mYWN0b3IobGV2ZWxzID0gYygiVCIsICJGIikpLAojICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBgZGlzcGVyc2V5bj9gID0gY29sX2ZhY3RvcihsZXZlbHMgPSBjKCJUIiwgIkYiKSksCiMgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGBjb21wZXRleW4/YCA9IGNvbF9mYWN0b3IobGV2ZWxzID0gYygiVCIsICJGIikpLAojICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBgc2VsZWN0Zm9yRHluP2AgPSBjb2xfZmFjdG9yKGxldmVscyA9IGMoIlQiLCAiRiIpKSwKIyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYGNhdGFzdHJvcGhleW4/YCA9IGNvbF9mYWN0b3IobGV2ZWxzID0gYygiVCIsICJGIikpKSkKCnNhdmVSRFMoY29uc3RyYWludHMsIGhlcmUoImRhdGEiLCAiZGVyaXZlZF9kYXRhIiwgImNvbnN0cmFpbnRzLnJkcyIpKQoKCkJITCA8LSByZWFkX3JkcyhoZXJlKCJkYXRhIiwgInJhd19kYXRhIiwgIkJITC5yZHMiKSkgJT4lIAogIGNsZWFuX25hbWVzKGNhc2UgPSAic25ha2UiKSAlPiUgICAjIEdldCByaWQgb2YgaW52YWxpZCBjaGFyYWN0ZXJzIGluIGNvbHVtbiBuYW1lcwogIHJlbmFtZV9hbGwoZnVucyhzdHJfcmVtb3ZlX2FsbCguLCAieW4kIikpKSAlPiUgICMgR2V0IHJpZCBvZiAieW4iIGluIGNvbHVtbiBuYW1lcwogICMgQ2xlYW4gdXAgY29uc3RyYWludCB2YWx1ZXMKICBtdXRhdGUoY3JlYXRlX25ldHdvcmsgPSBmY3RfcmVjb2RlKGNyZWF0ZV9uZXR3b3JrLCAhISFjb25zdHJhaW50X2xldmVscygiY3Jl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