Select Git revision
run_video.bat
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
mxl_wtp_space_nat_ku.R 5.08 KiB
#### Apollo standard script #####
library(apollo) # Load apollo package
# Remove crazy outliers for testing the model
database <- database_full
# Test treatment effect
#initialize model
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MXL_wtp_nat_kubic",
modelDescr = "MXL wtp space naturalness kubic",
indivID ="id",
mixing = TRUE,
HB= FALSE,
nCores = n_cores,
outputDirectory = "Estimation_results/mxl"
)
##### Define model parameters depending on your attributes and model specification! ####
# set values to 0 for conditional logit model
apollo_beta=c(mu_natural = 25,
mu_natural_sq = 0,
mu_natural_ku = 0,
mu_walking = -2,
mu_rent = -2,
ASC_sq = 15,
sig_natural = 15,
sig_walking = 2,
sig_rent = 2,
sig_natural_sq = 0,
sig_natural_ku = 0)
### specify parameters that should be kept fixed, here = none
apollo_fixed = c()
### Set parameters for generating draws, use 2000 sobol draws
apollo_draws = list(
interDrawsType = "sobol",
interNDraws = n_draws,
interUnifDraws = c(),
interNormDraws = c("draws_natural", "draws_walking", "draws_rent", "draws_natural_sq", "draws_natural_ku"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters, define distribution of the parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_mu_natural"]] = mu_natural + sig_natural * draws_natural
randcoeff[["b_mu_walking"]] = mu_walking + sig_walking * draws_walking
randcoeff[["b_mu_rent"]] = -exp(mu_rent + sig_rent * draws_rent)
randcoeff[["b_mu_natural_sq"]] = mu_natural_sq + sig_natural_sq * draws_natural_sq
randcoeff[["b_mu_natural_ku"]] = mu_natural_ku + sig_natural_ku * draws_natural_ku
return(randcoeff)
}
### 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']] = -b_mu_rent*(b_mu_natural * Naturalness_1 + b_mu_walking * WalkingDistance_1 +
b_mu_natural_sq * (Naturalness_1)^2 +
b_mu_natural_ku * (Naturalness_1)^3
- Rent_1)
V[['alt2']] = -b_mu_rent*(b_mu_natural * Naturalness_2 + b_mu_walking * WalkingDistance_2 +
b_mu_natural_sq * (Naturalness_2)^2 +
b_mu_natural_ku * (Naturalness_2)^3
- Rent_2)
V[['alt3']] = -b_mu_rent*(ASC_sq + b_mu_natural * Naturalness_3 + b_mu_walking * WalkingDistance_3 +
b_mu_natural_sq * (Naturalness_3)^2 +
b_mu_natural_ku * (Naturalness_3)^3
- 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
mxl_wtp_nat_ku = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=list(maxIterations=400,
estimationRoutine="bfgs",
hessianRoutine="analytic"))
# ################################################################# #
#### MODEL OUTPUTS ##
# ################################################################# #
apollo_saveOutput(mxl_wtp_nat_ku)