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

# Load data
gcd = read_dta(path_to("gcd_raw"))

# Load currency calibration data
currency = read.csv2(path_to("currency_calibration"))
rownames(currency) = currency$Country.Code.ISO3


##################################################
################ GALLUP VARS #####################
##################################################
# Rename columns
gcd = plyr::rename(gcd, c("COUNTRY_ISO3"="iso3", "countrynew"="country"))

# Gender
gcd$female = as.numeric(mapvalues(gcd$WP1219, from=c(1, 2), to=c(0, 1)))
var_label(gcd$female) = "Female gender (dummy, takes value 1 for female respondents)"
gcd$WP1219 = NULL

# Age
gcd$age = as.numeric(mapvalues(gcd$WP1220, from=c(100), to=c(NA)))
gcd$age20 = gcd$age / 20
var_label(gcd$age) = "Age (numeric) (99 represents 99+, which rarely occurs)"
var_label(gcd$age20) = "Age (numeric) in 20 years"
gcd$WP1220 = NULL

# Household income: Per capita
gcd$income_pc_log = log(gcd$INCOME_4 + 1)
var_label(gcd$income_pc_log) = "Log. income (numeric); based on INCOME_4 (PPP adjusted $ income per household member); log(1+INCOME_4) to avoid log(0)"
gcd$INCOME_4 = NULL

# Education
gcd$education = mapvalues(gcd$WP3117, from=c(4,5), to=c(NA,NA))
var_label(gcd$education) = "Education level (categorical)"
gcd$education_lev1 = 1*(gcd$education == 1)
gcd$education_lev2 = 1*(gcd$education == 2)
gcd$education_lev3 = 1*(gcd$education == 3)
gcd$education_lev2_or_higher = 1*(gcd$education == 2 | gcd$education == 3)
var_label(gcd$education_lev1) = "Education level. Dummy. First Gallup level."
var_label(gcd$education_lev2) = "Education level. Dummy. Second Gallup level."
var_label(gcd$education_lev3) = "Education level. Dummy. Third Gallup level."
var_label(gcd$education_lev2_or_higher) = "Education level. Dummy. Second OR third Gallup level."
gcd$WP3117 = NULL

# Urban / rural
gcd$urban_rural = mapvalues(gcd$WP14, from=c(1,2,3,4,5,6), to=c(1,2,4,NA,NA,3))
lab = c(1:4)
names(lab) = names(attributes(gcd$WP14)$labels[c(1,2,6,3)])
gcd$urban_rural = set_labels(gcd$urban_rural, labels=lab)
var_label(gcd$urban_rural) = "Urban-rural (categorical), larger values indicate higher urbanicity"
gcd$urban = 1 * (gcd$urban_rural == 3 | gcd$urban_rural == 4) 
var_label(gcd$urban) = "Dummy for urban_rural level 3 and 4 (suburb of large city or large city)"
gcd$WP14 = NULL


##################################################
################ COOPERATION #####################
##################################################
# Experiment: hypothetical version
gcd$exp_hypo = 1*!is.na(gcd$RNDM_CLIMATE_ALT)
var_label(gcd$exp_hypo) = "Participated in hypothetical version of experiment (dummy)"

# Experiment: Participated
gcd$exp_part = 1*(gcd$exp_hypo | (gcd$WP22208 == 1))
var_label(gcd$exp_part) = "Consented to participate in experiment (dummy, always 1 for hypothetical version)"
gcd$WP22208 = NULL

# Experiment: Treatment
gcd$treatment = 1*ifelse(
  gcd$exp_hypo,
  gcd$RNDM_CLIMATE_ALT == 1,
  gcd$RNDM_CLIMATE == 1
)
var_label(gcd$treatment) = "Info treatment (dummy, 1 for treated respondents)"
gcd$RNDM_CLIMATE_ALT = NULL
gcd$RNDM_CLIMATE = NULL

# Experiment: Decision
gcd$coop = 1*ifelse(
  gcd$exp_hypo,
  mapvalues(gcd$WP22209_ALT, from=c(1, 2, 8, 9), to=c(0, 1, NA, NA)),
  mapvalues(gcd$WP22209, from=c(1, 2, 8, 9), to=c(0, 1, NA, NA))
)
var_label(gcd$coop) = "Cooperates in experiment (dummy)"
gcd$WP22209_ALT = NULL
gcd$WP22209 = NULL

# Experiment: Belief
gcd$coop_belief = as.numeric(ifelse(
  gcd$exp_hypo,
  mapvalues(gcd$WP22210_ALT, from=c(998, 999), to=c(NA, NA)),
  mapvalues(gcd$WP22210, from=c(998, 999), to=c(NA, NA))
))/100
var_label(gcd$coop_belief) = "Belief: %-share of respondents in own country who coop. in experiment (numeric, 0-1)"
gcd$WP22210_ALT = NULL
gcd$WP22210 = NULL


##################################################
################ PREFERENCES #######################
##################################################
# Preferences: Risk-taking
gcd$risktaking = as.numeric(mapvalues(gcd$WP22198, from=c(98, 99), to=c(NA, NA)))
var_label(gcd$risktaking) = "Risk-taking preference (scale from 0 to 10)"
gcd$WP22198 = NULL

# Preferences: Patience
gcd$patience = as.numeric(mapvalues(gcd$WP22200, from=c(1, 2, 8, 9), to=c(0, 1, NA, NA)))
var_label(gcd$patience) = "Patience preference (dummy, takes value 1 for respondents who prefer future payment)"
gcd$WP22200 = NULL

# Preferences: Altruism
# There's one respondent who contributes more than 100%. Data error. Solution: Cap at 100%.
if (any(!gcd$iso3 %in% currency$Country.Code.ISO3)) {message("Altruism: iso3 problem.")}
gcd$altruism = as.numeric(mapvalues(gcd$WP22199, from=c(9999998, 9999999), to=c(NA, NA)))
gcd$altruism = gcd$altruism / currency[gcd$iso3, "Q2"]
gcd$altruism = pmin(gcd$altruism, 1)
var_label(gcd$altruism) = "Altruism (% share of endowment donated)"
gcd$WP22199 = NULL

# Public good preference: Willingness to contribute money to fight CC
gcd$pg_preference = as.numeric(mapvalues(gcd$WP22203, from=c(1, 2, 8, 9), to=c(1, 0, NA, NA)))
var_label(gcd$pg_preference) = "Preference for public good: Willing to contribute 1% to fight CC (dummy)"
gcd$WP22203 = gcd$WP22204 = NULL

# Norm: fight climate change
gcd$norm = as.numeric(mapvalues(gcd$WP22206, from=c(1, 2, 8, 9), to=c(1, 0, NA, NA)))
var_label(gcd$norm) = "Norm: people in own country should fight CC (dummy). DK is also set to 0."
gcd$WP22206 = NULL


##################################################
################ EXPORT ##########################
##################################################
saveRDS(gcd, path_to("gcd"))

