rm(list = ls())

############################## MP MAIN EFFECTS TABLES #############################


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


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

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

load("results/mp_lm_results_maintext.RData")


load("data/uganda_mp_full.RData")
dumMod <- d.ug.mp

varInfo <- read.csv("data/mp_analysis_varinfo.csv", stringsAsFactors = F)

## Create necessary QOIs for ggplot

alpha <- 0.05
lm.results$uci_95 <- lm.results$est + qnorm(alpha/2)*lm.results$se
lm.results$lci_95 <- lm.results$est - qnorm(alpha/2)*lm.results$se
alpha <- 0.10
lm.results$uci_90 <- lm.results$est + qnorm(alpha/2)*lm.results$se
lm.results$lci_90 <- lm.results$est - qnorm(alpha/2)*lm.results$se
lm.results$iv <- gsub("ngo", "NGO", lm.results$iv)
lm.results$iv <- gsub("Ngo", "NGO", lm.results$iv)
lm.results$iv <- gsub("nontax_direct", "Aid/Oil", lm.results$iv)
lm.results$reference_category <- gsub("non-ngo", "non-NGO", lm.results$reference_category)
lm.results$reference_category <- gsub("ngo", "NGO", lm.results$reference_category)
lm.results$reference_category <- gsub("non-tax", "non-Tax", lm.results$reference_category)
lm.results$iv <- gsub(":", " X ", lm.results$iv)
lm.results$iv <- gsub("services_poor", "Poor Serv", lm.results$iv)
lm.results$iv <- gsub("corr_high", "High Corr", lm.results$iv)
lm.results$iv <- gsub("female", "Female", lm.results$iv)
lm.results$iv <- gsub("mp_ruling", "Ruling Party", lm.results$iv)
lm.results$comparison <- paste(capitalize(lm.results$iv), "Treatment,",
                               capitalize(lm.results$reference_category), "Control")
lm.results$comparison <- gsub("Ngo", "NGO", lm.results$comparison)

###### Benefit Measures, MP Results 
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="benefit"]
var_group <- c(plotVars) # Include all variable names you want to report
names(var_group) <- varInfo$var_label[varInfo$include_table == 1 & varInfo$var_index=="benefit"]

oilaid <- lm.results[lm.results$comparison %in% c("Oil Treatment, Aid Control")
                        & lm.results$model == "fe"
                        & lm.results$var_group == "benefit"
                        & lm.results$dv %in% plotVars, ]

taxaid <- lm.results[lm.results$comparison %in% c("Tax Treatment, Aid Control")
                        & lm.results$model == "fe"
                        & lm.results$var_group == "benefit"
                        & lm.results$dv %in% plotVars, ]

taxoil <- lm.results[lm.results$comparison %in% c("Tax Treatment, Oil Control")
                        & lm.results$model == "fe"
                        & lm.results$var_group == "benefit"
                        & lm.results$dv %in% plotVars, ]

res.list <- list(oilaid[oilaid$country == "uganda", ], 
                 oilaid[oilaid$country == "ghana", ],
                 taxaid[taxaid$country == "uganda", ],
                 taxaid[taxaid$country == "ghana", ],
                 taxoil[taxoil$country == "uganda", ],
                 taxoil[taxoil$country == "ghana", ])

coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$est[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.loop
}


se.list <- list()
for(j in 1:length(res.list)){
  se.loop <- c()
  for(i in 1:length(var_group)){
    se.loop <- c(se.loop, res.list[[j]]$se[res.list[[j]]$dv == var_group[i]])
  }
  names(se.loop) <- var_group
  se.list[[j]] <- se.loop
}

