
## Source a function to make equations (to remove clutter)

rm(list = ls())
require(stargazer)
require(starpolishr)

make.equation <- function(dvs, ivs, interactions = c()){
  return(paste(dvs, "~", paste(c(ivs, interactions), sep = "", collapse = " + ")))
}

load("data/ghana_citizen_imputed_manip.RData")
load("data/uganda_citizen_imputed_manip.RData")

ug.imp.manip <- subset(ug.imp.manip, imp==0)  ## Subset to unimputed sample
gh.imp.manip <- subset(gh.imp.manip, imp==0)

dataList <- list(ug.imp.manip, gh.imp.manip)

###########################################################################################
##                                      Balance Tests
###########################################################################################

treatVar <- list("tax", "aid", "oil", "ngo") #treatment DVs
minControls <- c("age", "employed", "female", "enum_rural", "edu_self ") #minimal covariates
fullControls <- c("noncoethnic", "trust_low", "perf_low", "oil_region", "gov_supp", "corr_high") #subgroup covars
fixedEffects <- c("as.factor(enum_id)") #enumerator fixed effects

#Lists to save model objects
ugMin <- list()
ugFull <- list()
ghMin <- list()
ghFull <- list()

for(i in 1:length(treatVar)) {
  
  ugMin[[i]] <- glm(make.equation(treatVar[[i]], c(minControls, fixedEffects)), family=binomial(link="logit"), data=ug.imp.manip)
  ugFull[[i]] <- glm(make.equation(treatVar[[i]], c(minControls, fullControls, fixedEffects)), family=binomial(link="logit"), data=ug.imp.manip)
  
  ghMin[[i]] <- glm(make.equation(treatVar[[i]], c(minControls, fixedEffects)), family=binomial(link="logit"), data=gh.imp.manip)
  ghFull[[i]] <- glm(make.equation(treatVar[[i]], c(minControls, fullControls, fixedEffects)), family=binomial(link="logit"), data=gh.imp.manip)

  }

  names(ugMin) <- treatVar
  names(ugFull) <- treatVar
  names(ghMin) <- treatVar
  names(ghFull) <- treatVar
  
  covariateLabs <- c("Age", "Employed", "Female", "Rural", "Education", "Non-Coethnic", "Low Trust", "Low Approval", "Oil Region", "Gov Supporter", "High Corruption", "Constant")

  ## Table S2.Uganda Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (Tax, Aid, Oil)
  tab.s2 <- stargazer(ugMin["tax"], ugFull["tax"], ugMin["aid"], ugFull["aid"], ugMin["oil"], ugFull["oil"], 
            omit="factor\\(enum", type="latex", covariate.labels = covariateLabs, 
            title="Uganda Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (Tax, Aid, Oil)",
            label = "balance:uganda", 
            dep.var.labels.include = F, 
            column.labels = c("Tax", "Tax", "Aid", "Aid", "Oil", "Oil"),
            multicolumn = F, 
            model.numbers = T)
  
  tab.s2 <- star_notes_tex(tab.s2, note.type = "threeparttable", 
                             note ="Estimates are based on logistic regression with enumerator fixed effects. Standard errors in parentheses.") 
  
  cat(tab.s2, file = paste0(getwd(),"/tables/s2.tex"),sep="\n")
    
  
  ## Table S3. Uganda Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (NGO)"
  tab.s3 <- stargazer(ugMin["ngo"], ugFull["ngo"],
            omit="factor\\(enum", type="latex", covariate.labels = covariateLabs,
            title="Uganda Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (NGO)",
            label = "balance:uganda:ngo", 
            dep.var.labels.include = F, 
            column.labels = c("NGO", "NGO"),
            multicolumn = F, 
            model.numbers = T)
  
  tab.s3 <- star_notes_tex(tab.s3, note.type = "threeparttable", 
                           note ="Estimates are based on logistic regression with enumerator fixed effects. Standard errors in parentheses.") 
  
  cat(tab.s3, file = paste0(getwd(),"/tables/s3.tex"),sep="\n")

  ## Table S4. Ghana Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (Tax, Aid, Oil)
  tab.s4 <- stargazer(ghMin["tax"], ghFull["tax"], ghMin["aid"], ghFull["aid"], ghFull["oil"], ghFull["oil"], 
            omit="factor\\(enum", type="latex", covariate.labels = covariateLabs, 
            title="Ghana Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (Tax, Aid, Oil)",
            label = "balance:ghana", 
            dep.var.labels.include = F, 
            column.labels = c("Tax", "Tax", "Aid", "Aid", "Oil", "Oil"),
            multicolumn = F, 
            model.numbers = T)

  tab.s4 <- star_notes_tex(tab.s4, note.type = "threeparttable", 
                           note ="Estimates are based on logistic regression with enumerator fixed effects. Standard errors in parentheses.") 
  
  cat(tab.s4, file = paste0(getwd(),"/tables/s4.tex"),sep="\n")  
  
  ## Table S5. Ghana Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (NGO)
  tab.s5 <- stargazer(ghMin["ngo"], ghFull["ngo"],
            omit="factor\\(enum", type="latex", covariate.labels = covariateLabs, 
            title="Ghana Mass Survey (Passed Manipulation Check): Balance Tests with Enum FEs (NGO)",
            label = "balance:ghana:ngo", 
            dep.var.labels.include = F, 
            column.labels = c("NGO", "NGO"),
            multicolumn = F, 
            model.numbers = T)
  
  tab.s5 <- star_notes_tex(tab.s5, note.type = "threeparttable", 
                           note ="Estimates are based on logistic regression with enumerator fixed effects. Standard errors in parentheses.") 
  
  cat(tab.s5, file = paste0(getwd(),"/tables/s5.tex"),sep="\n") 
  
  
  