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.

warning in MMNL

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

warning in MMNL

Post by qcng »

Hi,

I have tried to apply the MMNL (using the codes in website: MMNL_preference_space) with my data.
There are five attributes: barn, range, oragnic, vitamin, price. The first four are dummy variables, the last one is continuous variable.
I set random heterogeneity for the first four attributes as follows

Code: Select all

  randcoeff[["b_barn"]]    = -exp(mu_log_b_barn    + sigma_log_b_barn    * draws_barn)
  randcoeff[["b_range"]]   = -exp(mu_log_b_range   + sigma_log_b_range   * draws_range)
  randcoeff[["b_organic"]] = -exp(mu_log_b_organic + sigma_log_b_organic * draws_organic)
  randcoeff[["b_vitamin"]] = -exp(mu_log_b_vitamin + sigma_log_b_vitamin * draws_vitamin)
Here is my entire codes

Code: Select all

### Clear memory
rm(list = ls())

### Load libraries
library(openxlsx)
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_preference_space",
  modelDescr      = "Mixed logit model choice data, uncorrelated Lognormals in preference space",
  indivID         = "ID",  
  nCores          = 4,
  outputDirectory = "output"
)


# ####################################################### #
#### 2. Data loading                                   ####
# ####################################################### #
dtaApollo <- read.xlsx("dtaApollo.xlsx", sheet = "data", colNames = T)
database <- dtaApollo


# ####################################################### #
#### 3. Parameter definition                           ####
# ####################################################### #

### Vector of parameters, including any that are kept fixed during estimation
apollo_beta=c(asc_alt3      = 0,
      
              mu_log_b_barn     = 0,
              sigma_log_b_barn  = 0,
              
              mu_log_b_range     = 0,
              sigma_log_b_range  = 0,
              
              mu_log_b_organic     = 0,
              sigma_log_b_organic  = 0,
              
              mu_log_b_vitamin     = 0,
              sigma_log_b_vitamin  = 0,
              
              # b_barn  = 0,
              # b_range = 0,
              b_price = 0)

### Vector with names (in quotes) of parameters to be  kept fixed at their starting value in apollo_beta.
### Use apollo_beta_fixed = c() for no fixed parameters.
apollo_fixed = c()

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_barn","draws_range","draws_organic","draws_vitamin"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_barn"]]    = -exp(mu_log_b_barn    + sigma_log_b_barn    * draws_barn)
  randcoeff[["b_range"]]   = -exp(mu_log_b_range   + sigma_log_b_range   * draws_range)
  randcoeff[["b_organic"]] = -exp(mu_log_b_organic + sigma_log_b_organic * draws_organic)
  randcoeff[["b_vitamin"]] = -exp(mu_log_b_vitamin + sigma_log_b_vitamin * draws_vitamin)
  
  return(randcoeff)
}


# ####################################################### #
#### 4. Input validation                               ####
# ####################################################### #

apollo_inputs = apollo_validateInputs()


# ####################################################### #
#### 5. Likelihood definition                          ####
# ####################################################### #

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()
  V[["alt1"]]  =            b_barn * (barn_alt1 == 1) + b_range * (range_alt1 == 1) + b_organic * (organic_alt1 == 1) + b_vitamin * (vitamin_alt1 == 1) + b_price * price_alt1
  V[["alt2"]]  =            b_barn * (barn_alt2 == 1) + b_range * (range_alt2 == 1) + b_organic * (organic_alt2 == 1) + b_vitamin * (vitamin_alt2 == 1) + b_price * price_alt2
  V[["alt3"]]  = asc_alt3 + b_barn * (barn_alt3 == 1) + b_range * (range_alt3 == 1) + b_organic * (organic_alt3 == 1) + b_vitamin * (vitamin_alt3 == 1) + b_price * price_alt3
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2, alt3=3), 
    avail         = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3), 
    choiceVar     = choice,
    utilities     = 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)
}


# ####################################################### #
#### 6. Model estimation and reporting                 ####
# ####################################################### #

model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, 
                        apollo_inputs)

apollo_modelOutput(model)  #to screen

apollo_saveOutput(model)   #to file
However I got the warnings
WARNING: Singular Hessian, cannot calculate s.e.
Could not write hessian to a file.

WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle point!

For the results, it seems not good as mu_log is very high compared to b_price.
Preparing user-defined functions.

Testing likelihood function...

