rm(list = ls())

############################## MASS - SUBGROUP 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 = " + ")))
}

results <- load("results/mass_heteffects_results_appendix.RData")
load("data/uganda_citizen_imputed_full.RData")

dumMod <- subset(ug.imp.full, imp==0) #Create dummy host model as a placeholder 

varInfo <- read.csv("data/mass_analysis_varinfo.csv", stringsAsFactors = F)
final.results <- het.results

## 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


################################### Action Index ################################################

################################### Action, Oil vs. Aid ###################################

###### Table S30. Uganda: Action Index, Oil Treatment-Aid Control, Het. Effects Results (MI, Manipulation Chec
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="action"]

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

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

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

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


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


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


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

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

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

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Oil X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "action"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]


ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Oil X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "action"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])

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.s30 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low App", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:action:oilaid:het1", 
          title = "Uganda: Action Index, Oil Treatment-Aid Control, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s30 <- star_notes_tex(tab.s30, note.type = "threeparttable", 
                         note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

cat(tab.s30, file = paste0(getwd(),"/tables/s30.tex"),sep="\n")  
              
## Table S31. Uganda: Action Index, Oil Treatment-Aid Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s31 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "NGO Trust"), 
          label = "tab:ug:action:oilaid:het2", 
          title = "Uganda: Action Index, Oil Treatment-Aid Control II, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s31 <- star_notes_tex(tab.s31, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a government supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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

### Ghana Action Measures (Oil-Aid) Het Effects

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ],
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

## Table S32. Ghana: Action Index, Oil Treatment-Aid Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s32 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:action:oilaid:het1", 
          title = "Ghana: Action Index, Oil Treatment-Aid Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s32 <- star_notes_tex(tab.s32, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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

## Table S33. Ghana: Action Index, Oil Treatment-Aid Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s33 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:action:oilaid:het2", 
          title = "Ghana: Action Index, Oil Treatment-Aid Control II, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s33 <- star_notes_tex(tab.s33, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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

#################################### Action, Tax v.s Aid ####################################

###### Uganda Action (Tax-Aid) Measures, Het. Results Imputed 
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="action"]

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

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

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

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


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


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


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

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

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

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "action"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]


ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "action"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

## Table S34. Uganda: Action Index, Tax Treatment-Aid Control, Het. Effects Results (MI, Manipulation Check)## Uganda Action (Tax-Aid) Subgroups Het I
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s34 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corr"), 
          label = "tab:ug:action:taxaid:het1", 
          title = "Uganda: Action Index, Tax Treatment-Aid Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s34 <- star_notes_tex(tab.s34, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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


#Table S35. Uganda: Action Index, Tax Treatment-Aid Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s35 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Pov-Gov Supp", "NGOs Trust"), 
          label = "tab:ug:action:taxaid:het2", 
          title = "Uganda: Action Index, Tax Treatment-Aid Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s35 <- star_notes_tex(tab.s35, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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



###### Ghana Action (Tax-Aid) Measures, Het. Results Imputed -- GHANA

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S36. Ghana: Action Index, Tax Treatment-Aid Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s36 <- stargazer(host.model, host.model, 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[1:8], 
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:action:taxaid:het1", 
          title = "Ghana: Action Index, Tax Treatment-Aid Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


tab.s36 <- star_notes_tex(tab.s36, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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




# Table S37. Ghana: Action Index, Tax Treatment-Aid Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s37 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Pov-Gov Supp", "NGOs Trust"), 
          label = "tab:gh:action:taxaid:het2", 
          title = "Ghana: Action Index, Tax Treatment-Aid Control II, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s37 <- star_notes_tex(tab.s37, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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




###### Action Measures (Tax-Oil), Het. Results Imputed -- UGANDA
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="action"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Oil Reg Treatment, Oil Control")
                           & final.results$var_group == "action"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("Tax X Gov Supp Treatment, Oil Control")
                         & final.results$var_group == "action"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Female Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Type Treatment, Oil Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Low Approval Treatment, Oil Control")
                           & final.results$var_group == "action"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Low Trust Treatment, Oil Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X Noncoethnic Treatment, Oil Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Corruption Treatment, Oil Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("Tax X High Poverty Treatment, Oil Control") 
                             & final.results$var_group == "action"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Oil Control") 
                                 & final.results$var_group == "action"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]


ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Oil Control") 
                            & final.results$var_group == "action"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])

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
}

