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.

MNL with unlabelled experiment

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

MNL with unlabelled experiment

Post by qcng »

Hi,

I have stated choice experiement data. It is unlabelled experiement, including:
  • 370 individuals
  • 5 attributes: 4 dummy variables (barn, range, organic, vitamin), 1 continuous variable (price)
  • 8 choice sets, each including 3 alternatives (alternative 1, alternative 2, opt-out)
I 'd like to estimate the coefficients of the attributes, and also investiage the heterogeneous preferences.

I have tried to apply the functions in Apollo package, I struggle to set up the apollo_probabilities in the R code.

Here is my R code:

Code: Select all

### Initialise code
apollo_initialise()

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

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

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

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_alt3      = 0,
              
              b_no_barn     = 0,
              b_no_range    = 0,
              b_no_organic  = 0,
              b_no_vitamin  = 0,
              
              b_barn        = 0,
              b_range       = 0,
              b_organic     = 0,
              b_vitamin     = 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("b_no_barn","b_no_range","b_no_organic","b_no_vitamin")

# ################################################################# #
#### 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_no_barn * (barn_alt1 == 0) + b_no_range * (range_alt1 == 0) + b_no_organic * (organic_alt1 == 0) + b_no_vitamin * (vitamin_alt1 == 0) + b_range * (range_alt1 == 1) + b_organic * (organic_alt1 == 1) + b_vitamin * (vitamin_alt1 == 1) + b_price * price_alt1
  V[["alt2"]]  =            b_no_barn * (barn_alt2 == 0) + b_no_range * (range_alt2 == 0) + b_no_organic * (organic_alt2 == 0) + b_no_vitamin * (vitamin_alt2 == 0) + b_range * (range_alt2 == 1) + b_organic * (organic_alt2 == 1) + b_vitamin * (vitamin_alt2 == 1) + b_price * price_alt1
  V[["alt3"]]  = asc_alt3 + b_no_barn * (barn_alt3 == 0) + b_no_range * (range_alt3 == 0) + b_no_organic * (organic_alt3 == 0) + b_no_vitamin * (vitamin_alt3 == 0) + b_range * (range_alt3 == 1) + b_organic * (organic_alt3 == 1) + b_vitamin * (vitamin_alt3 == 1) + b_price * price_alt1
    
  ### 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)
I got the error
Error in apollo_mnl(c(mnl_settings, componentName2 = "model"), functionality) :
SPECIFICATION ISSUE - Only one alternative is available for each observation for model component "model!


Here are first lines of my data:
dataExample.png
dataExample.png (23.57 KiB) Viewed 4218 times

Could you please give me some instruction?

Thanks very much.

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

Re: MNL with unlabelled experiment

Post by stephanehess »

Hi

Apollo is trying to help you with the error message. It's telling you that only one alternative is available in each choice situation in your data. And if you look at your availability conditions, that's exactly what you've set up in your data - see the availability columns.

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

Re: MNL with unlabelled experiment

Post by qcng »

Hi,

I realise this error now. I fixed it as the following
dataExample1.png
dataExample1.png (23.66 KiB) Viewed 4210 times
I run again, but there is a new error
Testing influence of parametersError in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
SPECIFICATION ISSUE - Parameter b_barn does not influence the log-likelihood of your model!


I think there is issue in the way I define the unitlites, isn't it?

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

Re: MNL with unlabelled experiment

Post by stephanehess »

Hi

please try to look carefully at the error messages.

Apollo tells you

Parameter b_barn does not influence the log-likelihood of your model!

And this parameter is not used in your utility functions. So that's the mistake there

Best wishes

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

Re: MNL with unlabelled experiment

Post by qcng »

Hi,

I saw the mistakes in utility functions.
I added the b_barn in this.

I do not know why the coefficient of price is very low. May I define the utility functions wrongly?

Code: Select all

### Initialise code
apollo_initialise()

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


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

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

database <- dtaApollo


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_alt3      = 0,
              
              b_no_barn     = 0,
              b_no_range    = 0,
              b_no_organic  = 0,
              b_no_vitamin  = 0,
              
              b_barn        = 0,
              b_range       = 0,
              b_organic     = 0,
              b_vitamin     = 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("b_no_barn","b_no_range","b_no_organic","b_no_vitamin")

# ################################################################# #
#### 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_no_barn * (barn_alt1 == 0) + b_no_range * (range_alt1 == 0) + b_no_organic * (organic_alt1 == 0) + b_no_vitamin * (vitamin_alt1 == 0) + 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_no_barn * (barn_alt2 == 0) + b_no_range * (range_alt2 == 0) + b_no_organic * (organic_alt2 == 0) + b_no_vitamin * (vitamin_alt2 == 0) + b_barn * (barn_alt2 == 2) + b_range * (range_alt2 == 1) + b_organic * (organic_alt2 == 1) + b_vitamin * (vitamin_alt2 == 1) + b_price * price_alt1
  V[["alt3"]]  = asc_alt3 + b_no_barn * (barn_alt3 == 0) + b_no_range * (range_alt3 == 0) + b_no_organic * (organic_alt3 == 0) + b_no_vitamin * (vitamin_alt3 == 0) + b_barn * (barn_alt3 == 3) + b_range * (range_alt3 == 1) + b_organic * (organic_alt3 == 1) + b_vitamin * (vitamin_alt3 == 1) + b_price * price_alt1
  
  ### 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)
I can not get the approximate standard errors from BHHH matrix, with results as below
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...

Testing influence of parameters
Starting main estimation
Initial function value: -3251.892
Initial gradient value:
asc_alt3 b_barn b_range b_organic b_vitamin b_price
-2.886667e+02 5.133333e+01 2.296667e+02 1.480000e+02 7.333333e+01 5.873080e-13
initial value 3251.892374
iter 2 value 3144.656934
iter 3 value 3111.498991
iter 4 value 3090.377909
iter 5 value 3088.868989
iter 6 value 3088.756421
iter 7 value 3074.276979
iter 8 value 3073.688125
iter 9 value 3073.687416
iter 9 value 3073.687412
iter 9 value 3073.687412
final value 3073.687412
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their
current estimates and additional iterations will be performed.
initial value 3073.687412
iter 1 value 3073.687412
final value 3073.687412
converged

Estimated parameters:
Estimate
asc_alt3 -0.05705
b_no_barn 0.00000
b_no_range 0.00000
b_no_organic 0.00000
b_no_vitamin 0.00000
b_barn 0.59203
b_range 0.95630
b_organic 0.74963
b_vitamin -0.23978
b_price -4.757e-13

Final LL: -3073.6874

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%
With the warning:

WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle point!
Warning message:
In sqrt(diag(varcov)) : NaNs produced


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

Re: MNL with unlabelled experiment

Post by stephanehess »

Hi

again, this is a very easy mistake you made.

You included the same price attribute in all three alternatives (price_alt1)

Please check your utility functions carefully yourself each time you get an error. The forum is mainly for proper problems related to Apollo, and users should first check their specification themselves.
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 17
Joined: 25 Jun 2023, 12:22

Re: MNL with unlabelled experiment

Post by qcng »

Many thanks Stephane.
I will check them carefully.

--
Cuong
Post Reply