Overview of choices for MNL model component :
alt1 alt2 alt3
Times available 2960.00 2960.00 2960.00
Times chosen 955.00 1307.00 698.00
Percentage chosen overall 32.26 44.16 23.58
Percentage chosen when available 32.26 44.16 23.58


Pre-processing likelihood function...
Creating cluster...
Preparing workers for multithreading...

Testing influence of parameters
Starting main estimation
Initial function value: -4552.701
Initial gradient value:
asc_alt3 mu_log_b_barn sigma_log_b_barn mu_log_b_range sigma_log_b_range
-1.065371e+03 -3.578956e+02 4.327060e-02 -4.779576e+02 -1.088076e-01
mu_log_b_organic sigma_log_b_organic mu_log_b_vitamin sigma_log_b_vitamin b_price
-4.221774e+02 2.523584e-02 -6.998596e+02 -3.929654e-01 1.276747e+03
initial value 4552.700594
iter 2 value 4284.763351
iter 3 value 3064.798184
iter 4 value 3043.632602
iter 5 value 3031.589350
iter 6 value 2855.622805
iter 7 value 2821.452787
iter 8 value 2818.394461
iter 9 value 2809.551204
iter 10 value 2798.110528
iter 11 value 2761.134041
iter 12 value 2751.150277
iter 13 value 2738.469340
iter 14 value 2732.273568
iter 15 value 2726.800654
iter 16 value 2714.178608
iter 17 value 2703.314911
iter 18 value 2691.460221
iter 19 value 2686.312072
iter 20 value 2682.469001
iter 21 value 2679.635570
iter 22 value 2677.734367
iter 23 value 2677.731758
iter 24 value 2677.713236
iter 25 value 2677.534510
iter 26 value 2677.352348
iter 27 value 2676.963312
iter 28 value 2676.203104
iter 29 value 2675.747670
iter 30 value 2675.392379
iter 31 value 2675.183195
iter 32 value 2675.052192
iter 33 value 2674.990024
iter 34 value 2674.967226
iter 35 value 2674.912472
iter 36 value 2674.869342
iter 37 value 2674.840648
iter 38 value 2674.818647
iter 39 value 2674.805682
iter 40 value 2674.798233
iter 41 value 2674.792189
iter 42 value 2674.783433
iter 43 value 2674.764274
iter 44 value 2674.764156
iter 45 value 2674.764008
iter 46 value 2674.760705
iter 47 value 2674.757750
iter 48 value 2674.755915
iter 49 value 2674.754337
iter 50 value 2674.753013
iter 51 value 2674.751115
iter 52 value 2674.750099
iter 53 value 2674.748753
iter 54 value 2674.738955
iter 55 value 2674.711932
iter 56 value 2674.671580
iter 57 value 2674.629577
iter 58 value 2674.281864
iter 59 value 2673.938510
iter 60 value 2673.894603
iter 61 value 2673.859675
iter 62 value 2673.850345
iter 63 value 2673.830013
iter 64 value 2673.819293
iter 65 value 2673.818798
iter 66 value 2673.817939
iter 67 value 2673.813243
iter 68 value 2673.804321
iter 69 value 2673.791031
iter 70 value 2673.775546
iter 70 value 2673.775531
final value 2673.775531
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their
current estimates and additional iterations will be performed.
initial value 2673.775531
iter 2 value 2673.733456
iter 3 value 2673.717491
iter 4 value 2673.712277
iter 5 value 2673.708861
iter 6 value 2673.636402
iter 7 value 2673.610078
iter 8 value 2673.513912
iter 9 value 2673.447813
iter 10 value 2673.398029
iter 11 value 2673.384613
iter 12 value 2673.381160
iter 13 value 2673.377515
iter 13 value 2673.377515
final value 2673.377515
converged

Estimated parameters:
Estimate
asc_alt3 -2.4205
mu_log_b_barn -184.3784
sigma_log_b_barn 205.8515
mu_log_b_range -22.4080
sigma_log_b_range -1.9684
mu_log_b_organic -21.1239
sigma_log_b_organic -2.6224
mu_log_b_vitamin -7.5396
sigma_log_b_vitamin -6.6235
b_price -0.7075

Final LL: -2673.3775

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 analytical gradient.
0%....25%....50%....75%.100% (20 NA values)
Computing covariance matrix using numerical methods (numDeriv).
0%....25%....50%....75%....100% (36 NA values)
Computing covariance matrix using numerical methods (maxLik). This may take a while, no
progress bar displayed.
I guess, there is something wrong in my model definition for the mixed logit. Do you have any idea what that might be?

