I want to use the MNL model for RP and SP data. It is a forced choice with 3 alternatives. I have prepared my dataset based on this example: https://www.apollochoicemodelling.com/f ... NL_RP_SP.r
Codes
Code: Select all
### Set core controls
apollo_control = list(
modelName = "MNL_RP_SP",
modelDescr = "RP-SP model on mode choice data",
indivID = "id",
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)
database = read.csv("input_MNL.csv", header = TRUE)
database$Panelist_Code <- NULL
database <- database[!is.na(database$choice), ]
### for data dictionary, use ?apollo_modeChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation (I deleted b_tasting and b_weekly_coffee_spending, because I do not know how to incorporate that)
apollo_beta=c(asc_1 = 0,
asc_2 = 0,
asc_3 = 0,
b_hedonic = 0, # overall liking
b_robusta = 0,
b_arabica = 0,
b_wo_sugar = 0,
b_half = 0,
b_normal = 0,
b_wo_label = 0,
b_fairtrade = 0,
b_organic = 0,
b_reward = 0, # 3 options of rewards that they chose
b_cluster = 0, # 3 consumer segments based on their attitudinal data
b_age = 0, # age
b_gender = 0, # Female (Binary)
b_BMI = 0, # 5 categories
b_urbanization = 0, # 3 categories
b_education = 0, # 6 categories
b_income = 0, # 5 categories
b_frequency = 0, # 6 categories
mu_SP = 1,
mu_RP = 1)
### 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("asc_1", "mu_SP")
# ################################################################# #
#### 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 (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
# PS: I have not included b_tasting? not sure how to put it also remove the asc_1
V = list()
V[["choice1"]] = asc_1 + b_hedonic * alt1_value + b_robusta * alt1_robusta + b_arabica * alt1_arabica + b_wo_sugar * alt1_wo_sugar + b_half * alt1_half + b_normal * alt1_normal + b_wo_label * alt1_wo_label + b_fairtrade * alt1_fairtrade + b_organic * alt1_organic + b_reward * reward + b_cluster * cluster + b_age * age + b_gender * gender + b_BMI * BMI + b_urbanization * urbanization + b_education * education + b_income * income + b_frequency * frequency
V[["choice2"]] = asc_2 + b_hedonic * alt2_value + b_robusta * alt2_robusta + b_arabica * alt2_arabica + b_wo_sugar * alt2_wo_sugar + b_half * alt2_half + b_normal * alt2_normal + b_wo_label * alt2_wo_label + b_fairtrade * alt2_fairtrade + b_organic * alt2_organic + b_reward * reward + b_cluster * cluster + b_age * age + b_gender * gender + b_BMI * BMI + b_urbanization * urbanization + b_education * education + b_income * income + b_frequency * frequency
V[["choice3"]] = asc_3 + b_hedonic * alt3_value + b_robusta * alt3_robusta + b_arabica * alt3_arabica + b_wo_sugar * alt3_wo_sugar + b_half * alt3_half + b_normal * alt3_normal + b_wo_label * alt3_wo_label + b_fairtrade * alt3_fairtrade + b_organic * alt3_organic + b_reward * reward + b_cluster * cluster + b_age * age + b_gender * gender + b_BMI * BMI + b_urbanization * urbanization + b_education * education + b_income * income + b_frequency * frequency
### Compute probabilities for the RP part of the data using MNL model
mnl_settings_SP = list(
alternatives = c(choice1=1, choice2=2, choice3=3),
avail = list(choice1=1, choice2=1, choice3=1), # change RP into 1 here
choiceVar = choice,
utilities = list(choice1 = mu_SP*V[["choice1"]],
choice2 = mu_SP*V[["choice2"]],
choice3 = mu_SP*V[["choice3"]]),
rows = (SP == 1)
)
P[["SP"]] = apollo_mnl(mnl_settings_SP, functionality)
### Compute probabilities for the SP part of the data using MNL model
mnl_settings_RP = list(
alternatives = c(choice1=1, choice2=2, choice3=3),
avail = list(choice1=1, choice2=1, choice3=1),
choiceVar = choice,
utilities = list(choice1 = mu_RP*V[["choice1"]],
choice2 = mu_RP*V[["choice2"]],
choice3 = mu_RP*V[["choice3"]]),
rows = (RP == 1)
)
P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
### Combined model
P = apollo_combineModels(P, apollo_inputs, 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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
Results
Code: Select all
> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Testing likelihood function...
Apollo found a model component of type MNL without a componentName. The name was set to "SP"
by default.
Overview of choices for MNL model component SP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 429.00 168.00 191.00
Percentage chosen overall 54.44 21.32 24.24
Percentage chosen when available 54.44 21.32 24.24
Apollo found a model component of type MNL without a componentName. The name was set to "RP"
by default.
Overview of choices for MNL model component RP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 399.00 167.00 222.00
Percentage chosen overall 50.63 21.19 28.17
Percentage chosen when available 50.63 21.19 28.17
Pre-processing likelihood function...
Testing influence of parameters
Starting main estimation
BGW using analytic model derivatives supplied by caller...
Iterates will be written to:
output/MNL_RP_SP_iterations.csv
it nf F RELDF PRELDF RELDX MODEL stppar
0 1 1.731412967e+03
1 4 1.199075843e+03 3.075e-01 2.709e-01 1.00e+00 G 1.12e-03
2 5 6.880394007e+02 4.262e-01 4.813e-01 6.78e-01 G 1.12e-03
3 6 5.359061542e+02 2.211e-01 3.860e-01 3.98e-01 G 7.81e-02
4 7 5.176915061e+02 3.399e-02 2.865e-02 6.78e-02 G -6.81e-05
5 8 5.167308379e+02 1.856e-03 1.560e-03 1.43e-02 G -6.81e-08
6 11 5.166600715e+02 1.370e-04 2.645e-04 8.64e-03 S 5.50e-01
7 12 5.166392782e+02 4.025e-05 3.565e-05 3.12e-03 G -5.19e-04
8 13 5.166382971e+02 1.899e-06 1.790e-06 8.75e-03 S 3.17e-13
9 14 5.166382715e+02 4.944e-08 4.589e-08 8.94e-03 S 5.11e-13
10 15 5.166382708e+02 1.308e-09 1.442e-09 8.98e-03 S 6.03e-13
11 16 5.166382708e+02 3.147e-11 3.297e-11 6.04e-03 S 2.01e-13
***** Singular convergence *****
Estimated parameters:
Estimate
asc_1 0.00000
asc_2 -0.28649
asc_3 -0.33379
b_hedonic 2.67917
b_robusta 0.06385
b_arabica -0.06244
b_wo_sugar -0.22845
b_half 0.32183
b_normal -0.30687
b_wo_label -0.90304
b_fairtrade 0.29097
b_organic 0.32886
b_reward 0.13024
b_cluster 0.09047
b_age 1.76265
b_gender 0.05528
b_BMI 0.14068
b_urbanization 0.09226
b_education 0.27037
b_income 0.11091
b_frequency 0.15578
mu_SP 1.00000
mu_RP 0.58764
Final LL: -516.6383
WARNING: Estimation failed. No covariance matrix to compute.
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
Your model was estimated using the BGW algorithm. Please acknowledge this by citing Bunch et
al. (1993) - DOI 10.1145/151271.151279Code: Select all
Model name : MNL_RP_SP
Model description : RP-SP model on mode choice data
Model run at : 2025-03-17 13:58:46.442725
Estimation method : bgw
Model diagnosis : Singular convergence
Number of individuals : 197
Number of rows in database : 1576
Number of modelled outcomes : 1576
SP : 788
RP : 788
Number of cores used : 1
Model without mixing
LL(start) : -1731.41
LL (whole model) at equal shares, LL(0) : -1731.41
LL (whole model) at observed shares, LL(C) : -1603.06
LL(final, whole model) : -516.64
Rho-squared vs equal shares : 0.7016
Adj.Rho-squared vs equal shares : 0.6895
Rho-squared vs observed shares : 0.6777
Adj.Rho-squared vs observed shares : 0.6671
AIC : 1075.28
BIC : 1173.34
LL(0,SP) : -865.71
LL(final,SP) : -194.33
LL(0,RP) : -865.71
LL(final,RP) : -322.31
Estimated parameters : 21
Time taken (hh:mm:ss) : 00:00:3.03
pre-estimation : 00:00:2.64
estimation : 00:00:0.36
post-estimation : 00:00:0.02
Iterations : 11 (Singular convergence)
Unconstrained optimisation.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_1 0.00000 NA NA NA NA
asc_2 -0.28649 NA NA NA NA
asc_3 -0.33379 NA NA NA NA
b_hedonic 2.67917 NA NA NA NA
b_robusta 0.06385 NA NA NA NA
b_arabica -0.06244 NA NA NA NA
b_wo_sugar -0.22845 NA NA NA NA
b_half 0.32183 NA NA NA NA
b_normal -0.30687 NA NA NA NA
b_wo_label -0.90304 NA NA NA NA
b_fairtrade 0.29097 NA NA NA NA
b_organic 0.32886 NA NA NA NA
b_reward 0.13024 NA NA NA NA
b_cluster 0.09047 NA NA NA NA
b_age 1.76265 NA NA NA NA
b_gender 0.05528 NA NA NA NA
b_BMI 0.14068 NA NA NA NA
b_urbanization 0.09226 NA NA NA NA
b_education 0.27037 NA NA NA NA
b_income 0.11091 NA NA NA NA
b_frequency 0.15578 NA NA NA NA
mu_SP 1.00000 NA NA NA NA
mu_RP 0.58764 NA NA NA NA
Simpler code
Code: Select all
database = read.csv("input_MNL.csv", header = TRUE)
### for data dictionary, use ?apollo_modeChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation (I deleted b_tasting and b_weekly_coffee_spending, because I do not know how to incorporate that)
apollo_beta=c(asc_1 = 0,
asc_2 = 0,
asc_3 = 0,
b_hedonic = 0,
b_robusta = 0,
b_arabica = 0,
b_wo_sugar = 0,
b_half = 0,
b_normal = 0,
b_wo_label = 0,
b_fairtrade = 0,
b_organic = 0,
mu_SP = 1,
mu_RP = 1)
### 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("asc_1", "mu_SP")
# ################################################################# #
#### 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 (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
# PS: I have not included b_tasting? not sure how to put it also remove the asc_1
V = list()
V[["choice1"]] = asc_1 + b_hedonic * alt1_value + b_robusta * alt1_robusta + b_arabica * alt1_arabica + b_wo_sugar * alt1_wo_sugar + b_half * alt1_half + b_normal * alt1_normal + b_wo_label * alt1_wo_label + b_fairtrade * alt1_fairtrade + b_organic * alt1_organic
V[["choice2"]] = asc_2 + b_hedonic * alt2_value + b_robusta * alt2_robusta + b_arabica * alt2_arabica + b_wo_sugar * alt2_wo_sugar + b_half * alt2_half + b_normal * alt2_normal + b_wo_label * alt2_wo_label + b_fairtrade * alt2_fairtrade + b_organic * alt2_organic
V[["choice3"]] = asc_3 + b_hedonic * alt3_value + b_robusta * alt3_robusta + b_arabica * alt3_arabica + b_wo_sugar * alt3_wo_sugar + b_half * alt3_half + b_normal * alt3_normal + b_wo_label * alt3_wo_label + b_fairtrade * alt3_fairtrade + b_organic * alt3_organic
### Compute probabilities for the SP part of the data using MNL model
mnl_settings_SP = list(
alternatives = c(choice1=1, choice2=2, choice3=3),
avail = list(choice1=1, choice2=1, choice3=1), # change SP into 1 here
choiceVar = choice,
utilities = list(choice1 = mu_SP*V[["choice1"]],
choice2 = mu_SP*V[["choice2"]],
choice3 = mu_SP*V[["choice3"]]),
rows = (SP == 1)
)
P[["SP"]] = apollo_mnl(mnl_settings_SP, functionality)
### Compute probabilities for the RP part of the data using MNL model
mnl_settings_RP = list(
alternatives = c(choice1=1, choice2=2, choice3=3),
avail = list(choice1=1, choice2=1, choice3=1),
choiceVar = choice,
utilities = list(choice1 = mu_RP*V[["choice1"]],
choice2 = mu_RP*V[["choice2"]],
choice3 = mu_RP*V[["choice3"]]),
rows = (RP == 1)
)
P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
### Combined model
P = apollo_combineModels(P, apollo_inputs, 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)Code: Select all
Testing likelihood function...
Apollo found a model component of type MNL without a componentName. The name was set to "SP"
by default.
Overview of choices for MNL model component SP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 429.00 168.00 191.00
Percentage chosen overall 54.44 21.32 24.24
Percentage chosen when available 54.44 21.32 24.24
Apollo found a model component of type MNL without a componentName. The name was set to "RP"
by default.
Overview of choices for MNL model component RP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 399.00 167.00 222.00
Percentage chosen overall 50.63 21.19 28.17
Percentage chosen when available 50.63 21.19 28.17
Pre-processing likelihood function...
Testing influence of parameters
Starting main estimation
BGW using analytic model derivatives supplied by caller...
Iterates will be written to:
output/MNL_RP_SP_iterations.csv
it nf F RELDF PRELDF RELDX MODEL stppar
0 1 1.731412967e+03
1 4 1.199075843e+03 3.075e-01 2.709e-01 1.00e+00 G 1.12e-03
2 5 6.880394007e+02 4.262e-01 4.813e-01 6.78e-01 G 1.12e-03
3 6 5.359061542e+02 2.211e-01 3.860e-01 3.98e-01 G 7.81e-02
4 7 5.176915061e+02 3.399e-02 2.865e-02 6.78e-02 G -6.81e-05
5 8 5.167308379e+02 1.856e-03 1.560e-03 1.43e-02 G -6.81e-08
6 11 5.166600715e+02 1.370e-04 2.645e-04 8.64e-03 S 5.50e-01
7 12 5.166392782e+02 4.025e-05 3.565e-05 3.12e-03 G -5.19e-04
8 13 5.166382971e+02 1.899e-06 1.790e-06 1.17e-03 S -5.19e-07
9 14 5.166382715e+02 4.944e-08 4.589e-08 1.48e-04 S -5.19e-07
10 15 5.166382708e+02 1.308e-09 1.442e-09 1.21e-05 S -5.19e-10
11 16 5.166382708e+02 3.147e-11 3.297e-11 2.82e-06 S -5.19e-13
***** Singular convergence *****
Estimated parameters:
Estimate
asc_1 0.00000
asc_2 -0.28649
asc_3 -0.33379
b_hedonic 2.67917
b_robusta 0.06315
b_arabica -0.06315
b_wo_sugar -0.23030
b_half 0.31997
b_normal -0.30872
b_wo_label -0.90232
b_fairtrade 0.29169
b_organic 0.32958
mu_SP 1.00000
mu_RP 0.58764
Final LL: -516.6383
WARNING: Estimation failed. No covariance matrix to compute.
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 measuresMuch simpler MNL model
Code: Select all
> apollo_modelOutput(model)
Model run by wzj545 using Apollo 0.3.4 on R 4.3.2 for Windows.
Please acknowledge the use of Apollo by citing Hess & Palma (2019)
DOI 10.1016/j.jocm.2019.100170
www.ApolloChoiceModelling.com
Model name : MNL_RP_SP
Model description : RP-SP model on mode choice data
Model run at : 2025-03-17 14:23:01.783546
Estimation method : bgw
Model diagnosis : Relative function convergence
Optimisation diagnosis : Maximum found
hessian properties : Negative definite
maximum eigenvalue : 0
reciprocal of condition number : 4.1813e-14
Number of individuals : 197
Number of rows in database : 1576
Number of modelled outcomes : 1576
SP : 788
RP : 788
Number of cores used : 1
Model without mixing
LL(start) : -1731.41
LL (whole model) at equal shares, LL(0) : -1731.41
LL (whole model) at observed shares, LL(C) : -1603.06
LL(final, whole model) : -551.17
Rho-squared vs equal shares : 0.6817
Adj.Rho-squared vs equal shares : 0.6782
Rho-squared vs observed shares : 0.6562
Adj.Rho-squared vs observed shares : 0.6549
AIC : 1114.34
BIC : 1142.36
LL(0,SP) : -865.71
LL(final,SP) : -213.43
LL(0,RP) : -865.71
LL(final,RP) : -337.74
Estimated parameters : 6
Time taken (hh:mm:ss) : 00:00:3.02
pre-estimation : 00:00:1.54
estimation : 00:00:0.49
post-estimation : 00:00:0.99
Iterations : 11
Unconstrained optimisation.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_1 0.0000 NA NA NA NA
asc_2 -0.8861 0.15832 -5.597 0.15655 -5.660356
asc_3 -0.7808 0.14624 -5.339 0.16304 -4.788759
b_hedonic 2.7972 0.18319 15.270 0.22262 12.564959
b_robusta 2.0623 1.155e+05 1.786e-05 1275.76885 0.001616
b_arabica 2.1466 1.155e+05 1.859e-05 1275.76885 0.001683
mu_SP 1.0000 NA NA NA NA
mu_RP 0.5748 0.05068 11.343 0.05987 9.601318When I added a b_wo_sugar:
Code: Select all
Preparing user-defined functions.
Testing likelihood function...
Apollo found a model component of type MNL without a componentName. The name was set to "SP"
by default.
Overview of choices for MNL model component SP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 429.00 168.00 191.00
Percentage chosen overall 54.44 21.32 24.24
Percentage chosen when available 54.44 21.32 24.24
Apollo found a model component of type MNL without a componentName. The name was set to "RP"
by default.
Overview of choices for MNL model component RP:
choice1 choice2 choice3
Times available 788.00 788.00 788.00
Times chosen 399.00 167.00 222.00
Percentage chosen overall 50.63 21.19 28.17
Percentage chosen when available 50.63 21.19 28.17
Pre-processing likelihood function...
Testing influence of parameters
Starting main estimation
BGW using analytic model derivatives supplied by caller...
Iterates will be written to:
output/MNL_RP_SP_iterations.csv
it nf F RELDF PRELDF RELDX MODEL stppar
0 1 1.731412967e+03
1 4 1.262663611e+03 2.707e-01 2.453e-01 1.00e+00 G -1.05e-03
2 5 7.370061157e+02 4.163e-01 4.553e-01 6.58e-01 G 1.05e-03
3 6 5.571602593e+02 2.440e-01 4.250e-01 4.31e-01 G 4.72e-02
4 7 5.465721347e+02 1.900e-02 1.589e-02 3.19e-02 G -4.40e-05
5 8 5.459901487e+02 1.065e-03 8.861e-04 1.97e-01 G 0.00e+00
6 9 5.459574443e+02 5.990e-05 4.654e-05 5.26e-03 G -7.48e-10
7 10 5.459544235e+02 5.533e-06 4.155e-06 1.54e-03 G -7.48e-10
8 11 5.459540249e+02 7.299e-07 7.577e-07 8.72e-04 S -7.48e-10
9 12 5.459540183e+02 1.220e-08 2.529e-08 2.46e-01 S -6.23e-17
10 13 5.459540151e+02 5.787e-09 5.605e-09 6.18e-02 G 0.00e+00
11 14 5.459540150e+02 2.373e-10 2.096e-10 9.10e-06 G -7.48e-13
12 15 5.459540150e+02 1.606e-11 1.335e-11 2.53e-02 G -7.48e-16
***** Singular convergence *****
Estimated parameters:
Estimate
asc_1 0.0000
asc_2 -0.7424
asc_3 -0.7711
b_hedonic 2.6893
b_robusta 6.3572
b_arabica 6.3305
b_wo_sugar -0.5776
mu_SP 1.0000
mu_RP 0.5905
Final LL: -545.954
WARNING: Estimation failed. No covariance matrix to compute.
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 measuresBest,
Andika