Hi Apollo team,
I ran mixed logit model without covariates for my DCE study with dummy coding.
The estimates in my result are showing undesired directions (for almost all the attributes or some level of the attributes). The directions were fine for all the attributes besides one level of fasting (Fast_3) in MNL model. But, in mixed logit it is completely messed up. I could not exactly identify the mistake in my codes since they ran fine.
Would you please help me figure this out regarding the issue and possible solution for it?
Below are my codes and the result:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MMNL_DCE3_uncorrelated",
modelDescr = "Mixed logit, uncorrelated",
indivID = "ID",
nCores = 4,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data
data = read.csv("DCE_withoutcovariates.csv",header=TRUE)
head(data)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
mu_b_asc_2 = 0,
sigma_b_asc_2 = 0,
mu_b_Fast_2 = 0,
sigma_b_Fast_2 = 0,
mu_b_Fast_3 = 0,
sigma_b_Fast_3 = 0,
mu_b_Fast_4 = 0,
sigma_b_Fast_4 = 0,
mu_b_Inv_2 = 0,
sigma_b_Inv_2 = 0,
mu_b_Sen_2 = 0,
sigma_b_Sen_2 = 0,
mu_b_Sen_3 = 0,
sigma_b_Sen_3 = 0,
mu_b_Spe_2 = 0,
sigma_b_Spe_2 = 0,
mu_b_Spe_3 = 0,
sigma_b_Spe_3 = 0,
mu_b_Pre_2 = 0,
sigma_b_Pre_2 = 0,
mu_b_Cost_2 = -3,
sigma_b_Cost_2 = 0.01,
mu_b_Cost_3 = -3,
sigma_b_Cost_3 = 0.01,
mu_b_Cost_4 = -3,
sigma_b_Cost_4 = 0.01
)
### 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 = "mlhs",
interNDraws = 1000,
interUnifDraws = c(),
interNormDraws = c("draws_asc_2","draws_Fast_2","draws_Fast_3","draws_Fast_4","draws_Inv_2","draws_Sen_2","draws_Sen_3","draws_Spe_2","draws_Spe_3", "draws_Pre_2", "draws_Cost_2","draws_Cost_3", "draws_Cost_4"),
intraDrawsType = "mlhs",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_asc_2"]] = mu_b_asc_2 + sigma_b_asc_2 * draws_asc_2 #(mean + std dev * std normal variable)
randcoeff[["b_Fast_2"]] = mu_b_Fast_2 + sigma_b_Fast_2 * draws_Fast_2
randcoeff[["b_Fast_3"]] = mu_b_Fast_3 + sigma_b_Fast_3 * draws_Fast_3
randcoeff[["b_Fast_4"]] = mu_b_Fast_4 + sigma_b_Fast_4 * draws_Fast_4
randcoeff[["b_Inv_2"]] = mu_b_Inv_2 + sigma_b_Inv_2 * draws_Inv_2
randcoeff[["b_Sen_2"]] = mu_b_Sen_2 + sigma_b_Sen_2 * draws_Sen_2
randcoeff[["b_Sen_3"]] = mu_b_Sen_3 + sigma_b_Sen_3 * draws_Sen_3
randcoeff[["b_Spe_2"]] = mu_b_Spe_2 + sigma_b_Spe_2 * draws_Spe_2
randcoeff[["b_Spe_3"]] = mu_b_Spe_3 + sigma_b_Spe_3 * draws_Spe_3
randcoeff[["b_Pre_2"]] = mu_b_Pre_2 + sigma_b_Pre_2 * draws_Pre_2
randcoeff[["b_Cost_2"]] = -exp( mu_b_Cost_2 + sigma_b_Cost_2 * draws_Cost_2 )
randcoeff[["b_Cost_3"]] = -exp( mu_b_Cost_3 + sigma_b_Cost_3 * draws_Cost_3 )
randcoeff[["b_Cost_4"]] = -exp( mu_b_Cost_4 + sigma_b_Cost_4 * draws_Cost_4 )
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"]] = b_Fast_2 * Fast1_2 + b_Fast_3 * Fast1_3 + b_Fast_4 * Fast1_4 + b_Inv_2 * Inv1_2 + b_Sen_2 * Sen1_2 + b_Sen_3 * Sen1_3 + b_Spe_2 * Spe1_2 + b_Spe_3 * Spe1_3 + b_Pre_2 * Pre1_2 + b_Cost_2 * Cost1_2 + b_Cost_3 * Cost1_3 + b_Cost_4 * Cost1_4
V[["alt2"]] = b_asc_2 + b_Fast_2 * Fast2_2 + b_Fast_3 * Fast2_3 + b_Fast_4 * Fast2_4 + b_Inv_2 * Inv2_2 + b_Sen_2 * Sen2_2 + b_Sen_3 * Sen2_3 + b_Spe_2 * Spe2_2 + b_Spe_3 * Spe2_3 + b_Pre_2 * Pre2_2 + b_Cost_2 * Cost2_2 + b_Cost_3 * Cost2_3 + b_Cost_4 * Cost2_4
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = Choice,
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual(heterogeneity is across per person; how likely is the sequences of choices per person in each group)
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 ####
# ################################################################# #
apollo_modelOutput(model)
apollo_saveOutput(model)
apollo_deltaMethod(model, list(operation="lognormal",
parName1="mu_b_Cost_2",
parName2="sigma_b_Cost_2" ))
apollo_deltaMethod(model, list(operation="lognormal",
parName1="mu_b_Cost_3",
parName2="sigma_b_Cost_3"
))
apollo_deltaMethod(model, list(operation="lognormal",
parName1="mu_b_Cost_4",
parName2="sigma_b_Cost_4"
))
OUTPUT:
Overview of choices for MNL model component :
alt1 alt2
Times available 1962.00 1962.00
Times chosen 874.00 1088.00
Percentage chosen overall 44.55 55.45
Percentage chosen when available 44.55 55.45
Pre-processing likelihood function...
Creating cluster...
Preparing workers for multithreading...
Testing influence of parameters
Starting main estimation
BGW using analytic model derivatives supplied by caller...
Iterates will be written to:
output/MMNL_DCE3_uncorrelated_iterations.csv
it nf F RELDF PRELDF RELDX MODEL stppar
0 1 1.353752076e+03
1 3 1.308904952e+03 3.313e-02 1.107e-03 6.76e-03 G 1.78e+02
2 4 1.225550505e+03 6.368e-02 6.070e-02 3.48e-01 G 5.21e-02
3 6 1.181683378e+03 3.579e-02 3.460e-02 1.47e-01 G 2.02e+00
4 7 1.140016480e+03 3.526e-02 3.747e-02 2.64e-01 G 2.12e-01
5 8 1.116452558e+03 2.067e-02 2.685e-02 2.68e-01 G 4.33e-02
6 10 1.103921359e+03 1.122e-02 1.373e-02 6.78e-02 S 8.52e-01
7 12 1.102851508e+03 9.691e-04 1.100e-03 1.22e-02 S 1.72e+00
8 14 1.101617837e+03 1.119e-03 1.450e-03 3.57e-02 S 2.77e-01
9 16 1.101154505e+03 4.206e-04 9.718e-04 3.37e-02 G-S 1.23e-01
10 17 1.100854622e+03 2.723e-04 3.995e-04 2.71e-02 S 0.00e+00
11 18 1.100738574e+03 1.054e-04 9.502e-05 9.68e-03 S 0.00e+00
12 19 1.100720598e+03 1.633e-05 1.626e-05 4.07e-03 S 0.00e+00
13 20 1.100717994e+03 2.365e-06 2.259e-06 1.74e-03 S 0.00e+00
14 21 1.100717592e+03 3.653e-07 3.783e-07 4.59e-04 S 0.00e+00
15 22 1.100717524e+03 6.225e-08 7.278e-08 2.63e-04 S 0.00e+00
16 23 1.100717508e+03 1.368e-08 1.317e-08 1.06e-04 S 0.00e+00
17 24 1.100717506e+03 2.047e-09 1.938e-09 5.74e-05 S 0.00e+00
18 25 1.100717506e+03 3.088e-10 3.315e-10 2.03e-05 S 0.00e+00
19 26 1.100717506e+03 3.337e-11 3.794e-11 6.86e-06 S 0.00e+00
***** Relative function convergence *****
Estimated parameters with approximate standard errors from BHHH matrix:
Estimate BHHH se BHH t-ratio (0)
mu_b_asc_2 -0.058791 0.08324 -0.706328
sigma_b_asc_2 -0.127283 0.45828 -0.277738
mu_b_Fast_2 0.207459 0.15225 1.362645
sigma_b_Fast_2 0.423840 0.29448 1.439281
mu_b_Fast_3 -0.056458 0.14750 -0.382771
sigma_b_Fast_3 0.147138 0.59088 0.249015
mu_b_Fast_4 0.049181 0.13607 0.361437
sigma_b_Fast_4 0.390015 0.28721 1.357944
mu_b_Inv_2 0.673622 0.11526 5.844429
sigma_b_Inv_2 0.825433 0.13646 6.048810
mu_b_Sen_2 0.551308 0.11645 4.734335
sigma_b_Sen_2 -0.057836 0.99055 -0.058388
mu_b_Sen_3 1.188378 0.16331 7.276738
sigma_b_Sen_3 -0.811546 0.19617 -4.136863
mu_b_Spe_2 0.641736 0.13011 4.932336
sigma_b_Spe_2 0.062626 0.90136 0.069479
mu_b_Spe_3 1.391553 0.18258 7.621526
sigma_b_Spe_3 -0.460507 0.27227 -1.691363
mu_b_Pre_2 -0.559131 0.11941 -4.682613
sigma_b_Pre_2 -0.987894 0.14541 -6.793766
mu_b_Cost_2 -0.259587 0.17801 -1.458281
sigma_b_Cost_2 -0.003030 1.66725 -0.001818
mu_b_Cost_3 0.119796 0.14831 0.807716
sigma_b_Cost_3 0.414163 0.24587 1.684453
mu_b_Cost_4 0.832412 0.12614 6.599141
sigma_b_Cost_4 0.755763 0.13479 5.607058
Final LL: -1100.7175
Calculating log-likelihood at equal shares (LL(0)) for applicable models...
Calculating log-likelihood at observed shares from estimation data (LL(c)) for applicable models...
Calculating LL of each model component...
Calculating other model fit measures
Computing covariance matrix using numerical jacobian of analytical gradient.
0%....25%....50%....75%....100%
Negative definite Hessian with maximum eigenvalue: -4.730608
Computing score matrix...
Looking forward to your prompt response.
Sincerely,
Priyadarsini
Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- posts will not immediately appear on the forum, but will be checked by a moderator first. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.
Different directions in results from MNL and Mixed logit
-
- Site Admin
- Posts: 1261
- Joined: 24 Apr 2020, 16:29
Re: Different directions in results from MNL and Mixed logit
Hi
first, how does the fit compare to MNL?
Then, some comments:
- this many random parameters almost never works unless you have a huge amount of data
- using Normals for parameters with clear sign assumptions is not generally advisable
- with categorical variables, you maybe need to use additive lognormals if you have clear sign assumptions
Or consider latent class
Stephane
first, how does the fit compare to MNL?
Then, some comments:
- this many random parameters almost never works unless you have a huge amount of data
- using Normals for parameters with clear sign assumptions is not generally advisable
- with categorical variables, you maybe need to use additive lognormals if you have clear sign assumptions
Or consider latent class
Stephane
Re: Different directions in results from MNL and Mixed logit
Hi Stephane,
Thank you for your response.
The AIC for MNL is 2332.18 and Mixed logit is 2253.44.
Could you advise me on the starting values if I choose to go with positive or negative lognormal
for the parameters based on the signs from MNL model? Is the way I have coded for the different levels correct so far?
Also, would you kindly provide more insights on the additive lognormals?
Is it not a normal practice to use all the levels of attributes for mixed logit? Is it the case that I have to choose levels among them as random parameters? If yes, how should I decide on which to choose?
Sincerely,
Priyadarsini
Thank you for your response.
The AIC for MNL is 2332.18 and Mixed logit is 2253.44.
Could you advise me on the starting values if I choose to go with positive or negative lognormal
for the parameters based on the signs from MNL model? Is the way I have coded for the different levels correct so far?
Also, would you kindly provide more insights on the additive lognormals?
Is it not a normal practice to use all the levels of attributes for mixed logit? Is it the case that I have to choose levels among them as random parameters? If yes, how should I decide on which to choose?
Sincerely,
Priyadarsini
-
- Site Admin
- Posts: 1261
- Joined: 24 Apr 2020, 16:29
Re: Different directions in results from MNL and Mixed logit
HI
can you show us the actual outputs, please, with t-ratios, etc
Thanks
can you show us the actual outputs, please, with t-ratios, etc
Thanks
Re: Different directions in results from MNL and Mixed logit
Hi,
Please find the output below for MMNL and kindly help me understand and resolve the issue:
Model name : MMNL_DCE3_uncorrelated
Model description : Mixed logit model on DCE3 data, uncorrelated
Model run at : 2024-12-19 17:03:34.606872
Estimation method : bgw
Model diagnosis : Relative function convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definite
maximum eigenvalue : -4.730608
reciprocal of condition number : 0.0191397
Number of individuals : 218
Number of rows in database : 1962
Number of modelled outcomes : 1962
Number of cores used : 4
Number of inter-individual draws : 1000 (mlhs)
LL(start) : -1353.75
LL at equal shares, LL(0) : -1359.95
LL at observed shares, LL(C) : -1348.26
LL(final) : -1100.72
Rho-squared vs equal shares : 0.1906
Adj.Rho-squared vs equal shares : 0.1715
Rho-squared vs observed shares : 0.1836
Adj.Rho-squared vs observed shares : 0.1651
AIC : 2253.44
BIC : 2398.56
Estimated parameters : 26
Time taken (hh:mm:ss) : 00:14:46.88
pre-estimation : 00:00:43.14
estimation : 00:02:4.17
post-estimation : 00:11:59.58
Iterations : 19
Unconstrained optimisation.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
mu_b_asc_2 -0.058791 0.07647 -0.768776 0.07441 -0.79006
sigma_b_asc_2 -0.127283 0.21878 -0.581794 0.12168 -1.04602
mu_b_Fast_2 0.207459 0.13303 1.559438 0.12731 1.62954
sigma_b_Fast_2 0.423840 0.25108 1.688087 0.22870 1.85329
mu_b_Fast_3 -0.056458 0.12810 -0.440741 0.12140 -0.46507
sigma_b_Fast_3 0.147138 0.42701 0.344575 0.33484 0.43943
mu_b_Fast_4 0.049181 0.12932 0.380310 0.13446 0.36577
sigma_b_Fast_4 0.390015 0.28480 1.369412 0.31140 1.25246
mu_b_Inv_2 0.673622 0.10741 6.271342 0.11232 5.99743
sigma_b_Inv_2 0.825433 0.13603 6.068034 0.15296 5.39624
mu_b_Sen_2 0.551308 0.10773 5.117339 0.11106 4.96418
sigma_b_Sen_2 -0.057836 0.24841 -0.232819 0.07165 -0.80718
mu_b_Sen_3 1.188378 0.15724 7.557515 0.16817 7.06641
sigma_b_Sen_3 -0.811546 0.17901 -4.533565 0.18311 -4.43211
mu_b_Spe_2 0.641736 0.11592 5.536216 0.11340 5.65892
sigma_b_Spe_2 0.062626 0.21129 0.296407 0.06248 1.00238
mu_b_Spe_3 1.391553 0.16839 8.264111 0.17487 7.95776
sigma_b_Spe_3 -0.460507 0.25382 -1.814334 0.25543 -1.80290
mu_b_Pre_2 -0.559131 0.11015 -5.075962 0.11296 -4.94993
sigma_b_Pre_2 -0.987894 0.14211 -6.951651 0.14787 -6.68074
mu_b_Cost_2 -0.259587 0.16987 -1.528121 0.18508 -1.40253
sigma_b_Cost_2 -0.003030 0.39780 -0.007618 0.10792 -0.02808
mu_b_Cost_3 0.119796 0.14864 0.805926 0.16895 0.70906
sigma_b_Cost_3 0.414163 0.19388 2.136173 0.16756 2.47179
mu_b_Cost_4 0.832412 0.11518 7.226855 0.12563 6.62586
sigma_b_Cost_4 0.755763 0.13303 5.681136 0.14134 5.34696
Sincerely,
Priyadarsini
Please find the output below for MMNL and kindly help me understand and resolve the issue:
Model name : MMNL_DCE3_uncorrelated
Model description : Mixed logit model on DCE3 data, uncorrelated
Model run at : 2024-12-19 17:03:34.606872
Estimation method : bgw
Model diagnosis : Relative function convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definite
maximum eigenvalue : -4.730608
reciprocal of condition number : 0.0191397
Number of individuals : 218
Number of rows in database : 1962
Number of modelled outcomes : 1962
Number of cores used : 4
Number of inter-individual draws : 1000 (mlhs)
LL(start) : -1353.75
LL at equal shares, LL(0) : -1359.95
LL at observed shares, LL(C) : -1348.26
LL(final) : -1100.72
Rho-squared vs equal shares : 0.1906
Adj.Rho-squared vs equal shares : 0.1715
Rho-squared vs observed shares : 0.1836
Adj.Rho-squared vs observed shares : 0.1651
AIC : 2253.44
BIC : 2398.56
Estimated parameters : 26
Time taken (hh:mm:ss) : 00:14:46.88
pre-estimation : 00:00:43.14
estimation : 00:02:4.17
post-estimation : 00:11:59.58
Iterations : 19
Unconstrained optimisation.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
mu_b_asc_2 -0.058791 0.07647 -0.768776 0.07441 -0.79006
sigma_b_asc_2 -0.127283 0.21878 -0.581794 0.12168 -1.04602
mu_b_Fast_2 0.207459 0.13303 1.559438 0.12731 1.62954
sigma_b_Fast_2 0.423840 0.25108 1.688087 0.22870 1.85329
mu_b_Fast_3 -0.056458 0.12810 -0.440741 0.12140 -0.46507
sigma_b_Fast_3 0.147138 0.42701 0.344575 0.33484 0.43943
mu_b_Fast_4 0.049181 0.12932 0.380310 0.13446 0.36577
sigma_b_Fast_4 0.390015 0.28480 1.369412 0.31140 1.25246
mu_b_Inv_2 0.673622 0.10741 6.271342 0.11232 5.99743
sigma_b_Inv_2 0.825433 0.13603 6.068034 0.15296 5.39624
mu_b_Sen_2 0.551308 0.10773 5.117339 0.11106 4.96418
sigma_b_Sen_2 -0.057836 0.24841 -0.232819 0.07165 -0.80718
mu_b_Sen_3 1.188378 0.15724 7.557515 0.16817 7.06641
sigma_b_Sen_3 -0.811546 0.17901 -4.533565 0.18311 -4.43211
mu_b_Spe_2 0.641736 0.11592 5.536216 0.11340 5.65892
sigma_b_Spe_2 0.062626 0.21129 0.296407 0.06248 1.00238
mu_b_Spe_3 1.391553 0.16839 8.264111 0.17487 7.95776
sigma_b_Spe_3 -0.460507 0.25382 -1.814334 0.25543 -1.80290
mu_b_Pre_2 -0.559131 0.11015 -5.075962 0.11296 -4.94993
sigma_b_Pre_2 -0.987894 0.14211 -6.951651 0.14787 -6.68074
mu_b_Cost_2 -0.259587 0.16987 -1.528121 0.18508 -1.40253
sigma_b_Cost_2 -0.003030 0.39780 -0.007618 0.10792 -0.02808
mu_b_Cost_3 0.119796 0.14864 0.805926 0.16895 0.70906
sigma_b_Cost_3 0.414163 0.19388 2.136173 0.16756 2.47179
mu_b_Cost_4 0.832412 0.11518 7.226855 0.12563 6.62586
sigma_b_Cost_4 0.755763 0.13303 5.681136 0.14134 5.34696
Sincerely,
Priyadarsini
Last edited by apk0022 on 12 Jan 2025, 18:37, edited 1 time in total.
-
- Site Admin
- Posts: 1261
- Joined: 24 Apr 2020, 16:29
Re: Different directions in results from MNL and Mixed logit
Apologies for the slow reply, I was away. Can you also share the MNL results, please?
Re: Different directions in results from MNL and Mixed logit
Sure.
Here are the code and output from the MNL model:
Code:
Here are the code and output from the MNL model:
Code:
Code: Select all
### Clear memory
rm(list = ls())
install.packages("apollo")
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "DCE3",
modelDescr = "MNL model",
indivID = "ID",
nAlternatives = 2, # Number of alternatives
alternatives = c(1, 2), # Define alternative IDs
outputDirectory = "output"
)
database = read.csv("DCE3_dummy_coded.csv")
database <- database %>% select(-NAFLD.year, -NASH.year, -Advfibrosis.year)
### Vector of parameters, including any that are kept fixed in estimation
# Initial parameter values for 2 alternatives and 6 attributes
# Example with corrected parameters
apollo_beta <- c(
asc_alt1 = 0, # Alternative 1 (ASC)
asc_alt2 = 0, # Alternative 2 (ASC)
# Fasting Time for alternative 1 and alternative 2 (4 levels)
b_fasting_time_2 = 0,
b_fasting_time_3 = 0,
b_fasting_time_4 = 0,
# Invasiveness for alternative 1 and alternative 2
b_invasiveness_2 = 0,
# Sensitivity for alternative 1 and alternative 2 (3 levels)
b_sensitivity_2 = 0,
b_sensitivity_3 = 0,
# Specificity for alternative 1 and alternative 2 (3 levels)
b_specificity_2 = 0,
b_specificity_3 = 0,
# Prediction for alternative 1 and alternative 2
b_prediction_2 = 0,
# Cost for alternative 1 and alternative 2 (4 levels)
b_cost_2 = 0,
b_cost_3 = 0,
b_cost_4 = 0
)
apollo_fixed <- c("asc_alt1")
# ################################################################# #
#### 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: these must use the same names as in mnl_settings, order is irrelevant
V <- list(
# Utility function for alternative 1
alt1 = asc_alt1 +
b_fasting_time_2 * (Fast_A_2) +
b_fasting_time_3 * (Fast_A_3) +
b_fasting_time_4 * (Fast_A_4) +
b_invasiveness_2 * (Inv_A_2) +
b_sensitivity_2 * (Sen_A_2) +
b_sensitivity_3 * (Sen_A_3) +
b_specificity_2 * (Spe_A_2) +
b_specificity_3 * (Spe_A_3) +
b_prediction_2 * (Pre_A_2) +
b_cost_2 * (Cost_A_2) +
b_cost_3 * (Cost_A_3) +
b_cost_4 * (Cost_A_4),
# Utility function for alternative 2
alt2 = asc_alt2 +
b_fasting_time_2 * (Fast_B_2) +
b_fasting_time_3 * (Fast_B_3) +
b_fasting_time_4 * (Fast_B_4) +
b_invasiveness_2 * (Inv_B_2) +
b_sensitivity_2 * (Sen_B_2) +
b_sensitivity_3 * (Sen_B_3) +
b_specificity_2 * (Spe_B_2) +
b_specificity_3 * (Spe_B_3) +
b_prediction_2 * (Pre_B_2) +
b_cost_2 * (Cost_B_2) +
b_cost_3 * (Cost_B_3) +
b_cost_4 * (Cost_B_4)
)
### Define settings for MNL model component
mnl_settings <- list(
alternatives = c(alt1 = 1, alt2 = 2),
avail = 1, # Assuming all alternatives are available
choiceVar = Choice, # Ensure 'Choice' column exists in your data
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] <- apollo_mnl(mnl_settings, functionality)
### Take product across observations for the same individual if needed (for panel data)
P <- apollo_panelProd(P, apollo_inputs, functionality)
### Prepare and return outputs of the function (final preparation)
P <- apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# #################################################################
# Estimating the model
model <- apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model,modelOutput_settings = list(printClassical=FALSE))
[b]Output:[/b]
Code: Select all
Model name : DCE3
Model description : MNL model
Model run at : 2025-01-16 10:03:09.295304
Estimation method : bgw
Model diagnosis : Relative function convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definite
maximum eigenvalue : -33.424202
reciprocal of condition number : 0.0734103
Number of individuals : 218
Number of rows in database : 1962
Number of modelled outcomes : 1962
Number of cores used : 1
Model without mixing
LL(start) : -1359.95
LL at equal shares, LL(0) : -1359.95
LL at observed shares, LL(C) : -1348.26
LL(final) : -1153.09
Rho-squared vs equal shares : 0.1521
Adj.Rho-squared vs equal shares : 0.1426
Rho-squared vs observed shares : 0.1448
Adj.Rho-squared vs observed shares : 0.1359
AIC : 2332.18
BIC : 2404.75
Estimated parameters : 13
Time taken (hh:mm:ss) : 00:00:1.6
pre-estimation : 00:00:0.69
estimation : 00:00:0.19
post-estimation : 00:00:0.72
Iterations : 7
Unconstrained optimisation.
Estimates:
Estimate Rob.s.e. Rob.t.rat.(0)
asc_alt1 0.000000 NA NA
asc_alt2 -0.015345 0.04757 -0.32258
b_fasting_time_2 0.128454 0.08425 1.52459
b_fasting_time_3 -0.022743 0.08165 -0.27853
b_fasting_time_4 -0.006711 0.08716 -0.07700
b_invasiveness_2 0.437541 0.06152 7.11211
b_sensitivity_2 0.401004 0.07693 5.21277
b_sensitivity_3 0.791814 0.09982 7.93224
b_specificity_2 0.396364 0.06790 5.83727
b_specificity_3 0.927694 0.09065 10.23430
b_prediction_2 -0.389581 0.06851 -5.68623
b_cost_2 -0.547656 0.09845 -5.56287
b_cost_3 -0.842889 0.10848 -7.76982
b_cost_4 -1.709433 0.14692 -11.63531
Re: Different directions in results from MNL and Mixed logit
Hi Priyadarsini,
Your model seems to only have dummy variables as explanatory variables. As Walker (2002) discuses, when using random coefficients for dummy variables you cannot select the base arbitrarily. Instead, you should first estimate an overspecified model including random effects for all levels of categorical variables, then look for the level with the lowest standard deviation, and you use that as a base. Alternatively, you can estimate coefficients for all levels but fix the mean of the effect of one of them to zero. Or use an arbitrary base but correlate the coefficients. Please try one of these approaches and see if that helps.
Best wishes,
David
Ref:
Walker, J. (2002) Mixed Logit (or Logit Kernel) Model: Dispelling Misconceptions of Identification. Transportation Research Record 1805.
Your model seems to only have dummy variables as explanatory variables. As Walker (2002) discuses, when using random coefficients for dummy variables you cannot select the base arbitrarily. Instead, you should first estimate an overspecified model including random effects for all levels of categorical variables, then look for the level with the lowest standard deviation, and you use that as a base. Alternatively, you can estimate coefficients for all levels but fix the mean of the effect of one of them to zero. Or use an arbitrary base but correlate the coefficients. Please try one of these approaches and see if that helps.
Best wishes,
David
Ref:
Walker, J. (2002) Mixed Logit (or Logit Kernel) Model: Dispelling Misconceptions of Identification. Transportation Research Record 1805.