Thanks very much.

--
Cuong
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: warning in MMNL

Post by stephanehess »

Hi

the model has gone to an unidentified solution. Are you sure the negative lognormal distributions make sense here as that would imply purely negative utilities for that level? Also, why keep the price coefficient fixed as that is likely to be where most of the heterogeneity is

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: warning in MMNL

Post by qcng »

Dear Prof. Hess,

I have changed the parameters of function apollo_randCoeff in which b_price is also randomed as shown in the following, and then it works

Code: Select all

apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_barn"]]    = mu_b_barn    + sigma_b_barn    * draws_barn
  randcoeff[["b_range"]]   = mu_b_range   + sigma_b_range   * draws_range
  randcoeff[["b_organic"]] = mu_b_organic + sigma_b_organic * draws_organic
  randcoeff[["b_vitamin"]] = mu_b_vitamin + sigma_b_vitamin * draws_vitamin
  
  randcoeff[["b_price"]]   = -exp(mu_log_b_price + sigma_log_b_price * draws_price)
  
  return(randcoeff)
}
Thanks for explanation.

Best,
Cuong
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: warning in MMNL

Post by qcng »

Dear Prof. Hess,

I have tried the same approach with the previous one, but different data.
In this data, there are 4 binary non-price attributes (ing_1, ing_2, fat, carb) and continuous price attribute.

I ran MNL that works well, but when I ran MMNL, the errors/warnings happen agian
WARNING: Singular Hessian, cannot calculate s.e.
Could not write hessian to a file.
WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle point!
Here the codes that I used

Code: Select all

# ####################################################### #
#### 1. Definition of core settings                        
# ####################################################### #
### Clear memory
rm(list = ls())

### Load libraries
library(openxlsx)
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_preference_space",
  modelDescr      = "Mixed logit model choice data, uncorrelated Lognormals in preference space",
  indivID         = "ID",  
  nCores          = 4,
  outputDirectory = "output"
)


# ####################################################### #
#### 2. Data loading                                   ####
# ####################################################### #
dataApollo <- read.xlsx("dataApollo.xlsx", sheet = "data", colNames = T)
database <- dataApollo


# ####################################################### #
#### 3. Parameter definition                           ####
# ####################################################### #

### Vector of parameters, including any that are kept fixed during estimation
apollo_beta=c(asc_alt3      = 0,
              
              mu_b_ing_1    = 0,
              sigma_b_ing_1 = 0,

              mu_b_ing_2    = 0,
              sigma_b_ing_2 = 0,

              mu_b_fat      = 0,
              sigma_b_fat   = 0,

              mu_b_carb     = 0,
              sigma_b_carb  = 0,

              mu_log_b_price    = 0,
              sigma_log_b_price = 0)

### Vector with names (in quotes) of parameters to be  kept fixed at their starting value in apollo_beta.
### Use apollo_beta_fixed = c() for no fixed parameters.
apollo_fixed = c()

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_ing_1","draws_ing_2","draws_fat","draws_carb", "draws_price"),  
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_ing_1"]] = mu_b_ing_1 + sigma_b_ing_1 * draws_ing_1
  randcoeff[["b_ing_2"]] = mu_b_ing_2 + sigma_b_ing_2 * draws_ing_2
  randcoeff[["b_fat"]]   = mu_b_fat   + sigma_b_fat   * draws_fat  
  randcoeff[["b_carb"]]  = mu_b_carb  + sigma_b_carb  * draws_carb
  
  randcoeff[["b_price"]]   = -exp(mu_log_b_price + sigma_log_b_price * draws_price)
  
  return(randcoeff)
}


# ####################################################### #
#### 4. Input validation                               ####
# ####################################################### #

apollo_inputs = apollo_validateInputs()

# ####################################################### #
#### 5. Likelihood definition                          ####
# ####################################################### #

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()
  V[["alt1"]]  = (           b_ing_1 * (ingredient_alt1 == 1) + b_ing_2 * (ingredient_alt1 == 2) + b_fat * fat_alt1 + b_carb * carbon_alt1 + b_price * price_alt1)
  V[["alt2"]]  = (           b_ing_1 * (ingredient_alt2 == 1) + b_ing_2 * (ingredient_alt2 == 2) + b_fat * fat_alt2 + b_carb * carbon_alt2 + b_price * price_alt2)
  V[["alt3"]]  = (asc_alt3 + b_ing_1 * (ingredient_alt3 == 1) + b_ing_2 * (ingredient_alt3 == 2) + b_fat * fat_alt3 + b_carb * carbon_alt3 + b_price * price_alt3)
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2, alt3=3), 
    avail         = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3), 
    choiceVar     = choice,
    utilities     = 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)
}


