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. 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.

MMNL model starting value cost variable

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
dce.farmers
Posts: 8
Joined: 03 Sep 2024, 16:27

MMNL model starting value cost variable

Post by dce.farmers »

Dear apollo users,

I am getting this error code in apollo when estimating a MMNL model, assuming here that the parameter of my cost attibute is distributed as lognormal and the rest of my parameters are distributed as normal.

Erreur dans apollo_mnl(c(mnl_settings, componentName2 = "model"), functionality) :
CALCULATION ISSUE - Some utilities for model component "model" contain values that are not finite numbers!

I did try with finite numbers as starting values as mean_log_b_cost = -3 and sd_log_b_cost = 0, but then I get the following error code: Erreur dans apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
CALCULATION ISSUE - Log-likelihood calculation fails at values close to the starting values!

I wonder if this is due to how my cost variable is coded, my levels are high amount of money: 45 000, 50 000, 55 000, 60 000 euros, I therefore get a very small negative coeff for this attribute when estimating a regular mnl as -9.674e-05.

This is my code below.

Thanks in advance for the help.

Code: Select all

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

parallel::detectCores()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_uncorrelated",
  modelDescr      = "Mixed logit model WITH sobol draws",
  indivID         = "RID",  
  nCores          = 11,
  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)
setwd("C:/Users/***/Nextcloud/Documents/Choice Experiment/Apollo")
database = read.csv("widedata.csv",header=TRUE)
### for data dictionary, use ?apollo_swissRouteChoiceData

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_alt3              = 0,
                mean_b_suppnone       = 0,
                sd_b_suppnone         = 0,
                mean_b_suppindiv      = 0.48,
                sd_b_suppindiv        = 0,
                mean_b_suppcoll       = 0.60,
                sd_b_suppcoll         = 0,
                mean_b_tvxindiv       = 0,
                sd_b_tvxindiv         = 0,
                mean_b_tvxindgp       = -0.04,
                sd_b_tvxindgp         = 0,
                mean_b_tvxcoop        = -0.10,
                sd_b_tvxcoop          = 0,
                mean_b_pulvindiv      = 0,
                sd_b_pulvindiv        = 0,
                mean_b_pulvindgp      = -0.64,
                sd_b_pulvindgp        = 0,
                mean_b_pulvcoop       = -0.84,
                sd_b_pulvcoop         = 0,
                mean_b_techindiv      = 0, 
                sd_b_techindiv        = 0,
                mean_b_techcoll       = -0.22,
                sd_b_techcoll         = 0,
                mean_log_b_cost       = NA,
                sd_log_b_cost         = NA)

### 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("mean_b_suppnone", "sd_b_suppnone", "mean_b_tvxindiv", "sd_b_tvxindiv", "mean_b_pulvindiv", "sd_b_pulvindiv", "mean_b_techindiv","sd_b_techindiv")

# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "sobol",
  interNDraws    = 500,
  interNormDraws = c("draws_suppindiv","draws_suppcoll","draws_tvxindgp","draws_tvxcoop", "draws_cost", "draws_pulvindgp", "draws_pulvcoop", "draws_techcoll")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()

  randcoeff[["b_suppindiv"]] = mean_b_suppindiv + sd_b_suppindiv * draws_suppindiv
  randcoeff[["b_suppcoll"]] = mean_b_suppcoll + sd_b_suppcoll * draws_suppcoll
  randcoeff[["b_tvxindgp"]] = mean_b_tvxindgp + sd_b_tvxindgp * draws_tvxindgp
  randcoeff[["b_tvxcoop"]] = mean_b_tvxcoop + sd_b_tvxcoop * draws_tvxcoop
  randcoeff[["b_cost"]] = -exp( mean_log_b_cost + sd_log_b_cost * draws_cost )
  randcoeff[["b_pulvindgp"]] = mean_b_pulvindgp + sd_b_pulvindgp * draws_pulvindgp
  randcoeff[["b_pulvcoop"]] = mean_b_pulvcoop + sd_b_pulvcoop * draws_pulvcoop
  randcoeff[["b_techcoll"]] = mean_b_techcoll + sd_b_techcoll * draws_techcoll


  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"]] = mean_b_suppnone*(SUPP1==1) + b_suppindiv*(SUPP1==2) + b_suppcoll*(SUPP1==3) + mean_b_tvxindiv*(TVX1==1) + b_tvxindgp*(TVX1==2) + b_tvxcoop*(TVX1==3) + b_cost*COST1 + mean_b_pulvindiv*(PULV1==1) + b_pulvindgp*(PULV1==2) + b_pulvcoop*(PULV1==3) + mean_b_techindiv*(TECH1==1) + b_techcoll*(TECH1==2)                      
  V[["alt2"]] = mean_b_suppnone*(SUPP2==1) + b_suppindiv*(SUPP2==2) + b_suppcoll*(SUPP2==3) + mean_b_tvxindiv*(TVX2==1) + b_tvxindgp*(TVX2==2) + b_tvxcoop*(TVX2==3) + b_cost*COST2 + mean_b_pulvindiv*(PULV2==1) + b_pulvindgp*(PULV2==2) + b_pulvcoop*(PULV2==3) + mean_b_techindiv*(TECH2==1) + b_techcoll*(TECH2==2)                      
  V[["alt3"]] = asc_alt3 + mean_b_suppnone*(SUPP3==1)

  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c("alt1"=1, "alt2"=2, "alt3"=3), 
    avail = 1,
    choiceVar     = prefapollo,
    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)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
stephanehess
Site Admin
Posts: 1235
Joined: 24 Apr 2020, 16:29

Re: MMNL model starting value cost variable

Post by stephanehess »

Hi

you cannot start parameters as NA, that is not meaningful

Try making the mu parameter for the lognormal distribution more negative, say -7.

You could also think about rescaling your cost attribute, e.g. express it in 1000s

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
dce.farmers
Posts: 8
Joined: 03 Sep 2024, 16:27

Re: MMNL model starting value cost variable

Post by dce.farmers »

Thank you Stephane! It worked by setting the starting value of my cost attribute to -7!

I have a question regarding the sign of my cost coefficients.

Here are the results I get:

Code: Select all

Model name                                  : MMNL_uncorrelated
Model description                           : Mixed logit model WITH sobol draws and cost attribute as log normal
Model run at                                : 2024-09-23 15:44:07.805383
Estimation method                           : bgw
Model diagnosis                             : Relative function convergence
Optimisation diagnosis                      : Maximum found
     hessian properties                     : Negative definite
     maximum eigenvalue                     : -0.294793
     reciprocal of condition number         : 0.000574256
Number of individuals                       : 120
Number of rows in database                  : 720
Number of modelled outcomes                 : 720

Number of cores used                        :  11 
Number of inter-individual draws            : 1000 (sobol)

LL(start)                                   : -25771.31
LL at equal shares, LL(0)                   : -791
LL at observed shares, LL(C)                : -656.96
LL(final)                                   : -510.15
Rho-squared vs equal shares                  :  0.3551 
Adj.Rho-squared vs equal shares              :  0.3323 
Rho-squared vs observed shares               :  0.2235 
Adj.Rho-squared vs observed shares           :  0.1991 
AIC                                         :  1056.3 
BIC                                         :  1138.73 

Estimated parameters                        : 18
Time taken (hh:mm:ss)                       :  00:02:1.77 
     pre-estimation                         :  00:00:29.55 
     estimation                             :  00:00:41.59 
          initial estimation                :  00:00:38.92 
          estimation after rescaling        :  00:00:2.68 
     post-estimation                        :  00:00:50.63 
Iterations                                  :  49  
     initial estimation                     :  48 
     estimation after rescaling             :  1 

Unconstrained optimisation.

Estimates:
                    Estimate        s.e.   t.rat.(0)  p(1-sided)    Rob.s.e. Rob.t.rat.(0)
b_asc_alt3        -12.306161      1.7455    -7.05004   8.943e-13     1.78435      -6.89671
sd_asc_alt3         3.208970      0.6643     4.83087   6.797e-07     0.70401       4.55812
mean_b_suppnone     0.000000          NA          NA          NA          NA            NA
sd_b_suppnone       0.000000          NA          NA          NA          NA            NA
mean_b_suppindiv    0.747774      0.2536     2.94865    0.001596     0.27478       2.72135
sd_b_suppindiv     -0.948029      0.3828    -2.47687    0.006627     0.35116      -2.69973
mean_b_suppcoll     0.950345      0.2859     3.32432  4.4317e-04     0.29049       3.27149
sd_b_suppcoll      -1.150911      0.3337    -3.44859  2.8176e-04     0.28961      -3.97394
mean_b_tvxindiv     0.000000          NA          NA          NA          NA            NA
sd_b_tvxindiv       0.000000          NA          NA          NA          NA            NA
mean_b_tvxindgp     0.007379      0.2680     0.02753    0.489019     0.27362       0.02697
sd_b_tvxindgp       1.027454      0.4145     2.47864    0.006594     0.46579       2.20584
mean_b_tvxcoop     -0.146975      0.2695    -0.54539    0.292744     0.27670      -0.53117
sd_b_tvxcoop       -0.724429      0.5089    -1.42343    0.077305     0.54254      -1.33525
mean_b_pulvindiv    0.000000          NA          NA          NA          NA            NA
sd_b_pulvindiv      0.000000          NA          NA          NA          NA            NA
mean_b_pulvindgp   -0.999544      0.2572    -3.88630   5.089e-05     0.31178      -3.20596
sd_b_pulvindgp     -1.276843      0.3723    -3.42946  3.0239e-04     0.46066      -2.77176
mean_b_pulvcoop    -1.409081      0.3099    -4.54731   2.717e-06     0.40153      -3.50924
sd_b_pulvcoop       0.867854      0.4619     1.87899    0.030123     0.59837       1.45038
mean_b_techindiv    0.000000          NA          NA          NA          NA            NA
sd_b_techindiv      0.000000          NA          NA          NA          NA            NA
mean_b_techcoll    -0.443518      0.1924    -2.30508    0.010581     0.20797      -2.13256
sd_b_techcoll      -1.135895      0.3026    -3.75402   8.701e-05     0.29163      -3.89493
mean_log_b_cost    -8.848881      0.1841   -48.05938    0.000000     0.19189     -46.11417
sd_log_b_cost       0.105516      0.1013     1.04113    0.148907     0.08847       1.19261
                  p(1-sided)
b_asc_alt3         2.661e-12
sd_asc_alt3        2.581e-06
mean_b_suppnone           NA
sd_b_suppnone             NA
mean_b_suppindiv    0.003251
sd_b_suppindiv      0.003470
mean_b_suppcoll   5.3492e-04
sd_b_suppcoll      3.535e-05
mean_b_tvxindiv           NA
sd_b_tvxindiv             NA
mean_b_tvxindgp     0.489242
sd_b_tvxindgp       0.013698
mean_b_tvxcoop      0.297651
sd_b_tvxcoop        0.090898
mean_b_pulvindiv          NA
sd_b_pulvindiv            NA
mean_b_pulvindgp  6.7306e-04
sd_b_pulvindgp      0.002788
mean_b_pulvcoop   2.2470e-04
sd_b_pulvcoop       0.073477
mean_b_techindiv          NA
sd_b_techindiv            NA
mean_b_techcoll     0.016480
sd_b_techcoll      4.911e-05
mean_log_b_cost     0.000000
sd_log_b_cost       0.116512
And here are the values I get after applying the Delta method to my cost coefficient:

Code: Select all

