rm(list=ls())


library(tidyverse)
library(tidylog)
library(apollo)
library(reshape2)
library(xtable)
library(stargazer)
library(texreg)
#test
# Set values for estimation in Apollo
n_draws <- 2000
n_cores <- min(parallel::detectCores()-1, 25)

# Load data
load("Data/database_full.RData")
load("Data/database.RData")

# Data preparation
source("Scripts/data_prep.R")
source("Scripts/treatment.R")

####### Estimate models ######

### Logit
source("Scripts/logit/chr_vol_treat.R")
source("Scripts/logit/protesters.R")
       
### OLS
source("Scripts/ols/ols_time_spent.R")
source("Scripts/ols/ols_quiz.R")
source("Scripts/ols/ols_opt_out.R")
source("Scripts/ols/ols_nr.R")
source("Scripts/ols/ols_consequentiality.R")

##### Conditional Logits #####
#source("Scripts/clogit.R")
#source("Scripts/clogit_wtp.R")

##### Mixed Logit Models ######

#source("Scripts/mxl/mxl_wtp_space.R")
#source("Scripts/mxl/mxl_wtp_space_4d.R")
#source("Scripts/mxl/mxl_wtp_space_interact_all.R")
#source("Scripts/mxl/mxl_socio_int.R")
#source("Scripts/mxl/mxl_treatment_time.R")
#source("Scripts/mxl/mxl_treatment_time_interaction.R")
#source("Scripts/mxl/mxl_treatment_time_Dummies.R")
#source("Scripts/mxl/mxl_wtp_space_interact_everything.R")
#source("Scripts/mxl/mxl_wtp_space_4d_interact_everything.R")
#############################

##### Load models ############

mxl_wtp <- apollo_loadModel("Estimation_results/mxl/MXL_wtp")
mxl_wtp_4d <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_4d")
mxl_wtp_all_int <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_interact_all")
mxl_wtp_socio <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_socio_int")
mxl_wtp_tt <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_treatment_time")
mxl_wtp_tt_interaction <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_treatment_time_interaction")
mxl_wtp_time_groups <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_time_groups")
mxl_wtp_everything <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_interact_everything")
mxl_wtp_case_a <- apollo_loadModel("Estimation_results/mxl/MXL_wtp Case A")
mxl_wtp_case_a_NR <- apollo_loadModel("Estimation_results/mxl/MXL_wtp NR A")
mxl_wtp_case_b <- apollo_loadModel("Estimation_results/mxl/MXL_wtp Case B")
mxl_wtp_case_b_NR <- apollo_loadModel("Estimation_results/mxl/MXL_wtp NR B")
mxl_wtp_case_c <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_Case_C")
mxl_wtp_case_c_NR <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_NR_Case_C")
mxl_wtp_case_d <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_Case_D base")

# rent interactions models
mxl_wtp_case_a_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp Case A Rent Int")
mxl_wtp_case_b_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp Case B Rent Int")
mxl_wtp_case_c_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_Case_C Rent INT")
mxl_wtp_case_d_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_Case_D")

# rent interactions models NR
mxl_wtp_NR_case_a_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp NR A Rent INT")
mxl_wtp_NR_case_c_rentINT <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_NR_Case_C RENT INT X")

# Alternative case
case_d <- apollo_loadModel("Estimation_results/mxl/MXL_wtp Case D Rent Int")

# New Case Text Video merged
new_case_b <- apollo_loadModel("Estimation_results/mxl/MXL_wtp_Case_D")

new_case_b_NR <- apollo_loadModel("Estimation_results/mxl/D_NR")

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

# Model analysis
source("Scripts/visualize_models.R")

source("Scripts/compare_split_samples.R")

source("Scripts/create_tables.R")

source("Scripts/interaction_plots_presi.R")


### Old models ###


# # without protesters
# mxl_wtp_case_a_prot <- apollo_loadModel("Estimation_results/mxl/without_protesters/MXL_wtp Case A prot")
# mxl_wtp_case_b_prot <- apollo_loadModel("Estimation_results/mxl/without_protesters/MXL_wtp Case B prot")
# mxl_wtp_case_c_prot <- apollo_loadModel("Estimation_results/mxl/without_protesters/MXL_wtp_Case_C prot")


#### Implement Propensity Score Matching 


####### Show distributions of split samples and compare 

source("Scripts/visualize_distr_spli.R")