# ####################################################### #
#### 6. Model estimation and reporting                 ####
# ####################################################### #

model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, 
                        apollo_inputs)
Could you please have a look and give me some advices?

Thanks in advance,

Cuong
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: warning in MMNL

Post by stephanehess »

Hi

can you please show us the parameter estimates? Otherwise, there's no way to diagnose what's going wrong

Thanks

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: warning in MMNL

Post by qcng »

Hi,

Below is the estimate results
Preparing user-defined functions.

Testing likelihood function...

Overview of choices for MNL model component :
alt1 alt2 alt3
Times available 22293.00 22293.00 22293.00
Times chosen 9174.00 8066.00 5053.00
Percentage chosen overall 41.15 36.18 22.67
Percentage chosen when available 41.15 36.18 22.67


Pre-processing likelihood function...
Creating cluster...
Preparing workers for multithreading...

Testing influence of parameters
Starting main estimation
Initial function value: -239974.8
Initial gradient value:
asc_alt3 mu_b_ing_1 sigma_b_ing_1 mu_b_ing_2 sigma_b_ing_2
-1.714053e+04 5.536185e+03 5.443775e-01 3.174495e+03 -1.572201e-01
mu_b_fat sigma_b_fat mu_b_carb sigma_b_carb mu_log_b_price
8.174426e+03 -8.308839e-01 9.303272e+03 -6.939595e-01 -2.393776e+05
sigma_log_b_price
1.482451e-01
initial value 239974.803882
iter 2 value 90038.255654
iter 3 value 37132.380904
iter 4 value 27941.749721
iter 5 value 24973.425593
iter 6 value 22734.172307
iter 7 value 21388.541308
iter 8 value 21322.499414
iter 9 value 21285.772282
iter 10 value 21208.521347
iter 11 value 21046.658189
iter 12 value 19599.003905
iter 13 value 19576.492171
iter 14 value 19456.334413
iter 15 value 19432.667626
iter 16 value 19369.433591
iter 17 value 19329.099211
iter 18 value 19319.521896
iter 19 value 19302.213445
iter 20 value 19270.622441
iter 21 value 19247.327413
iter 22 value 19240.363721
iter 23 value 19239.680918
iter 24 value 19239.325259
iter 25 value 19239.148165
iter 26 value 19239.010207
iter 27 value 19238.913575
iter 28 value 19238.872991
iter 29 value 19238.857770
iter 30 value 19238.856904
iter 31 value 19238.856353
iter 32 value 19238.849116
iter 33 value 19238.839716
iter 34 value 19238.791376
iter 35 value 19238.784784
iter 35 value 19238.784784
iter 35 value 19238.784784
final value 19238.784784
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their
current estimates and additional iterations will be performed.
initial value 19238.784784
iter 1 value 19238.784784
final value 19238.784784
converged

Estimated parameters:
Estimate
asc_alt3 -1.3203
mu_b_ing_1 -1.0644
sigma_b_ing_1 2.2669
mu_b_ing_2 -2.6634
sigma_b_ing_2 -2.7271
mu_b_fat -0.2406
sigma_b_fat -1.1183
mu_b_carb 0.3742
sigma_b_carb -0.8624
mu_log_b_price -77.6079
sigma_log_b_price 4.806e-05

Final LL: -19238.7848

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
INFORMATION: Your model took more than 10 minutes to estimate, so it was saved to file
output/MMNL_preference_space_model.rds before calculating its covariance matrix. If
calculation of the covariance matrix fails or is stopped before finishing, you can load
the model up to this point using apollo_loadModel. You may also want to inspect the
approximate BHHH standard errors shown above to determine whether you wish to continue
this process.
Computing covariance matrix using analytical gradient.
0%....25%....50%....75%..100%
Best regards,

Cuong
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: warning in MMNL

Post by stephanehess »

Hi

can you also share the output for the MNL model on the same data, with the corresponding specification

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: warning in MMNL

Post by qcng »

Dear Prof. Hess,

Below are codes and estimates from MNL model of the same data.
The codes

Code: Select all