p.list <- list()
for(j in 1:length(res.list)){
  p.loop <- c()
  for(i in 1:length(var_group)){
    p.loop <- c(p.loop, res.list[[j]]$p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "tax", ivs = var_group), data = dumMod) # -1 to account for intercept in host model

#Table S86. MP Benefit Index, Enumerator FEs
tab.s86 <- stargazer(host.model, host.model, host.model, host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = se.list, 
          p = p.list,
          intercept.bottom = F,
          column.separate = c(2, 2, 2),
          column.labels = c("Oil Treat-Aid Ctr", "Tax Treat-Aid Ctr", "Tax Treat - Oil Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana", "Uganda", "Ghana", "Uganda", "Ghana"), 
          label = "tab:mp:benefit", 
          omit.stat = c("all"),
          title  = "MP Benefit Index, Enumerator FEs",
          multicolumn = F, 
          model.numbers = T)

tab.s86 <- star_notes_tex(tab.s86, note.type = "threeparttable", 
                          note ="Columns 1, 3, and 5 report the Ugandan results while Columns 2, 4, and 6 report the Ghanaian estimates. Standard errors in parentheses.") 

cat(tab.s86, file = paste0(getwd(),"/tables/s86.tex"),sep="\n") 



###### Benefit Measures, MP Results (NGO)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="benefit"]
var_group <- c(plotVars) # Include all variable names you want to report
names(var_group) <- varInfo$var_label[varInfo$include_table == 1 & varInfo$var_index=="benefit"]

ngonon <- lm.results[lm.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & lm.results$var_group == "benefit"
                        & lm.results$model == "fe"
                        & lm.results$dv %in% plotVars, ]


res.list <- list(ngonon[ngonon$country == "uganda", ], 
                 ngonon[ngonon$country == "ghana", ])


coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$est[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.loop
}


se.list <- list()
for(j in 1:length(res.list)){
  se.loop <- c()
  for(i in 1:length(var_group)){
    se.loop <- c(se.loop, res.list[[j]]$se[res.list[[j]]$dv == var_group[i]])
  }
  names(se.loop) <- var_group
  se.list[[j]] <- se.loop
}

p.list <- list()
for(j in 1:length(res.list)){
  p.loop <- c()
  for(i in 1:length(var_group)){
    p.loop <- c(p.loop, res.list[[j]]$p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "tax", ivs = var_group), data = dumMod) # -1 to account for intercept in host model

# Table S87. Benefit Index, NGO Treatment-Non-NGO Control Results
tab.s87 <- stargazer(host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = se.list, 
          p = p.list,
          intercept.bottom = F,
          column.separate = c(2),
          column.labels = c("NGO Treatment, Non-NGO Control"), 
          title  = "Benefit Index, NGO Treatment-Non-NGO Control Results",
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:mp:benefit:ngo", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s87 <- star_notes_tex(tab.s87, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Column 1 reports the Ugandan results while Column 2 report the Ghanaian estimates. Standard errors in parentheses.") 

cat(tab.s87, file = paste0(getwd(),"/tables/s87.tex"),sep="\n") 


###### Influence Measures, MP Results 
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="influence"]
var_group <- c(plotVars) # Include all variable names you want to report
names(var_group) <- varInfo$var_label[varInfo$include_table == 1 & varInfo$var_index=="influence"]

oilaid <- lm.results[lm.results$comparison %in% c("Oil Treatment, Aid Control")
                        & lm.results$var_group == "influence"
                        & lm.results$model == "fe"
                        & lm.results$dv %in% plotVars, ]

taxaid <- lm.results[lm.results$comparison %in% c("Tax Treatment, Aid Control")
                        & lm.results$var_group == "influence"
                        & lm.results$model == "fe"
                        & lm.results$dv %in% plotVars, ]

taxoil <- lm.results[lm.results$comparison %in% c("Tax Treatment, Oil Control")
                        & lm.results$var_group == "influence"
                        & lm.results$model == "fe"
                        & lm.results$dv %in% plotVars, ]

res.list <- list(oilaid[oilaid$country == "uganda", ], 
                 oilaid[oilaid$country == "ghana", ],
                 taxaid[taxaid$country == "uganda", ],
                 taxaid[taxaid$country == "ghana", ],
                 taxoil[taxoil$country == "uganda", ],
                 taxoil[taxoil$country == "ghana", ])

coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$est[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.loop
}


se.list <- list()
for(j in 1:length(res.list)){
  se.loop <- c()
  for(i in 1:length(var_group)){
    se.loop <- c(se.loop, res.list[[j]]$se[res.list[[j]]$dv == var_group[i]])
  }
  names(se.loop) <- var_group
  se.list[[j]] <- se.loop
}

p.list <- list()
for(j in 1:length(res.list)){
  p.loop <- c()
  for(i in 1:length(var_group)){
    p.loop <- c(p.loop, res.list[[j]]$p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "tax", ivs = var_group), data = dumMod) # -1 to account for intercept in host model

# Table S88. MP Influence Index, Enumerator FEs 
tab.s88 <- stargazer(host.model, host.model, host.model, host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = se.list, 
          p = p.list,
          intercept.bottom = F,
          column.separate = c(2, 2, 2),
          column.labels = c("Oil Treat-Aid Ctr", "Tax Treat-Aid Ctr", "Tax Treat - Oil Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana", "Uganda", "Ghana", "Uganda", "Ghana"), 
          label = "tab:mp:influence", 
          omit.stat = c("all"),
          title  = "MP Influence Index, Enumerator FEs",
          multicolumn = F, 
          model.numbers = T)

tab.s88 <- star_notes_tex(tab.s88, note.type = "threeparttable", 
                          note ="Columns 1, 3, and 5 report the Ugandan results while Columns 2, 4, and 6 report the Ghanaian estimates. Standard errors in parentheses.") 

cat(tab.s88, file = paste0(getwd(),"/tables/s88.tex"),sep="\n") 



###### Benefit Measures, MP Results (NGO)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="influence"]
var_group <- c(plotVars) # Include all variable names you want to report
names(var_group) <- varInfo$var_label[varInfo$include_table == 1 & varInfo$var_index=="influence"]

ngonon <- lm.results[lm.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & lm.results$var_group == "influence"
                        & lm.results$model == "fe"
                        & lm.results$dv %in% plotVars, ]


res.list <- list(ngonon[ngonon$country == "uganda", ], 
                 ngonon[ngonon$country == "ghana", ])


coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$est[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.loop
}


se.list <- list()
for(j in 1:length(res.list)){
  se.loop <- c()
  for(i in 1:length(var_group)){
    se.loop <- c(se.loop, res.list[[j]]$se[res.list[[j]]$dv == var_group[i]])
  }
  names(se.loop) <- var_group
  se.list[[j]] <- se.loop
}

p.list <- list()
for(j in 1:length(res.list)){
  p.loop <- c()
  for(i in 1:length(var_group)){
    p.loop <- c(p.loop, res.list[[j]]$p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "tax", ivs = var_group), data = dumMod) # -1 to account for intercept in host model

# Table S89. MP Influence Index, NGO Treatment-Non-NGO Control Results
tab.s89 <-  stargazer(host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = se.list, 
          p = p.list,
          intercept.bottom = F,
          column.separate = c(2),
          column.labels = c("NGO Treatment, Non-NGO Control"), 
          title  = "Influence Index, NGO Treatment-Non-NGO Control Results",
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:mp:influence:ngo", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


tab.s89 <- star_notes_tex(tab.s89, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Column 1 reports the Ugandan results while Column 2 report the Ghanaian estimates. Standard errors in parentheses.") 

cat(tab.s89, file = paste0(getwd(),"/tables/s89.tex"),sep="\n") 