# Table S38. Uganda: Action Index, Tax Treatment-Oil Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s38 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:action:taxoil:het1", 
          title = "Uganda: Action Index, Tax Treatment-Oil Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s38 <- star_notes_tex(tab.s38, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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



# Uganda, Tax-Oil, Het. Effects #2
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s39 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "NGOs Trust"), 
          label = "tab:ug:action:taxoil:het1", 
          title = "Uganda: Action Index, Tax Treatment-Oil Control II, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s39 <- star_notes_tex(tab.s39, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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


###### Action Measures (Tax-Oil), Het. Results Imputed -- GHANA


res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

#Table S40. Ghana: Action Index, Tax Treatment-Oil Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s40 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:action:taxoil:het1", 
          title = "Ghana: Action Index, Tax Treatment-Oil Control, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s40 <- star_notes_tex(tab.s40, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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



#Table S41. Ghana: Action Index, Tax Treatment-Oil Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s41 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:action:taxoil:het2", 
          title = "Ghana: Action Index, Tax Treatment-Oil Control II, Het. Effects  Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s41 <- star_notes_tex(tab.s41, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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



############################## Action, NGO-non-NGO  ############################################################

###### Action Measures (NGO-Non NGO), Main Results Imputed -- UGANDA
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="action"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Oil Reg Treatment, Non-NGO Control")
                           & final.results$var_group == "action"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("NGO X Gov Supp Treatment, Non-NGO Control")
                         & final.results$var_group == "action"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Female Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Type Treatment, Non-NGO Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Low Approval Treatment, Non-NGO Control")
                           & final.results$var_group == "action"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Low Trust Treatment, Non-NGO Control")
                        & final.results$var_group == "action"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X Noncoethnic Treatment, Non-NGO Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Corruption Treatment, Non-NGO Control")
                          & final.results$var_group == "action"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("NGO X High Poverty Treatment, Non-NGO Control") 
                             & final.results$var_group == "action"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("NGO X High Poverty-Gov Supp Treatment, Non-NGO Control") 
                                 & final.results$var_group == "action"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("NGO X High Trust-NGOs Treatment, Non-NGO Control") 
                            & final.results$var_group == "action"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

#Table S42. Uganda: Action Index, NGO Treatment-Non-NGO Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s42 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:action:ngo:het1", 
          title = "Uganda: Action Index, NGO Treatment-Non-NGO Control, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s42 <- star_notes_tex(tab.s42, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).") 

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



#Table S43. Uganda: Action Index, NGO Treatment-Non-NGO Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s43 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:action:ngo:het2", 
          title = "Uganda: Action Index, NGO Treatment-Non-NGO Control II, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s43 <- star_notes_tex(tab.s43, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).") 

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


###### Action Measures, NGO-Non Het. Results Imputed -- GHANA

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

#Table S44. Ghana: Action Index, NGO Treatment-Non-NGO Control, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s44 <-  stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:action:ngonon:het1", 
          title = "Ghana: Action Index, NGO Treatment-Non-NGO Control, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s44 <- star_notes_tex(tab.s44, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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

#Table S45. Ghana: Action Index, NGO Treatment-Non-NGO Control II, Het. Effects Results (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s45 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:action:ngonon:het2", 
          title = "Ghana: Action Index, NGO Treatment-Non-NGO Control, Het. Effects Results (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s45 <- star_notes_tex(tab.s45, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


########## !!!!!!!!!!!!!!!!!!!!!!!!!  BENEFIT INDEX !!!!!!!!!!!!!!!!!! ################

#################################### Benefit, Oil-Aid ####################################

###### Uganda: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="benefit"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Oil X Oil Reg Treatment, Aid Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("Oil X Gov Supp Treatment, Aid Control")
                         & final.results$var_group == "benefit"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Oil X Female Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Oil X High Type Treatment, Aid Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Oil X Low Approval Treatment, Aid Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Oil X Low Trust Treatment, Aid Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Oil X Noncoethnic Treatment, Aid Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Oil X High Corruption Treatment, Aid Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("Oil X High Poverty Treatment, Aid Control") 
                             & final.results$var_group == "benefit"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Oil X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "benefit"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Oil X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "benefit"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

# Table S46. Uganda: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s46 <-  stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:benefit:oilaid:het1", 
          title = "Uganda: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s46 <- star_notes_tex(tab.s46, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S47. Uganda: Benefit Index Oil Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s47 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:benefit:oilaid:het2", 
          title = "Uganda: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s47 <- star_notes_tex(tab.s47, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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

### "Ghana: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

#Table S48. Ghana: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s48 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:benefit:oilaid:het1", 
          title = "Ghana: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s48 <- star_notes_tex(tab.s48, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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

#Table S49. Ghana: Benefit Index Oil Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s49 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:benefit:oilaid:het1", 
          title = "Ghana: Benefit Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s49 <- star_notes_tex(tab.s49, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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

###### Uganda: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="benefit"]

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

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

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

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


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


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

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

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

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

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "benefit"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "benefit"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])



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
}

#Table S50. Uganda: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s50 <-  stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:benefit:taxaid:het1",
          title = "Uganda: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s50 <- star_notes_tex(tab.s50, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


#Table S51. Uganda: Benefit Index Tax Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s51 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:benefit:taxaid:het2",
          title = "Uganda: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s51 <- star_notes_tex(tab.s51, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


###### Benefit Measures, Main Results Imputed -- GHANA

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

#Table S52. Ghana: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s52 <- stargazer(host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:benefit:taxaid:het1", 
          title = "Ghana: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s52 <- star_notes_tex(tab.s52, note.type = "threeparttable", 
                        note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