# anal. Apollo ------------------------------------------------------------

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MNL_preference",
  modelDescr      = "Simple MNL model on mode choice SP data, in preference space",
  indivID         = "ID",
  outputDirectory = "output"
)


# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

dataApollo <- read.xlsx("dataApollo.xlsx", sheet = "data", colNames = T)
database <- dataApollo

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_alt3      = 0,

              b_ing_1       = 0,
              b_ing_2       = 0,
              b_fat         = 0,
              b_carb        = 0,
              
              b_price       = 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()

# ################################################################# #
#### 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()
  V[["alt1"]]  = (           b_ing_1 * (ingredient_alt1 == 1) + b_ing_2 * (ingredient_alt1 == 2) + b_fat * fat_alt1 + b_carb * carbon_alt1 + b_price * price_alt1)
  V[["alt2"]]  = (           b_ing_1 * (ingredient_alt2 == 1) + b_ing_2 * (ingredient_alt2 == 2) + b_fat * fat_alt2 + b_carb * carbon_alt2 + b_price * price_alt2)
  V[["alt3"]]  = (asc_alt3 + b_ing_1 * (ingredient_alt3 == 1) + b_ing_2 * (ingredient_alt3 == 2) + b_fat * fat_alt3 + b_carb * carbon_alt3 + b_price * price_alt3)
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1, alt2=2, alt3=3), 
    avail         = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3), 
    choiceVar     = choice,
    utilities     = 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)
  
  ### 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)
And the optput
Model run by THINKPAD using Apollo 0.2.9 on R 4.3.1 for Windows.
www.ApolloChoiceModelling.com

Model name : MNL_preference
Model description : Simple MNL model on mode choice SP data, in preference space
Model run at : 2023-07-21 10:25:05.278945
Estimation method : bfgs
Model diagnosis : successful convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definitive
maximum eigenvalue : -727.034153
Number of individuals : 2477
Number of rows in database : 22293
Number of modelled outcomes : 22293

Number of cores used : 1
Model without mixing

LL(start) : -24491.36
LL at equal shares, LL(0) : -24491.36
LL at observed shares, LL(C) : -23845.73
LL(final) : -20663.89
Rho-squared vs equal shares : 0.1563
Adj.Rho-squared vs equal shares : 0.156
Rho-squared vs observed shares : 0.1334
Adj.Rho-squared vs observed shares : 0.1333
AIC : 41339.78
BIC : 41387.85

Estimated parameters : 6
Time taken (hh:mm:ss) : 00:00:7.37
pre-estimation : 00:00:2.85
estimation : 00:00:2.3
initial estimation : 00:00:2.01
estimation after rescaling : 00:00:0.29
post-estimation : 00:00:2.22
Iterations : 14
initial estimation : 13
estimation after rescaling : 1

Unconstrained optimisation.

Estimates:
Estimate s.e. t.rat.(0) p(1-sided) Rob.s.e. Rob.t.rat.(0) p(1-sided)
asc_alt3 -1.99236 0.030173 -66.03 0.000 0.039240 -50.773 0.000
b_ing_1 -0.68717 0.022406 -30.67 0.000 0.038255 -17.963 0.000
b_ing_2 -1.40654 0.025450 -55.27 0.000 0.043669 -32.209 0.000
b_fat -0.19049 0.017732 -10.74 0.000 0.022496 -8.468 0.000
b_carb 0.22643 0.017531 12.92 0.000 0.018766 12.066 0.000
b_price -0.05339 0.001008 -52.97 0.000 0.001623 -32.889 0.000
Please have a look.

Thanks very much.

Cuong
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: warning in MMNL

Post by stephanehess »

Have you tried using the MNL results as starting values? So for the parameters that are normally distributed, use the MNL estimate as the starting value for the mean, and for the lognormally distributed one, use the log of the absolute value of the MNL estimate as the starting value for the mean of log(-beta)
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: warning in MMNL

Post by qcng »

Dear Prof. Hess,

I have set the estimates from MNL model as the starting values

Code: Select all

apollo_beta=c(asc_alt3      = -1.99236,
              
              mu_b_ing_1    = -0.68717,
              sigma_b_ing_1 = 0,

              mu_b_ing_2    = -1.40654,
              sigma_b_ing_2 = 0,

              mu_b_fat      = -0.19049,
              sigma_b_fat   = 0,

              mu_b_carb     = 0.22643,
              sigma_b_carb  = 0,

              mu_log_b_price    = -2.93013,
              sigma_log_b_price = 0)
