I am trying to estimate a joint model with the data from two stated preference choice surveys. In my data collection, the respondents were first asked choose their preferred vehicle (between electric and conventional). Further in the same questionnaire, the same respondents were asked to choose their preferred location of charging, if they were to purchase an electric vehicle in future. I am trying to estimate a joint model (I have some variables that are same across both the games) to understand if the sensitivities to the common variables remains the same for the two types of decisions. I also wanted to introduce correlation terms to see if there is a correlation between choosing a type of vehicle and a type of location. For example, I wanted to see if there is correlation between choosing an electric vehicle and a particular type of charging location and similarly with the choice of conventional vehicle and choice of location.
What is the correct way to introduce these correlation terms?
Code: Select all
### Clear memory
rm(list = ls())
# install.packages("apollo")
library(apollo)
database = read.csv(file.choose(),header=TRUE)
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="V1_Joint",
modelDescr ="Joint SP1/SP2 Model Estimation",
indivID ="Person_ID",
nCores = 32
)
apollo_beta=c(asc_CV = 0,
asc_EV = 0,
asc_L = 0,
asc_W = 0,
asc_H = 0,
b_pp = 0,
b_oc = 0,
b_ct = 0,
b_rn = 0,
b_cf = 0,
b_em = 0,
b_cc = 0,
b_wt = 0,
mu_SP1 = 1,
mu_SP2 = 1,
sigma = 0
)
apollo_fixed = c("asc_CV", "asc_H",
"mu_SP1")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 1000,
interUnifDraws = c(),
interNormDraws = c("draws_alt1", "draws_alt2", "draws_alt3", "draws_alt4", "draws_alt5"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["ec1"]] = sigma*draws_alt1
randcoeff[["ec2"]] = sigma*draws_alt2
randcoeff[["ec3"]] = sigma*draws_alt3
randcoeff[["ec4"]] = sigma*draws_alt4
randcoeff[["ec5"]] = sigma*draws_alt5
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### 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 (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
V = list()
#######################################################################################
V[['EV']] = asc_EV + b_pp*ppEV* + b_oc*ocEV + b_ct*ctEV + b_rn*rEV + b_cf*(cfEV) + b_em*(eEV) + ec1
V[['CV']] = asc_CV + b_pp*ppCV* + b_oc*ocCV + b_rn*rCV + ec2
V[['leisure']] = asc_L + b_ct*ctLeisure + b_cc*ccLeisure + b_wt*wtLeisure + b_em*eLeisure + ec3
V[['workplace']] = asc_W + b_ct*ctWorkplace + b_cc*ccWorkplace + b_wt*wtWorkplace + b_em*eWorkplace + ec4
V[['highway']] = asc_H + b_ct*ctHighway + b_cc*ccHighway + b_wt*wtHighway + b_em*eHighway + ec5
### Compute probabilities for the SP1 part of the data using MNL model
mnl_settings_SP1 = list(
alternatives = c(EV=1, CV=2),
avail = list(EV=avEV, CV=avCV),
choiceVar = choice,
utilities = list(EV = mu_SP1*V[["EV"]],
CV = mu_SP1*V[["CV"]]
),
rows = (SP1==1)
)
P[["SP1"]] = apollo_mnl(mnl_settings_SP1, functionality)
### Compute probabilities for the SP2 part of the data using MNL model
mnl_settings_SP2 = list(
alternatives = c(leisure=3, workplace=4, highway=5),
avail = list(leisure=avLeisure, workplace=avWorkplace, highway=avHighway),
choiceVar = choice,
utilities = list(leisure = mu_SP2*V[["leisure"]],
workplace = mu_SP2*V[["workplace"]],
highway = mu_SP2*V[["highway"]]),
rows = (SP2==1)
)
P[["SP2"]] = apollo_mnl(mnl_settings_SP2, functionality)
### Combined model
P = apollo_combineModels(P, apollo_inputs, functionality)
### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
apollo_saveOutput(model)