#Table S53. Ghana: Benefit Index Tax Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s53 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:benefit:taxaid:het2", 
          title = "Ghana: Benefit Index Tax Treatment - Aid Control (MI, Manipulation Check)", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s53 <- star_notes_tex(tab.s53, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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




############ Benefit - Treatment vs. Oil Control

###### Uganda: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)", 
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="benefit"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Oil Reg Treatment, Oil Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("Tax X Gov Supp Treatment, Oil Control")
                         & final.results$var_group == "benefit"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Female Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Type Treatment, Oil Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Low Approval Treatment, Oil Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Low Trust Treatment, Oil Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X Noncoethnic Treatment, Oil Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Corruption Treatment, Oil Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("Tax X High Poverty Treatment, Oil Control") 
                             & final.results$var_group == "benefit"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Oil Control") 
                                 & final.results$var_group == "benefit"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Oil Control") 
                            & final.results$var_group == "benefit"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

# Table S54. Uganda: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s54 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:benefit:taxoil:het1", 
          title = "Uganda: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s54 <- star_notes_tex(tab.s54, note.type = "threeparttable", 
                        note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S55. Uganda: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s55 <- stargazer(host.model, host.model, host.model,  style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:benefit:taxoil:het2", 
          title = "Uganda: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s55 <- star_notes_tex(tab.s55, note.type = "threeparttable", 
                        note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


###### Ghana: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S56. Ghana: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s56 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:benefit:taxoil:het1", 
          omit.stat = c("all"),
          title = "Ghana: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          multicolumn = F, 
          model.numbers = T)

tab.s56 <- star_notes_tex(tab.s56, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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



# Table S57. Ghana: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s57 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:benefit:taxoil:het2", 
          omit.stat = c("all"),
          title = "Ghana: Benefit Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          multicolumn = F, 
          model.numbers = T)

tab.s57 <- star_notes_tex(tab.s57, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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

############ Benefit, NGO-non

###### Uganda: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="benefit"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Oil Reg Treatment, Non-NGO Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("NGO X Gov Supp Treatment, Non-NGO Control")
                         & final.results$var_group == "benefit"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Female Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Type Treatment, Non-NGO Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Low Approval Treatment, Non-NGO Control")
                           & final.results$var_group == "benefit"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Low Trust Treatment, Non-NGO Control")
                        & final.results$var_group == "benefit"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X Noncoethnic Treatment, Non-NGO Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Corruption Treatment, Non-NGO Control")
                          & final.results$var_group == "benefit"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("NGO X High Poverty Treatment, Non-NGO Control") 
                             & final.results$var_group == "benefit"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("NGO X High Poverty-Gov Supp Treatment, Non-NGO Control") 
                                 & final.results$var_group == "benefit"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("NGO X High Trust-NGOs Treatment, Non-NGO Control") 
                            & final.results$var_group == "benefit"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

# Table S58. Uganda: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s58 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:benefit:ngo:het1", 
          title = "Uganda: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s58 <- star_notes_tex(tab.s58, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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



# Table S59. Uganda: Benefit Index NGO Treatment - non-NGO Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s59 <-  stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:benefit:ngo:het2", 
          title = "Uganda: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s59 <- star_notes_tex(tab.s59, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


###### Ghana: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

#Table S60. Ghana: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s60 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:benefit:ngo:het1", 
          title = "Ghana: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s60 <- star_notes_tex(tab.s60, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S61. Ghana: Benefit Index NGO Treatment - non-NGO Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s61 <-  stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:benefit:ngo:het2", 
          title = "Ghana: Benefit Index NGO Treatment - non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s61 <- star_notes_tex(tab.s61, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


########## !!!!!!!!!!!!!!!!!!!!!!!!!  Misappropriation INDEX !!!!!!!!!!!!!!!!!! ################

######  Uganda: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)",
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="misappropriation"]

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

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

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

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

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

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

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

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

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

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Oil X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "misappropriation"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Oil X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "misappropriation"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

# Table S62. Uganda: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s62 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:misapp:oilaid:het1", 
          title = "Uganda: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s62 <- star_notes_tex(tab.s62, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


#Table S63. Uganda: Misappropriation Index Oil Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s63 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:misapp:oilaid:het2", 
          title = "Uganda: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s63 <- star_notes_tex(tab.s63, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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




### Ghana: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S64. Ghana: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s64 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T,
          title = "Ghana: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)", 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:misapp:oilaid:het1", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s64 <- star_notes_tex(tab.s64, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


#Table S65. Ghana: Misappropriation Index Oil Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s65 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T,
          title = "Ghana: Misappropriation Index Oil Treatment - Aid Control (MI, Manipulation Check)", 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:misapp:oilaid:het2", 
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


tab.s65 <- star_notes_tex(tab.s65, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


######  Uganda: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)"
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="misappropriation"]

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

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

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

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

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

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

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

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

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

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Aid Control") 
                                 & final.results$var_group == "misappropriation"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Aid Control") 
                            & final.results$var_group == "misappropriation"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

# Table S66. Uganda: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s66 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:misapp:taxaid:het1", 
          title = "Uganda: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s66 <- star_notes_tex(tab.s66, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S67. Uganda: Misappropriation Index Tax Treatment - Aid Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s67 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:misapp:taxaid:het2", 
          title = "Uganda: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s67 <- star_notes_tex(tab.s67, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


###### "Ghana: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S68. Ghana: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s68 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:misapp:taxaid:het1", 
          title = "Ghana: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s68 <- star_notes_tex(tab.s68, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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

# Table S69. Ghana: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s69 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"),
          label = "tab:gh:misapp:taxaid:het2", 
          title = "Ghana: Misappropriation Index Tax Treatment - Aid Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s69 <- star_notes_tex(tab.s69, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


############ 

###### Uganda: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="misappropriation"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Oil Reg Treatment, Oil Control")
                           & final.results$var_group == "misappropriation"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("Tax X Gov Supp Treatment, Oil Control")
                         & final.results$var_group == "misappropriation"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Female Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Type Treatment, Oil Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("Tax X Low Approval Treatment, Oil Control")
                           & final.results$var_group == "misappropriation"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("Tax X Low Trust Treatment, Oil Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X Noncoethnic Treatment, Oil Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("Tax X High Corruption Treatment, Oil Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("Tax X High Poverty Treatment, Oil Control") 
                             & final.results$var_group == "misappropriation"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("Tax X High Poverty-Gov Supp Treatment, Oil Control") 
                                 & final.results$var_group == "misappropriation"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("Tax X High Trust-NGOs Treatment, Oil Control") 
                            & final.results$var_group == "misappropriation"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])



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
}

