Mixed Logit Model: how to choose ASC value?
Posted: 21 Mar 2022, 16:18
Hello Prof. Hess,
I am running the mixed logit model, however, the output from Apollo does not match with Nlogit one. I have two questions:
1. Is it necessary to use "exp" in the utility function for the ML model? When I have not used "exp" it produced results that matched better with Nlogit (except one variable).
2. How do you determine the ASC value, in my case ASC is the one that influences the direction/sign of the output.
I have posted below the codes I have used for my model.
I look forward to your response.
Sincerely,
Sakil
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MMNL_preference_space",
modelDescr = "Mixed logit model on Swiss route choice data, uncorrelated Lognormals in preference space",
indivID = "ID",
mixing = TRUE,
nCores = 4
#outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
database <- read.csv("C:\\Users\\dataset_130.csv", header = TRUE)
#database = apollo_swissRouteChoiceData
### for data dictionary, use ?apollo_swissRouteChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
##maybe related to distribution (maybe normal?)
apollo_beta = c(asc_a = -3,
mu_log_b_rec = 0,
sigma_log_b_rec = 1,
mu_log_b_eff = 0,
sigma_log_b_eff = 1,
mu_log_b_sideno = 0,
sigma_log_b_sideno = 1,
mu_log_b_sidemild = 0,
sigma_log_b_sidemild = 1,
mu_log_b_sidesev = 0,
sigma_log_b_sidesev = 1,
mu_log_b_cost = 0,
sigma_log_b_cost = 1,
lambda_inc_sig = 0)
### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_rec", "draws_eff","draws_sideno","draws_sidemild","draws_sidesev","draws_cost"),
intraDrawsType = "",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
# apollo_randCoeff = function(apollo_beta, apollo_inputs){
# randcoeff = list()
#
# randcoeff[["b_rec"]] = -exp(mu_b_rec + sigma_b_rec * draws_rec )
# randcoeff[["b_eff"]] = -exp(mu_b_eff + sigma_b_eff * draws_eff )
# randcoeff[["b_sideno"]] = -exp( mu_b_sideno + sigma_b_sideno * draws_sideno )
# randcoeff[["b_sidemild"]] = -exp(mu_b_sidemild + sigma_b_sidemild * draws_sidemild )
# randcoeff[["b_sidesev"]] = -exp(mu_b_sidesev + sigma_b_sidesev * draws_sidesev )
# randcoeff[["b_cost"]] = -exp(mu_b_cost + sigma_b_cost * draws_cost )
# return(randcoeff)
# }
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_rec"]] = ( mu_log_b_rec + sigma_log_b_rec * draws_rec )
randcoeff[["b_eff"]] = ( mu_log_b_eff + sigma_log_b_eff * draws_eff )
randcoeff[["b_sideno"]] = ( mu_log_b_sideno + sigma_log_b_sideno * draws_sideno )
randcoeff[["b_sidemild"]] = ( mu_log_b_sidemild + sigma_log_b_sidemild * draws_sidemild )
randcoeff[["b_sidesev"]] = ( mu_log_b_sidesev + sigma_log_b_sidesev * draws_sidesev )
randcoeff[["b_cost"]] = ( mu_log_b_cost + sigma_log_b_cost^lambda_inc_sig* draws_cost )
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
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: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["alt1"]] = asc_a + b_rec * REC1 + b_eff * Eff1 + b_sideno * sideno1+ b_sidemild * sidemild1 + b_sidesev * sidesev1 + b_cost * cost1
V[["alt2"]] = asc_a + b_rec * REC2 + b_eff * Eff2 + b_sideno * sideno2+ b_sidemild * sidemild2 + b_sidesev * sidesev2 + b_cost * cost2
V[["alt3"]] = b_rec * REC3 + b_eff * Eff3 + b_sideno * sideno3+ b_sidemild * sidemild3 + b_sidesev * sidesev3 + b_cost * cost3
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=hp, alt2=nhp, alt3=opt),
choiceVar = Choice,
V = V
)
### 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
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)
modelOutput_settings <- list(printPVal=2)
ctable <- apollo_modelOutput(model,modelOutput_settings)
ctable
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
I am running the mixed logit model, however, the output from Apollo does not match with Nlogit one. I have two questions:
1. Is it necessary to use "exp" in the utility function for the ML model? When I have not used "exp" it produced results that matched better with Nlogit (except one variable).
2. How do you determine the ASC value, in my case ASC is the one that influences the direction/sign of the output.
I have posted below the codes I have used for my model.
I look forward to your response.
Sincerely,
Sakil
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MMNL_preference_space",
modelDescr = "Mixed logit model on Swiss route choice data, uncorrelated Lognormals in preference space",
indivID = "ID",
mixing = TRUE,
nCores = 4
#outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
database <- read.csv("C:\\Users\\dataset_130.csv", header = TRUE)
#database = apollo_swissRouteChoiceData
### for data dictionary, use ?apollo_swissRouteChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
##maybe related to distribution (maybe normal?)
apollo_beta = c(asc_a = -3,
mu_log_b_rec = 0,
sigma_log_b_rec = 1,
mu_log_b_eff = 0,
sigma_log_b_eff = 1,
mu_log_b_sideno = 0,
sigma_log_b_sideno = 1,
mu_log_b_sidemild = 0,
sigma_log_b_sidemild = 1,
mu_log_b_sidesev = 0,
sigma_log_b_sidesev = 1,
mu_log_b_cost = 0,
sigma_log_b_cost = 1,
lambda_inc_sig = 0)
### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_rec", "draws_eff","draws_sideno","draws_sidemild","draws_sidesev","draws_cost"),
intraDrawsType = "",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
# apollo_randCoeff = function(apollo_beta, apollo_inputs){
# randcoeff = list()
#
# randcoeff[["b_rec"]] = -exp(mu_b_rec + sigma_b_rec * draws_rec )
# randcoeff[["b_eff"]] = -exp(mu_b_eff + sigma_b_eff * draws_eff )
# randcoeff[["b_sideno"]] = -exp( mu_b_sideno + sigma_b_sideno * draws_sideno )
# randcoeff[["b_sidemild"]] = -exp(mu_b_sidemild + sigma_b_sidemild * draws_sidemild )
# randcoeff[["b_sidesev"]] = -exp(mu_b_sidesev + sigma_b_sidesev * draws_sidesev )
# randcoeff[["b_cost"]] = -exp(mu_b_cost + sigma_b_cost * draws_cost )
# return(randcoeff)
# }
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_rec"]] = ( mu_log_b_rec + sigma_log_b_rec * draws_rec )
randcoeff[["b_eff"]] = ( mu_log_b_eff + sigma_log_b_eff * draws_eff )
randcoeff[["b_sideno"]] = ( mu_log_b_sideno + sigma_log_b_sideno * draws_sideno )
randcoeff[["b_sidemild"]] = ( mu_log_b_sidemild + sigma_log_b_sidemild * draws_sidemild )
randcoeff[["b_sidesev"]] = ( mu_log_b_sidesev + sigma_log_b_sidesev * draws_sidesev )
randcoeff[["b_cost"]] = ( mu_log_b_cost + sigma_log_b_cost^lambda_inc_sig* draws_cost )
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
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: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["alt1"]] = asc_a + b_rec * REC1 + b_eff * Eff1 + b_sideno * sideno1+ b_sidemild * sidemild1 + b_sidesev * sidesev1 + b_cost * cost1
V[["alt2"]] = asc_a + b_rec * REC2 + b_eff * Eff2 + b_sideno * sideno2+ b_sidemild * sidemild2 + b_sidesev * sidesev2 + b_cost * cost2
V[["alt3"]] = b_rec * REC3 + b_eff * Eff3 + b_sideno * sideno3+ b_sidemild * sidemild3 + b_sidesev * sidesev3 + b_cost * cost3
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=hp, alt2=nhp, alt3=opt),
choiceVar = Choice,
V = V
)
### 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
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)
modelOutput_settings <- list(printPVal=2)
ctable <- apollo_modelOutput(model,modelOutput_settings)
ctable
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)