### Tunisia Trade Imputation (AMELIA)
### 2018-09-06

### Start from clean workspace
rm(list = ls()) 

### Load libraries
library(Amelia)
library(readstata13)

### Set working directory 
MAIN_DIR <- ""

### Load unimputed data
tunisia.unimputed <- read.dta13(paste(MAIN_DIR,"data/ripe_tunisia_replication.dta", sep = ""), generate.factors=T)

### Set as data frame
tunisia.trade <- as.data.frame(tunisia.unimputed)

## Imputation Info
impInfo <- read.csv(paste(MAIN_DIR,"scripts/ripe_mi_vars.csv", sep = ""), stringsAsFactors = F)
numImp <- 5 #Set number of imputations

## Set random
set.seed(12345)

## Check to make sure all variables to be used in imputation are present in dataset
impUsing <- impInfo[impInfo$use_imputation == 1, ]
impUsing$var_name[!impUsing$var_name[impUsing$use_imputation == 1] %in% colnames(tunisia.trade)]
# Note: this should return character(0)
tunisiaUse <- tunisia.trade[, impUsing$var_name]
tunisiaUse[tunisiaUse < 0] <- NA

## Check the Number of Missing Values
NAs <- function(x) {
  as.vector(apply(x, 2, function(x) length(which(is.na(x)))))
}

NAs(tunisiaUse)
mean(NAs(tunisiaUse)/nrow(tunisiaUse))*100

# Set variable types for imputation
idVars <- impUsing$var_name[impUsing$var_type == "id"]
nomVars <- impUsing$var_name[impUsing$var_type == "nominal" | impUsing$var_type == "binary"]
ordVars <- impUsing$var_name[impUsing$var_type == "ordinal"]
logVars <- impUsing$var_name[impUsing$var_type == "log"]

dfImp <- amelia(tunisiaUse, idvars = idVars, noms = nomVars, ords = ordVars, log=logVars, m = numImp, empri = 0.01*nrow(tunisiaUse))

summary(dfImp)


## Add back in the columns from the raw dataset (e.g., unimputed)

dfFinal <- tunisia.trade # Note that this will include the original unimputed dataset
dfFinal$imp <- 0
for(i in 1:numImp){
  dfTemp <- tunisia.trade
  dfTemp[, impUsing$var_name] <- dfImp$imputations[[i]][, impUsing$var_name]
  dfTemp$imp <- i
  dfFinal <- rbind(dfFinal, dfTemp)} # This will stack the datasets together with the original unimputed dataset at the top

## SAVE Stacked Datasets as .csv and .dta files (where unimputed imp == 0)
write.csv(dfFinal, paste(MAIN_DIR,"data/ripe_imputed.csv", sep = ""))
save.dta13(dfFinal, paste(MAIN_DIR,"data/ripe_imputed.dta", sep = ""))