#Table S70. Uganda: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s70 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:ug:misapp:taxoil:het1",
          title = "Uganda: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)


tab.s70 <- star_notes_tex(tab.s70, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


#Table S71. Uganda: Misappropriation Index Tax Treatment - Oil Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s71 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:misapp:taxoil:het2",
          title = "Uganda: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s71 <- star_notes_tex(tab.s71, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


###### Ghana: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S72. Ghana: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s72 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corruption"), 
          label = "tab:gh:misapp:taxoil:het1", 
          title = "Ghana: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s72 <- star_notes_tex(tab.s72, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S73. Ghana: Misappropriation Index Tax Treatment - Oil Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s73 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:misapp:taxoil:het2", 
          title = "Ghana: Misappropriation Index Tax Treatment - Oil Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s73 <- star_notes_tex(tab.s73, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


############ 

###### Uganda: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",
plotVars <- varInfo$var_name[varInfo$include_plot == 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_plot == 1 & varInfo$var_index=="misappropriation"]

oilregion <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Oil Reg Treatment, Non-NGO Control")
                           & final.results$var_group == "misappropriation"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

govsupp <- final.results[final.results$model == "interaction"
                         & final.results$comparison %in% c("NGO X Gov Supp Treatment, Non-NGO Control")
                         & final.results$var_group == "misappropriation"
                         & final.results$manip == "yes"
                         & final.results$dv %in% plotVars, ]

gender <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Female Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

hightype <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Type Treatment, Non-NGO Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

lapproval <- final.results[final.results$model == "interaction"
                           & final.results$comparison %in% c("NGO X Low Approval Treatment, Non-NGO Control")
                           & final.results$var_group == "misappropriation"
                           & final.results$manip == "yes"
                           & final.results$dv %in% plotVars, ]

ltrust <- final.results[final.results$model == "interaction"
                        & final.results$comparison %in% c("NGO X Low Trust Treatment, Non-NGO Control")
                        & final.results$var_group == "misappropriation"
                        & final.results$manip == "yes"
                        & final.results$dv %in% plotVars, ]

noncoeth <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X Noncoethnic Treatment, Non-NGO Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highcorr <- final.results[final.results$model == "interaction"
                          & final.results$comparison %in% c("NGO X High Corruption Treatment, Non-NGO Control")
                          & final.results$var_group == "misappropriation"
                          & final.results$manip == "yes"
                          & final.results$dv %in% plotVars, ]

highpoverty <- final.results[final.results$model == "interaction"
                             & final.results$comparison %in% c("NGO X High Poverty Treatment, Non-NGO Control") 
                             & final.results$var_group == "misappropriation"
                             & final.results$manip == "yes"
                             & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("NGO X High Poverty-Gov Supp Treatment, Non-NGO Control") 
                                 & final.results$var_group == "misappropriation"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]

povertygovsupp <-  final.results[final.results$model == "interaction"
                                 & final.results$comparison %in% c("NGO X High Poverty-Gov Supp Treatment, Non-NGO Control") 
                                 & final.results$var_group == "misappropriation"
                                 & final.results$manip == "yes"
                                 & final.results$dv %in% plotVars, ]  

ngostrust <-  final.results[final.results$model == "interaction"
                            & final.results$comparison %in% c("NGO X High Trust-NGOs Treatment, Non-NGO Control") 
                            & final.results$var_group == "misappropriation"
                            & final.results$manip == "yes"
                            & final.results$dv %in% plotVars, ]  

res.list <- list(oilregion[oilregion$country == "uganda", ], 
                 govsupp[govsupp$country == "uganda", ],
                 gender[gender$country == "uganda", ],
                 hightype[hightype$country == "uganda", ],
                 lapproval[lapproval$country == "uganda", ],
                 ltrust[ltrust$country == "uganda", ],
                 noncoeth[noncoeth$country == "uganda", ],
                 highcorr[highcorr$country == "uganda", ], 
                 highpoverty[highpoverty$country == "uganda", ],
                 povertygovsupp[povertygovsupp$country == "uganda", ],
                 ngostrust[ngostrust$country == "uganda", ])


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
}

