rm(list = ls())

############################## MASS - MAIN EFFECTS #############################


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 unimputed results as dummy host model
load("data/uganda_citizen_imputed_full.RData")
dumMod <- subset(ug.imp.full, imp==0) #Create dummy host model as a placeholder 

# Load Results
results <- load("results/mass_maineffects_results_appendix.RData")

# Load Variable Labels
varInfo <- read.csv("data/mass_analysis_varinfo.csv", stringsAsFactors = F)


## Create necessary QOIs for ggplot
alpha <- 0.05
final.results$uci_95 <- final.results$est + qnorm(alpha/2)*final.results$se
final.results$lci_95 <- final.results$est - qnorm(alpha/2)*final.results$se
alpha <- 0.10
final.results$uci_90 <- final.results$est + qnorm(alpha/2)*final.results$se
final.results$lci_90 <- final.results$est - qnorm(alpha/2)*final.results$se
final.results$iv <- gsub("ngo", "NGO", final.results$iv)
final.results$iv <- gsub("Ngo", "NGO", final.results$iv)
final.results$iv <- gsub("nontax_direct", "Aid/Oil", final.results$iv)
final.results$reference_category <- gsub("non-ngo", "non-NGO", final.results$reference_category)
final.results$reference_category <- gsub("ngo", "NGO", final.results$reference_category)
final.results$reference_category <- gsub("non-tax", "non-Tax", final.results$reference_category)
final.results$iv <- gsub(":", " X ", final.results$iv)
final.results$iv <- gsub("oil_region", "Oil Reg", final.results$iv)
final.results$iv <- gsub("gov_supp", "Gov Supp", final.results$iv)
final.results$iv <- gsub("coethnic_pres", "Coethnic Pres", final.results$iv)
final.results$iv <- gsub("noncoethnic", "Noncoethnic", final.results$iv)
final.results$iv <- gsub("type_high", "High Type", final.results$iv)
final.results$iv <- gsub("female", "Female", final.results$iv)
final.results$iv <- gsub("perf_high", "High Approval", final.results$iv)
final.results$iv <- gsub("perf_low", "Low Approval", final.results$iv)
final.results$iv <- gsub("corr_high", "High Corruption", final.results$iv)
final.results$iv <- gsub("trust_high", "High Trust", final.results$iv)
final.results$iv <- gsub("trust_low", "Low Trust", final.results$iv)
final.results$iv <- gsub("poverty_high", "High Poverty", final.results$iv)
final.results$iv <- gsub("poverty_govsupp", "High Poverty-Gov Supp", final.results$iv)
final.results$iv <- gsub("NGO_hightrust", "High Trust-NGOs", final.results$iv)
final.results$comparison <- paste(capitalize(final.results$iv), "Treatment,",
                                  capitalize(final.results$reference_category), "Control")
final.results$comparison <- gsub("Ngo", "NGO", final.results$comparison)

## MI Estimates (Confidence Intervals)
alpha <- 0.05
final.results$mi_uci_95 <- final.results$mi_est + qnorm(alpha/2)*final.results$mi_se
final.results$mi_lci_95 <- final.results$mi_est - qnorm(alpha/2)*final.results$mi_se
alpha <- 0.10
final.results$mi_uci_90 <- final.results$mi_est + qnorm(alpha/2)*final.results$mi_se
final.results$mi_lci_90 <- final.results$mi_est - qnorm(alpha/2)*final.results$mi_se


###################################################################################################################
################ 5. Mass Surveys, Main Results (Unimputed, Passed Manipulation Check)  ############################
###################################################################################################################


######  S6. Action Index, Main Results (Unimputed, Passed Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model

tab.s6 <- 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:mass:action:unimp:mchk", 
          omit.stat = c("all"),
          title  = "Action Index, Main Results (Unimputed, Passed Manipulation Check)",
          multicolumn = F, 
          model.numbers = T)

tab.s6 <- star_notes_tex(tab.s6, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s6, file = paste0(getwd(),"/tables/s6.tex"),sep="\n") 
              

######  Table S7. Action Index, NGO Treatment, non-NGO Control (Unimputed, Passed Manipulation Check)

plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s7 <- 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  = "Action Index, NGO Pooled Results (Unimputed, Passed Manipulation Check)",
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:mass:action:unimp:mchk", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


#### Table S8. Benefit Index, Main Results (Unimputed, Passed Manipulation Check)

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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Oil Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s8 <- 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"), 
          title  = "Benefit Index, Main Results (Unimputed, Passed Manipulation Check)",
          label = "tab:mass:benefit:unimp:mchk", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s8 <- star_notes_tex(tab.s8, note.type = "threeparttable", 
                         note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s8, file = paste0(getwd(),"/tables/s8.tex"),sep="\n") 



## Table S9. Benefit Index, NGO Treatment, non-NGO Control (Unimputed, Passed Manipulation Check)
###### Benefit Measures, Main Results Unimputed (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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s9 <-  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 Treat-Non-NGO Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:benefit:ngo:unimp:mchk", 
          omit.stat = c("all"),
          title = "Benefit Index, NGO Pooled Results (Unimputed, Passed Manipulation Check)",
          multicolumn = F, 
          model.numbers = T)

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

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




## Table S10. Misappropriation Index, Main Results (Unimputed, Passed Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s10 <-  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"), 
          title = "Misappropriation Index, Main Results (Unimputed, Passed Manipulation Check)",
          label = "tab:misapp:unimp:mchk", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s10 <- star_notes_tex(tab.s10, note.type = "threeparttable", 
                         note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s10, file = paste0(getwd(),"/tables/s10.tex"),sep="\n") 


## Table S11. Misappropriation Index, NGO Treatment, non-NGO Control (Unimputed, Passed Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s11 <- 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 Treat- Non-NGO Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          title = "Misappropriation Index, NGO Pooled Results (Unimputed, Passed Manipulation Check)",
          label = "tab:misapp:ngo:unimp:mchk", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


######################################################################################
## Appendix Section 6. Mass Surveys, Main Results (Unimputed, Full Sample)
######################################################################################

###### Table S12. Action Index, Main Results (Unimputed, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s12 <- 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"), 
          title = "Action Index, Main Results (Unimputed, Full Sample)",
          label = "tab:action:unimp:full", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s12 <- star_notes_tex(tab.s12, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s12, file = paste0(getwd(),"/tables/s12.tex"),sep="\n") 


#### Table S13. Action Index, NGO Treatment, non-NGO Control (Unimputed, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s13 <-  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 Treat, Non-NGO Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:unimpactionngofull", 
          title = "Action Index, NGO Pooled Results (Unimputed, Full Sample)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


### Table S14. Benefit Index, Main Results (Unimputed, Full Sample)
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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Oil Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.results$dv %in% plotVars, ]

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s14 <-  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:unimpbenefitfull", 
          omit.stat = c("all"),
          title = "Benefit Index, Main Results (Unimputed, Full Sample)",
          multicolumn = F, 
          model.numbers = T)

tab.s14 <- star_notes_tex(tab.s14, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s14, file = paste0(getwd(),"/tables/s14.tex"),sep="\n") 


###### Table S15. Benefit Index, NGO Treatment, non-NGO Control (Unimputed, Full Sample)

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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s15 <-  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 Treat, Non-NGO Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          title = "Benefit Index, NGO Pooled Results (Unimputed, Full Sample)",
          label = "tab:unimpbenefitngofull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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



######  Table S16. Misappropriation Index, Main Results (Unimputed, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s16 <- 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"), 
          title = "Misappropriation Index, Main Results (Unimputed, Full Sample)",
          label = "tab:unimpmisapfull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s16 <- star_notes_tex(tab.s16, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. 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.s16, file = paste0(getwd(),"/tables/s16.tex"),sep="\n") 

## Table S17. Misappropriation Index, NGO Treatment, non-NGO Control (Unimputed, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "no"
                        & final.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 = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s17 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          title = "Misappropriation Index, NGO Pooled Results (Unimputed, Full Sample)",
          label = "tab:unimpbenefitngofull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


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

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


####################################################################
########################### MI RESULTS  ############################
####################################################################
## Appendix Section 7. Mass Surveys, Main Results (Imputed, Passed Manipulation Check)

###### Table S18. Action Index, Main Results (MI, Manipulation Check)

plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s18 <- 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"), 
          title = "Action Index, Main Results (MI, Manipulation Check)", 
          label = "tab:impaction", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s18 <- star_notes_tex(tab.s18, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for only respondents who passed the manipulation check. 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.s18, file = paste0(getwd(),"/tables/s18.tex"),sep="\n") 


###### Table S19. Action Index, NGO Treatment, non-NGO Control (MI, Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s19 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"),
          title = "Action Index, NGO Pooled Results (MI, Manipulation Check)", 
          label = "tab:impactionngo", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


### Table S20. Benefit Index, Main Results (MI, Manipulation Check)
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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Oil Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s20 <- 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"), 
          title = "Benefit Index, Main Results (MI, Manipulation Check)", 
          label = "tab:impbenefit", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s20 <- star_notes_tex(tab.s20, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for only respondents who passed the manipulation check. 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.s20, file = paste0(getwd(),"/tables/s20.tex"),sep="\n") 


###### Table S21. Benefit Index, NGO Treatment, non-NGO Control (MI, Manipulation Check)
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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s21 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:unimpbenefitngo", 
          title = "Benefit Index, NGO Pooled Resluts (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


###### Table S22. Misappropriation Index, Main Results (MI, Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s22 <- 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:impmisap", 
          title = "Misappropriation Index, Main Results (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s22 <- star_notes_tex(tab.s22, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for only respondents who passed the manipulation check. 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.s22, file = paste0(getwd(),"/tables/s22.tex"),sep="\n") 




###### Table S23. Misappropriation Index, NGO Treatment, non-NGO Control (MI, Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s23 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"),
          title = "Misappropriation Index, NGO Pooled Results (MI, Manipulation Check)", 
          label = "tab:impbenefitngo", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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

     
         
########################################################################################################################################
########################### LM RESULTS, MAIN EFFECTS - IMPUTED (No MANIPULATION CHECK)  ############################
########################################################################################################################################
### Appendix Section 8. Mass Surveys, Main Results (Imputed, Full Sample)

###### Table S24. Action Index, Main Results (MI, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s24 <- 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"), 
          title  = "Action Index, Main Results (MI, Full Sample)", 
          label = "tab:impactionfull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


tab.s24 <- star_notes_tex(tab.s24, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for the full sample. 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.s24, file = paste0(getwd(),"/tables/s24.tex"),sep="\n") 


###### Table S25. Action Index, NGO Treatment, non-NGO Control (MI, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="action"]
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=="action"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s25 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          title = "Action Index, NGO Pooled Results (MI, Full Sample)", 
          label = "tab:impactionngofull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


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

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


###### Table S26. Benefit Index, Main Results (MI, Full Sample)
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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Oil Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.results$dv %in% plotVars, ]

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s26 <- 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:unimpbenefit", 
          title = "Benefit Index, Main Results (MI, Full Sample)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s26 <- star_notes_tex(tab.s26, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for the full sample. 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.s26, file = paste0(getwd(),"/tables/s26.tex"),sep="\n") 


###### Table S27. Benefit Index, NGO Treatment, non-NGO Control (MI, Full Sample)
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 <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s27 <- 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"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          title = "Benefit Index, NGO Pooled Index (MI, Full Sample)",
          label = "tab:impbenefitngofull", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


###### Table S28. Misappropriation Index, Main Effects (MI, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

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

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

taxoil <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("Tax Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s28 <- 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:impmisapfull", 
          title = "Misappropriation Index, Main Effects (MI, Full Sample)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s28 <- star_notes_tex(tab.s28, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects over five imputed datasets for the full sample. 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.s28, file = paste0(getwd(),"/tables/s28.tex"),sep="\n") 


##### Table S29. Misappropriation Index, Main Effects (MI, Full Sample)
plotVars <- varInfo$var_name[varInfo$include_table == 1 & varInfo$var_index=="misappropriation"]
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=="misappropriation"]

ngonon <- final.results[final.results$model == "fe"
                        & final.results$comparison %in% c("NGO Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "no"
                        & final.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]]$mi_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]]$mi_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]]$mi_p[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s29 <- 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 Treat, Non-NGO Ctr"), 
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:impbenefitngofull", 
          title = "Misappropriation Index, Main Effects (MI, Full Sample)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

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

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


