Page 1 of 1

MNL with unlabelled experiment

Posted: 25 Jun 2023, 17:50
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 18823 times

Could you please give me some instruction?

Thanks very much.

Cuong

Re: MNL with unlabelled experiment

Posted: 25 Jun 2023, 21:23
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

Re: MNL with unlabelled experiment

Posted: 25 Jun 2023, 21:59
by qcng
Hi,

I realise this error now. I fixed it as the following
dataExample1.png
dataExample1.png (23.66 KiB) Viewed 18815 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

Re: MNL with unlabelled experiment

Posted: 25 Jun 2023, 22:04
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

Re: MNL with unlabelled experiment

Posted: 25 Jun 2023, 22:26
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

Re: MNL with unlabelled experiment

Posted: 26 Jun 2023, 10:11
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.

Re: MNL with unlabelled experiment

Posted: 26 Jun 2023, 10:21
by qcng
Many thanks Stephane.
I will check them carefully.

--
Cuong