##################################################
################ PREPARATION #####################
##################################################
# Clear environment
rm(list=setdiff(ls(), "path_to"))

# Load data
gcd = readRDS(path_to("gcd")) %>% filter(!is.na(coop))
world = readRDS(path_to("world"))

# Shorten column names, if necessary
abbr = colnames(world) %>% abbreviate(32)
abbr[abbr != colnames(world)]
colnames(world) = abbr

# Release data
gcd_release = gcd %>% dplyr::select(
  wpid, iso3, country,
  coop, coop_belief, coop_belief_std, treatment,
  norm, norm_std, risktaking, risktaking_std, patience, patience_std,
  altruism, altruism_std, pg_preference,
  female, age, age20, education, education_lev1, education_lev2,
  education_lev3, education_lev2_or_higher, income_pc_log,
  exp_hypo, wgt_expfull, wgt_expfull_pop15, wgt_expctrl, wgt_expctrl_pop15
)
world_release = world %>% dplyr::select(c(
  iso3:australia_and_new_zealand,
  gcd_coop_wgtexpctrl, gcd_coop_belief_wgtexpctrl, gcd_norm_wgtexpctrl,
  gcd_altruism_std_wgtexpctrl, gcd_risktaking_std_wgtexpctrl, gcd_patience_std_wgtexpctrl,
  gcd_education_lev2, gcd_education_lev3,
  starts_with("biregcoeff_"),
  starts_with("biregp_"),
  qogcs_wdi_lfpfilo15:wgt_pop15
))


##################################################
################ EXPORT ##########################
##################################################
saveRDS(gcd, path_to("gcd"))
write_dta(gcd, path_to("data_out", "gcd.dta"))
#write_dta(gcd_release, "../global_cooperation_data.dta")
write_dta(world, path_to("data_out", "world.dta"))
#write_dta(world_release, "../country_data.dta")

