#### Apollo standard script #####

library(apollo) # Load apollo package 

# Remove crazy outliers for testing the model 
database <- database %>% filter(Rent_SQ <= 10000, WalkingDistance_SQ <= 300)

# Test treatment effect

database <- database %>%
  filter(!is.na(Treatment_new)) %>%
  mutate(Dummy_Video_1 = case_when(Treatment_new == 1 ~ 1, TRUE ~ 0),
         Dummy_Video_2 = case_when(Treatment_new == 5 ~ 1, TRUE ~ 0),
         Dummy_no_info = case_when(Treatment_new == 3 ~ 1, TRUE~0),
         Dummy_Info_nv1 = case_when(Treatment_new == 2 ~1, TRUE~0),
         Dummy_Info_nv2 = case_when(Treatment_new == 4 ~1 , TRUE~0))

  #initialize model 
  
  apollo_initialise()
  
  
  ### Set core controls
  apollo_control = list(
    modelName  = "Clogit_wtp",
    modelDescr = "Conditonal logit model wtp space",
    indivID    ="id",
    mixing     = FALSE,
    HB= FALSE,
    nCores     = 1, 
    outputDirectory = "Estimation_results"
  )
  
  ##### Define model parameters depending on your attributes and model specification! ####
  # set values to 0 for conditional logit model
  
  apollo_beta=c(mu_natural = 0,
                mu_walking = 0,
                mu_rent = 0,
                ASC_sq = 0,
                mu_nat_vid1 =0,
                mu_nat_vid2 = 0,
                mu_nat_no_info = 0,
                mu_nat_info_nv1 = 0,
                mu_nat_info_nv2 = 0)
  
  ### specify parameters that should be kept fixed, here = none
  apollo_fixed = c()
  
  
  ### validate 
  apollo_inputs = apollo_validateInputs()
  apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
    
    ### Function initialisation: do not change the following three commands
    ### Attach inputs and detach after function exit
    apollo_attach(apollo_beta, apollo_inputs)
    on.exit(apollo_detach(apollo_beta, apollo_inputs))
    
    ### Create list of probabilities P
    P = list()
    
    #### List of utilities (later integrated in mnl_settings below)  ####
    # Define utility functions here:
     
    V = list()
    V[['alt1']] = -mu_rent*(mu_natural * Naturalness_1 + mu_walking * WalkingDistance_1 +
                   mu_nat_vid1 * Naturalness_1 *Dummy_Video_1 + mu_nat_no_info * Naturalness_1 * Dummy_no_info
                   +  mu_nat_info_nv1 * Naturalness_1 *Dummy_Info_nv1 + mu_nat_vid2 * Naturalness_1 * Dummy_Video_2
                   +  mu_nat_info_nv2 * Naturalness_1 *Dummy_Info_nv2 - Rent_1)
    
    V[['alt2']] = -mu_rent*(mu_natural * Naturalness_2 + mu_walking * WalkingDistance_2 +
                  mu_nat_vid1 * Naturalness_2 *Dummy_Video_1 + mu_nat_no_info * Naturalness_2 * Dummy_no_info
                  +  mu_nat_info_nv1 * Naturalness_2 *Dummy_Info_nv1  + mu_nat_vid2 * Naturalness_2 * Dummy_Video_2
                  +  mu_nat_info_nv2 * Naturalness_2 *Dummy_Info_nv2 - Rent_2)
    
    V[['alt3']] = -mu_rent*(ASC_sq + mu_natural * Naturalness_3 + mu_walking * WalkingDistance_3 + 
                  mu_nat_vid1 * Naturalness_3 *Dummy_Video_1 + mu_nat_no_info * Naturalness_3 * Dummy_no_info
                  +  mu_nat_info_nv1 * Naturalness_3 *Dummy_Info_nv1  + mu_nat_vid2 * Naturalness_3 * Dummy_Video_2 
                  +  mu_nat_info_nv2 * Naturalness_3 *Dummy_Info_nv2 - Rent_3)
    
    
    ### Define settings for MNL model component
    mnl_settings = list(
      alternatives  = c(alt1=1, alt2=2, alt3=3),
      avail         = 1, # all alternatives are available in every choice
      choiceVar     = choice,
      V             = V#,  # tell function to use list vector defined above
      
    )
    
    ### Compute probabilities using MNL model
    P[['model']] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    P = apollo_panelProd(P, apollo_inputs, functionality)
    
    ### Average across inter-individual draws - nur bei Mixed Logit!
    #P = apollo_avgInterDraws(P, apollo_inputs, functionality)
    
    ### Prepare and return outputs of function
    P = apollo_prepareProb(P, apollo_inputs, functionality)
    return(P)
  }
  
  
  
  # ################################################################# #
  #### MODEL ESTIMATION                                            ##
  # ################################################################# #
  # estimate model with bfgs algorithm
  
  clogit_wtp = apollo_estimate(apollo_beta, apollo_fixed,
                        apollo_probabilities, apollo_inputs, 
                        estimate_settings=list(maxIterations=400,
                                               estimationRoutine="bfgs",
                                               hessianRoutine="analytic"))
  
  
  
  # ################################################################# #
  #### MODEL OUTPUTS                                               ##
  # ################################################################# #
  apollo_saveOutput(clogit_wtp)