
############################## MP - Randomization Inference #############################


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/mp_ri_results_appendix.RData")

load("data/uganda_mp_full.RData")
dumMod <- d.ug.mp #Create dummy host model as a placeholder 

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


###### - UGANDA (Benefit - NGO)
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"]

ngo <- ri.results[ri.results$treatment %in% c("ngo")
                     & ri.results$var_group == "benefit"
                     & ri.results$dv %in% plotVars, ]


res.list <- list(ngo[ngo$country == "uganda", ], 
                 ngo[ngo$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]]$estimate[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.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]]$two_tailed[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

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

#Table S107. Benefit Index - NGO Treatment, non-NGO Control, Randomization Inference
tab.s107 <- stargazer(host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = p.list, 
          p = p.list,
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:mp:benefit:ngo:ri", 
          omit.stat = c("all"),
          title  = "Benefit Index - NGO Treatment, non-NGO Control, Randomization Inferencee",
          multicolumn = F, 
          model.numbers = T)

tab.s107 <- star_notes_tex(tab.s107, note.type = "threeparttable", 
                           note ="Estimates are based on randomization inference with two-tailed p-values in parentheses. Column 1 reports the Ugandan results while Column 2 reports the Ghanaian estimates.") 

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

            
###### - UGANDA (Benefit - Tax-Aid)
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"]

oilaid <- ri.results[ri.results$treatment %in% c("oilaid")
                        & ri.results$var_group == "benefit"
                        & ri.results$dv %in% plotVars, ]

taxaid <- ri.results[ri.results$treatment %in% c("taxaid")
                        & ri.results$var_group == "benefit"
                        & ri.results$dv %in% plotVars, ]

taxoil <- ri.results[ri.results$treatment %in% c("taxoil")
                        & ri.results$var_group == "benefit"
                        & ri.results$dv %in% plotVars, ]

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

coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$estimate[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.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]]$two_tailed[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

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

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

tab.s106 <- star_notes_tex(tab.s106, note.type = "threeparttable", 
  note ="Estimates are based on randomization inference with two-tailed p-values in parentheses. Columns 1, 3, and 5 report the Ugandan results while Columns 2, 4, and 6 report the Ghanaian estimates.")   

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



######  (Influence - Tax-Aid)
plotVars <- varInfo$var_name[varInfo$include_plot == 1 & varInfo$var_index=="influence"]
var_group <- c(plotVars) # Include all variable names you want to report
names(var_group) <- varInfo$var_label[varInfo$include_plot == 1 & varInfo$var_index=="influence"]

oilaid <- ri.results[ri.results$treatment %in% c("oilaid")
                        & ri.results$var_group == "influence"
                        & ri.results$dv %in% plotVars, ]

taxaid <- ri.results[ri.results$treatment %in% c("taxaid")
                        & ri.results$var_group == "influence"
                        & ri.results$dv %in% plotVars, ]

taxoil <- ri.results[ri.results$treatment %in% c("taxoil")
                        & ri.results$var_group == "influence"
                        & ri.results$dv %in% plotVars, ]

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

coef.list <- list()
for(j in 1:length(res.list)){
  coef.loop <- c()
  for(i in 1:length(var_group)){
    coef.loop <- c(coef.loop, res.list[[j]]$estimate[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.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]]$two_tailed[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

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

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

tab.s108 <- star_notes_tex(tab.s108, note.type = "threeparttable", 
                           note ="Estimates are based on randomization inference with two-tailed p-values in parentheses. Columns 1, 3, and 5 report the Ugandan results while Columns 2, 4, and 6 report the Ghanaian estimates.") 

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


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

ngo <- ri.results[ri.results$treatment %in% c("ngo")
                     & ri.results$var_group == "influence"
                     & ri.results$dv %in% plotVars, ]


res.list <- list(ngo[ngo$country == "uganda", ], 
                 ngo[ngo$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]]$estimate[res.list[[j]]$dv == var_group[i]])
  }
  names(coef.loop) <- var_group
  coef.list[[j]] <- coef.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]]$two_tailed[res.list[[j]]$dv == var_group[i]])
  }
  names(p.loop) <- var_group
  p.list[[j]] <- p.loop
}

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

#Table S109. Influence Index - NGO Treatment, non-NGO Control, Randomization Inference
tab.s109 <- stargazer(host.model, host.model, style = "APSR", 
          colnames = T, omit="Constant", covariate.labels = c(names(var_group)), 
          coef = coef.list,
          se = p.list, 
          p = p.list,
          intercept.bottom = F,
          dep.var.labels.include = T, 
          dep.var.labels = c("Uganda", "Ghana"), 
          label = "tab:mp:influence:ngo:ri", 
          omit.stat = c("all"),
          title  = "Influence Index - NGO Treatment, non-NGO Control, Randomization Inference",
          multicolumn = F, 
          model.numbers = T)

tab.s109 <- star_notes_tex(tab.s109, note.type = "threeparttable", 
                           note ="Estimates are based on randomization inference with two-tailed p-values in parentheses. Column 1 reports the Ugandan results while Column 2 reports the Ghanaian estimates.") 

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