The MMNL model works.
Preparing user-defined functions.

Testing likelihood function...

Overview of choices for MNL model component :
alt1 alt2 alt3
Times available 22293.00 22293.00 22293.00
Times chosen 9174.00 8066.00 5053.00
Percentage chosen overall 41.15 36.18 22.67
Percentage chosen when available 41.15 36.18 22.67


Pre-processing likelihood function...
Creating cluster...
Preparing workers for multithreading...

Testing influence of parameters
Starting main estimation
Initial function value: -20663.89
Initial gradient value:
asc_alt3 mu_b_ing_1 sigma_b_ing_1 mu_b_ing_2 sigma_b_ing_2
0.01741329 -0.04277788 0.58865999 -0.03427876 -0.07455316
mu_b_fat sigma_b_fat mu_b_carb sigma_b_carb mu_log_b_price
-0.03584466 -0.61035277 -0.01822438 -0.31646622 0.02350294
sigma_log_b_price
-0.32400318
initial value 20663.888640
iter 2 value 19281.253297
iter 3 value 18456.698098
iter 4 value 17147.224237
iter 5 value 16776.439315
iter 6 value 16698.715027
iter 7 value 16667.381772
iter 8 value 16666.344466
iter 9 value 16664.734105
iter 10 value 16660.956125
iter 11 value 16659.224129
iter 12 value 16643.665426
iter 13 value 16422.165644
iter 14 value 16291.109807
iter 15 value 16256.303946
iter 16 value 15888.403600
iter 17 value 15787.442705
iter 18 value 15716.445100
iter 19 value 15586.556627
iter 20 value 15552.332348
iter 21 value 15535.383593
iter 22 value 15527.922456
iter 23 value 15525.448004
iter 24 value 15524.707569
iter 25 value 15524.635208
iter 26 value 15524.628345
iter 27 value 15524.625659
iter 28 value 15524.623367
iter 29 value 15524.621557
iter 30 value 15524.621283
iter 31 value 15524.621001
iter 31 value 15524.620884
iter 32 value 15524.619830
iter 33 value 15524.618750
iter 33 value 15524.618623
iter 33 value 15524.618623
final value 15524.618623
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their
current estimates and additional iterations will be performed.
initial value 15524.618623
iter 1 value 15524.618607
final value 15524.618607
converged

Estimated parameters with approximate standard errors from BHHH matrix:
Estimate BHHH se BHH t-ratio
asc_alt3 -4.1140 0.04757 -86.481
mu_b_ing_1 -1.3973 0.07797 -17.922
sigma_b_ing_1 3.2493 0.08167 39.785
mu_b_ing_2 -3.4370 0.10165 -33.813
sigma_b_ing_2 -3.7996 0.11165 -34.030
mu_b_fat -0.3099 0.04060 -7.634
sigma_b_fat -1.0845 0.04366 -24.840
mu_b_carb 0.4993 0.03581 13.945
sigma_b_carb -0.9151 0.04150 -22.051
mu_log_b_price -2.3295 0.02671 -87.210
sigma_log_b_price -1.0516 0.02820 -37.297

Final LL: -15524.6186

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
INFORMATION: Your model took more than 10 minutes to estimate, so it was saved to file
output/MMNL_preference_space_model.rds before calculating its covariance matrix. If
calculation of the covariance matrix fails or is stopped before finishing, you can load
the model up to this point using apollo_loadModel. You may also want to inspect the
approximate BHHH standard errors shown above to determine whether you wish to continue
this process.
Computing covariance matrix using analytical gradient.
0%....25%....50%....75%..100%
Negative definite Hessian with maximum eigenvalue: -48.770613
However, the MMNL estimates seem to be doubled as compared to the MNL estimates.
asc_alt3 -1.992355731 asc_alt3 -4.114046722
b_ing_1 -0.687173977 mu_b_ing_1 -1.397313369 sigma_b_ing_1 3.249289222
b_ing_2 -1.406544016 mu_b_ing_2 -3.43699741 sigma_b_ing_2 -3.799584188
b_fat -0.190487091 mu_b_fat -0.309917051 sigma_b_fat -1.084528549
b_carb 0.226431784 mu_b_carb 0.499339878 sigma_b_carb -0.915050842
b_price -0.053389951 mu_log_b_price -2.329519632 sigma_log_b_price -1.051593978
I am not sure if it is because the starting values or something wrong in the setting.

Could you please give me further comments?

Thanks

Cuong
Post Reply