Running Delta method computations
                                                      Value Robust s.e. Rob t-ratio (0)
Mean for exp(N( mean_log_b_cost , sd_log_b_cost ) 1.443e-04   2.706e-05           5.334
SD for exp(N( mean_log_b_cost , sd_log_b_cost )   1.527e-05   1.185e-05           1.288
1. I am a bit uncertain what results I should report in my table, the coefficient after the Delta method transformation or the log coeff? First, I just want to make sure that, even though I obtain a positive coefficient of 0.00001443 for the effect of cost after using the Delta method , I can still interpret that the effect is negative, as shown in the table before of -8.84888? If I shoud report the coeff with the delta method, should I put a negative sign in front of 0.0000144 then when I report?

2. I have then a question about how I should interpret the WTP based on the signs. This is what I get with unconditionals for for instance SUPPINDIV:

Code: Select all

> wtp_suppindiv = as.vector(unconditionals[["b_suppindiv"]])/as.vector(unconditionals[["b_cost"]])
> (mean(wtp_suppindiv)); (sd(wtp_suppindiv))
[1] -5238.444
[1] 6700.564
2.1: Is it enough to use unconditionals to get the WTP in my case? I have read some papers which use the delta method so I am a bit unsure.
2.2: How should I interpret the negative sign? I am still a bit unsure about the sign that the coefficient of my cost attribute actually has but, if I assume that it is negative (see question 1.), does a negative sign for the WTP means that my farmers are willing to pay 5238 euros to get a personalized support (SUPPINDIV) instead of no support at all (SUPPNONE which is my reference level)?

Thanks!
Last edited by dce.farmers on 23 Sep 2024, 15:15, edited 2 times in total.
stephanehess
Site Admin
Posts: 1235
Joined: 24 Apr 2020, 16:29

Re: MMNL model starting value cost variable

Post by stephanehess »

Hi

in apollo_randCoeff, you use -exp(...), so the cost coefficient is negative. In your delta method calculations, you are seeing the results without the minus sign, so for a positive lognormal. So your mean is actually -1.443e-04

regarding wtp, unconditionals is fine for you, I believe. The sign is because you are taking the ratio of a positive coefficient for a desirable attribute and a negative coefficient for cost. So the WTP for personalized support is actually positive

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
dce.farmers
Posts: 8
Joined: 03 Sep 2024, 16:27

Re: MMNL model starting value cost variable

Post by dce.farmers »

Hi Stephane,

Actually, regarding your last reply, I am confused whether I should define what I find as WTP or WTA. My guess is that I have both because I have both positive and negative signs for my attributes coefficients.

Below on the picture, how I summarized my results, it is a WTA when the sign is positive since it is the amount they need to receive (decrease of cost) to accept a worsen value in the attribute (I obtained a negative coefficient for these attributes) while the others are WTP when the sign is negative since this is the amont of money they are willing to pay for an improvement of level for the attribute (I obtained positive coefficients for these attributes)

Image

Thanks for the clarification!
Attachments
WTP_WTA.png
WTP_WTA.png (72.64 KiB) Viewed 4039 times
stephanehess
Site Admin
Posts: 1235
Joined: 24 Apr 2020, 16:29

Re: MMNL model starting value cost variable

Post by stephanehess »

Hi

many apologies for the slow reply.

Basically, if your model does not use separate coefficients for cost increases and decreases (or if your monetary attribute is only ever cost or only ever savings), then you can interpret all your monetary valuations as both WTP and WTA.

Imagine that your monetary attribute is cost, so the coefficient is negative, i.e. beta_c.

Then you have a desirable attribute, which has a positive coefficient, beta_k.

Then MRS=beta_k/beta_c will be negative. This will mean that people are willing to pay -MRS to obtain an increase in the desirable attribute, i.e. a WTP, but also that they are willing to accept a decrease in K in return for -MRS, i.e. a WTA. They are purely symmetrical in such a specification

Hope this helps

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