#Table S74. Uganda: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s74 <-  stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Nocoeth", "Corrupt"), 
          label = "tab:gh:misapp:ngo:het1", 
          title = "Uganda: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s74 <- star_notes_tex(tab.s74, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S75. Uganda: Misappropriation Index NGO Treatment - Non-NGO Control II (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s75 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:ug:misapp:ngo:het2", 
          title = "Uganda: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s75 <- star_notes_tex(tab.s75, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


###### "Ghana: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",

res.list <- list(oilregion[oilregion$country == "ghana", ], 
                 govsupp[govsupp$country == "ghana", ],
                 gender[gender$country == "ghana", ],
                 hightype[hightype$country == "ghana", ],
                 lapproval[lapproval$country == "ghana", ],
                 ltrust[ltrust$country == "ghana", ],
                 noncoeth[noncoeth$country == "ghana", ],
                 highcorr[highcorr$country == "ghana", ], 
                 highpoverty[highpoverty$country == "ghana", ],
                 povertygovsupp[povertygovsupp$country == "ghana", ],
                 ngostrust[ngostrust$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
}

# Table S76. Ghana: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s76 <- stargazer(host.model, host.model, 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[1:8],
          se = se.list[1:8], 
          p = p.list[1:8],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Oil Reg", "Gov Supp", "Female", "High Type", "Low Approval", "Low Trust", "Noncoeth", "Corrupt"), 
          label = "tab:gh:misapp:ngo:het1", 
          title = "Ghana: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s76 <- star_notes_tex(tab.s76, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects.  Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent resides in a high oil region (Oil Reg), was a government supporter (Gov Supp), is female, had experience paying taxes (High Type), did not approve of recent central government performance (Low Approval), reported low levels of trust in the government (Low Trust), was not a coethnic with the sitting president (Noncoeth), and reported high levels of corruption by politicians (Corrupt).")

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


# Table S77. Ghana: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)
host.model <- lm(make.equation(dvs = "type_high", ivs = var_group), data = dumMod) # -1 to account for intercept in host model
tab.s77 <- stargazer(host.model, host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant",  covariate.labels = c(names(var_group)), 
          coef = coef.list[9:11],
          se = se.list[9:11], 
          p = p.list[9:11],
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("High Poverty", "Poverty-Gov Supp", "Trust NGOs"), 
          label = "tab:gh:misapp:ngo:het2", 
          title = "Ghana: Misappropriation Index NGO Treatment - Non-NGO Control (MI, Manipulation Check)",
          omit.stat = c("all"),
          multicolumn = F, 
          model.numbers = T)

tab.s77 <- star_notes_tex(tab.s77, note.type = "threeparttable", 
                          note ="Estimates are based on Generalized Least Squares with enumerator fixed effects. Standard errors in parentheses. Reported estimates are interaction effects that test whether the effect of the treatment varies across potential moderations, including whether the respondent reported high levels of poverty in their area (High Poverty), was a governnment supporter and reported high levels of poverty (Poverty-Gov Supp), and expressed high levels of trust in NGOs (Trust NGOs).")

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


