Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

Mixed Logit Model: how to choose ASC value?

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
syeed
Posts: 3
Joined: 21 Mar 2022, 15:37

Mixed Logit Model: how to choose ASC value?

Post by syeed »

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)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Mixed Logit Model: how to choose ASC value?

Post by stephanehess »

Hi

from what you're saying below, it seems to me that you are not using the same distributions in NLogit and Apollo, and that is leading to your differences. What distributions do you want to use, Normal, Lognormal, etc? The use of exponentials leads to the use of Lognormal distributions. Please consult the manual/literature.

I do not understand what you mean by "how do you determine the ASC value". The ASC is an estimated parameter in your model, so you do not determine a value for it

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
syeed
Posts: 3
Joined: 21 Mar 2022, 15:37

Re: Mixed Logit Model: how to choose ASC value?

Post by syeed »

Thank you, Prof. Hess, for your prompt response.

I do understand that there is no need to apply the "exp" when I am assuming the normal distribution.

Regarding the ASC, my question was when we define the model parameter in model parameter, how do we define value for ASC? I have used 0 and 1 for the mean and standard deviation of assuming the normal distribution. But, how do we define the ASC in the apollo beta? I have used 0, -1, 1, -3 in the ASC, but not sure if I am doing it right and the rationale of what value needs to be used here.
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_cost = 0,
sigma_log_b_cost = 1)

Thank you again for your help. Truly appreciate it.
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Mixed Logit Model: how to choose ASC value?

Post by stephanehess »

Hi

these are starting values for the estimation. Starting them at 0 makes most sense probably. I think you're getting confused because the example you were looking at was using lognormal distributions, and then starting the mean of the log of the coefficient at -3 makes sense as this implies that the mode of the coefficient starts close to zero. See the FAQ on the website

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply