Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- 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.
errors in Latent class model
-
- Site Admin
- Posts: 1042
- Joined: 24 Apr 2020, 16:29
Re: erros in Latent class model
Georgios
you need many more draws than 20 to achieve reliable results. The fact that the model 'works' with a low number of draws doesn't mean anything - see
http://apollochoicemodelling.com/faq.html
Did you look at the fit for the latent class model without continuous heterogeneity and compare that to your new one? And did you use those earlier results as starting values
It's clear that some of your parameters go to +-inf, leading to the identification issue
mu_Assistance_emergency_call_c2 -149.994543 NA NA NA NA
mu_Assistance_smart_routes_c2 133.295872 NA NA NA NA
mu_Automatic_speed_safe_distance_c2 0.000000 NA NA NA NA
mu_Automatic_speed_speed_limit_c2 -676.074779 NA NA NA NA
you need many more draws than 20 to achieve reliable results. The fact that the model 'works' with a low number of draws doesn't mean anything - see
http://apollochoicemodelling.com/faq.html
Did you look at the fit for the latent class model without continuous heterogeneity and compare that to your new one? And did you use those earlier results as starting values
It's clear that some of your parameters go to +-inf, leading to the identification issue
mu_Assistance_emergency_call_c2 -149.994543 NA NA NA NA
mu_Assistance_smart_routes_c2 133.295872 NA NA NA NA
mu_Automatic_speed_safe_distance_c2 0.000000 NA NA NA NA
mu_Automatic_speed_speed_limit_c2 -676.074779 NA NA NA NA
Re: erros in Latent class model
Dear Stephane,
Thank you for your help.
Just tried what you suggested:
1) I compared the two models and here are the results:
2) I have also now included the earlier results from the latent class model to the mixed latent class. The model ran, however, the weights of the two classes changed extremely, is this correct?
I am sharing the code and the results here:
I put the results here:
Any help on this would be really useful. Thank you.
Best regards,
Georgios
Thank you for your help.
Just tried what you suggested:
1) I compared the two models and here are the results:
Code: Select all
> apollo_lrTest("model_2",model)
LL par
Model_2 -6591.67 29
Model_3 -6163.87 51
Difference 427.80 22
Likelihood ratio test-value: 855.6
Degrees of freedom: 22
Likelihood ratio test p-value: 9.369e-167
I am sharing the code and the results here:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################# ################################ #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Model_3-1",
modelDescr = "MLC model with values",
indivID = "ID",
mixing = TRUE
# nCores = 1
# outputDirectory = "test1"
)
# ################################################################# #
#### 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 = apollo_modeChoiceData
database = read.csv("....",header=TRUE)
### for data dictionary, use ?apollo_modeChoiceData
### Use only RP data
#database = subset(database,database$country_code=='NL')
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_A = 0,
asc_B = 0,
asc_C = 0,
mu_Assistance_emergency_unit_c1 = 0,
mu_Assistance_emergency_call_c1 = -0.0160802706434751,
mu_Assistance_smart_routes_c1 = -0.0935233800968684,
mu_Automatic_speed_safe_distance_c1 = 0,
mu_Automatic_speed_speed_limit_c1 = 0.23300473566489,
mu_Automatic_speed_risky_areas_c1 = 0.228321526737585,
mu_Collision_avoidance_front_side_c1 = 0,
mu_Collision_avoidance_rear_side_c1 = 0.369210190826483,
mu_Collision_avoidance_blind_spot_c1 = 0.294871148805062,
mu_cost_c1 = -0.00129785049220034,
sigma_Assistance_emergency_unit_c1 = 0,
sigma_Assistance_emergency_call_c1 = 0,
sigma_Assistance_smart_routes_c1 = 0,
sigma_Automatic_speed_safe_distance_c1 = 0,
sigma_Automatic_speed_speed_limit_c1 = 0,
sigma_Automatic_speed_risky_areas_c1 = 0,
sigma_Collision_avoidance_front_side_c1 = 0,
sigma_Collision_avoidance_rear_side_c1 = 0,
sigma_Collision_avoidance_blind_spot_c1 = 0,
sigma_cost_c1 = 0,
mu_Assistance_emergency_unit_c2 = 0,
mu_Assistance_emergency_call_c2 = -0.236642222853527,
mu_Assistance_smart_routes_c2 = 0.355593470878659,
mu_Automatic_speed_safe_distance_c2 = 0,
mu_Automatic_speed_speed_limit_c2 = -0.302341088633653,
mu_Automatic_speed_risky_areas_c2 = -0.245113328505669,
mu_Collision_avoidance_front_side_c2 = 0,
mu_Collision_avoidance_rear_side_c2 = 0.0297293749961098,
mu_Collision_avoidance_blind_spot_c2 = -0.0336956313177463,
mu_cost_c2 = -0.00675838806708174,
sigma_Assistance_emergency_unit_c2 = 0,
sigma_Assistance_emergency_call_c2 = 0,
sigma_Assistance_smart_routes_c2 = 0,
sigma_Automatic_speed_safe_distance_c2 = 0,
sigma_Automatic_speed_speed_limit_c2 = 0,
sigma_Automatic_speed_risky_areas_c2 = 0,
sigma_Collision_avoidance_front_side_c2 = 0,
sigma_Collision_avoidance_rear_side_c2 = 0,
sigma_Collision_avoidance_blind_spot_c2 = 0,
sigma_cost_c2 = 0,
elderly_Assistance_emergency_call = 0,
elderly_Assistance_smart_routes = 0,
male_Assistance_smart_routes = 0,
male_Automatic_speed_speed_limit = 0,
elderly_Automatic_speed_risky_areas = 0,
male_Automatic_speed_safe_distance = 0,
elderly_Automatic_speed_safe_distance = 0,
elderly_Collision_avoidance_rear_side = 0,
elderly_Collision_avoidance_blind_spot = 0,
age3049_Assistance_emergency_call = 0,
age3049_Assistance_smart_routes = 0,
elderly_Assistance_emergency_unit = 0,
ownebike_emergency_call = 0,
delta_class_1 = 0,
delta_class_2 = 0,
gamma_GR_1 = 0,
gamma_GR_2 = 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("asc_A",
"mu_Assistance_emergency_unit_c1",
"mu_Automatic_speed_safe_distance_c1",
"mu_Collision_avoidance_front_side_c1",
"mu_Assistance_emergency_unit_c2",
"mu_Automatic_speed_safe_distance_c2",
"mu_Collision_avoidance_front_side_c2",
"delta_class_2",
"gamma_GR_2")
# ################################################################# #
#### MODEL PARAMETERS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "mlhs", #"halton"
interNDraws = 100,
interUnifDraws = c(),
interNormDraws = c("draws_Assistance_emergency_unit_c1", "draws_Assistance_emergency_call_c1", "draws_Assistance_smart_routes_c1",
"draws_Automatic_speed_safe_distance_c1", "draws_Automatic_speed_speed_limit_c1", "draws_Automatic_speed_risky_areas_c1",
"draws_Collision_avoidance_front_side_c1", "draws_Collision_avoidance_rear_side_c1", "draws_Collision_avoidance_blind_spot_c1", "draws_cost_c1",
"draws_Assistance_emergency_unit_c2", "draws_Assistance_emergency_call_c2", "draws_Assistance_smart_routes_c2",
"draws_Automatic_speed_safe_distance_c2", "draws_Automatic_speed_speed_limit_c2", "draws_Automatic_speed_risky_areas_c2",
"draws_Collision_avoidance_front_side_c2", "draws_Collision_avoidance_rear_side_c2", "draws_Collision_avoidance_blind_spot_c2", "draws_cost_c2"),
# "draws_pi_c1", "draws_pi_c2")
intraDrawsType = "pmc",
# )#"mlhs", #"halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_Assistance_emergency_unit_c1"]] = mu_Assistance_emergency_unit_c1 + sigma_Assistance_emergency_unit_c1 * draws_Assistance_emergency_unit_c1
randcoeff[["b_Assistance_emergency_call_c1"]] = mu_Assistance_emergency_call_c1 + sigma_Assistance_emergency_call_c1 * draws_Assistance_emergency_call_c1
randcoeff[["b_Assistance_smart_routes_c1"]] = mu_Assistance_smart_routes_c1 + sigma_Assistance_smart_routes_c1 * draws_Assistance_smart_routes_c1
randcoeff[["b_Automatic_speed_safe_distance_c1"]] = mu_Automatic_speed_safe_distance_c1 + sigma_Automatic_speed_safe_distance_c1 * draws_Automatic_speed_safe_distance_c1
randcoeff[["b_Automatic_speed_speed_limit_c1"]] = mu_Automatic_speed_speed_limit_c1 + sigma_Automatic_speed_speed_limit_c1 * draws_Automatic_speed_speed_limit_c1
randcoeff[["b_Automatic_speed_risky_areas_c1"]] = mu_Automatic_speed_risky_areas_c1 + sigma_Automatic_speed_risky_areas_c1 * draws_Automatic_speed_risky_areas_c1
randcoeff[["b_Collision_avoidance_front_side_c1"]] = mu_Collision_avoidance_front_side_c1 + sigma_Collision_avoidance_front_side_c1 * draws_Collision_avoidance_front_side_c1
randcoeff[["b_Collision_avoidance_rear_side_c1"]] = mu_Collision_avoidance_rear_side_c1 + sigma_Collision_avoidance_rear_side_c1 * draws_Collision_avoidance_rear_side_c1
randcoeff[["b_Collision_avoidance_blind_spot_c1"]] = mu_Collision_avoidance_blind_spot_c1 + sigma_Collision_avoidance_blind_spot_c1 * draws_Collision_avoidance_blind_spot_c1
randcoeff[["b_cost_c1"]] = mu_cost_c1 + sigma_cost_c1 * draws_cost_c1
# randcoeff[["b_delta_c1"]] = delta_class_1 + sigma_delta_class_1 * draws_pi_c1
randcoeff[["b_Assistance_emergency_unit_c2"]] = mu_Assistance_emergency_unit_c2 + sigma_Assistance_emergency_unit_c2 * draws_Assistance_emergency_unit_c2
randcoeff[["b_Assistance_emergency_call_c2"]] = mu_Assistance_emergency_call_c2 + sigma_Assistance_emergency_call_c2 * draws_Assistance_emergency_call_c2
randcoeff[["b_Assistance_smart_routes_c2"]] = mu_Assistance_smart_routes_c2 + sigma_Assistance_smart_routes_c2 * draws_Assistance_smart_routes_c2
randcoeff[["b_Automatic_speed_safe_distance_c2"]] = mu_Automatic_speed_safe_distance_c2 + sigma_Automatic_speed_safe_distance_c2 * draws_Automatic_speed_safe_distance_c2
randcoeff[["b_Automatic_speed_speed_limit_c2"]] = mu_Automatic_speed_speed_limit_c2 + sigma_Automatic_speed_speed_limit_c2 * draws_Automatic_speed_speed_limit_c2
randcoeff[["b_Automatic_speed_risky_areas_c2"]] = mu_Automatic_speed_risky_areas_c2 + sigma_Automatic_speed_risky_areas_c2 * draws_Automatic_speed_risky_areas_c2
randcoeff[["b_Collision_avoidance_front_side_c2"]] = mu_Collision_avoidance_front_side_c2 + sigma_Collision_avoidance_front_side_c2 * draws_Collision_avoidance_front_side_c2
randcoeff[["b_Collision_avoidance_rear_side_c2"]] = mu_Collision_avoidance_rear_side_c2 + sigma_Collision_avoidance_rear_side_c2 * draws_Collision_avoidance_rear_side_c2
randcoeff[["b_Collision_avoidance_blind_spot_c2"]] = mu_Collision_avoidance_blind_spot_c2 + sigma_Collision_avoidance_blind_spot_c2 * draws_Collision_avoidance_blind_spot_c2
randcoeff[["b_cost_c2"]] = mu_cost_c2 + sigma_cost_c2 * draws_cost_c2
# randcoeff[["b_delta_c2"]] = delta_class_2 + sigma_delta_class_2 * draws_pi_c2
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_Assistance_emergency_unit"]] = list(b_Assistance_emergency_unit_c1,b_Assistance_emergency_unit_c2)
lcpars[["b_Assistance_emergency_call"]] = list(b_Assistance_emergency_call_c1,b_Assistance_emergency_call_c2)
lcpars[["b_Assistance_smart_routes"]] = list(b_Assistance_smart_routes_c1,b_Assistance_smart_routes_c2)
lcpars[["b_Automatic_speed_speed_limit"]] = list(b_Automatic_speed_speed_limit_c1,b_Automatic_speed_speed_limit_c2)
lcpars[["b_Automatic_speed_risky_areas"]] = list(b_Automatic_speed_risky_areas_c1,b_Automatic_speed_risky_areas_c2)
lcpars[["b_Automatic_speed_safe_distance"]] = list(b_Automatic_speed_safe_distance_c1,b_Automatic_speed_safe_distance_c2)
lcpars[["b_Collision_avoidance_front_side"]] = list(b_Collision_avoidance_front_side_c1,b_Collision_avoidance_front_side_c2)
lcpars[["b_Collision_avoidance_rear_side"]] = list(b_Collision_avoidance_rear_side_c1,b_Collision_avoidance_rear_side_c2)
lcpars[["b_Collision_avoidance_blind_spot"]] = list(b_Collision_avoidance_blind_spot_c1,b_Collision_avoidance_blind_spot_c2)
lcpars[["b_cost"]] = list(b_cost_c1,b_cost_c2)
V=list()
V[["class_1"]] = delta_class_1 + gamma_GR_1 * (database$GR_dummy == 1)
V[["class_2"]] = delta_class_2 + gamma_GR_2 * (database$GR_dummy == 1)
classAlloc_settings = list(
#mnl_settings = list(
alternatives = c(class_1=1, class_2=2),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
# lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")
# lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
return(lcpars)
}
# ################################################################# #
#### 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()
### Loop over classes
for(s in 1:2){
### Create coefficients using interaction effects
#female_value = Assistance_smart_routes + b_Age * female_dummy
#b_Assistance_older_60_value = Assistance_emergency_unit + b_older_60 * older_60
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["A"]] = (asc_A
+ (b_Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1== 1)
+ (b_Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1)
+ ownebike_emergency_call * (BuyEbike==1)) * (Att1== 2)
+ (b_Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1)) * (Att1== 3)
+ (b_Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2== 1)
+ (b_Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2== 2)
+ (b_Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2== 3)
+ b_Collision_avoidance_front_side[[s]] * (Att3== 1)
+ (b_Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3== 2)
+ b_Collision_avoidance_blind_spot[[s]] * (Att3== 3)
+ b_cost[[s]] * ((Att4== 1) * 400
+(Att4== 2) * 600
+(Att4== 3) * 800
+(Att4== 4) * 1000))
V[["B"]] = (asc_B
+ (b_Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1.1== 1)
+ (b_Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1)
+ ownebike_emergency_call * (BuyEbike==1)) * (Att1.1== 2)
+ (b_Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1) ) * (Att1.1== 3)
+ (b_Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2.1== 1)
+ (b_Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2.1== 2)
+ (b_Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2.1== 3)
+ b_Collision_avoidance_front_side[[s]] * (Att3.1== 1)
+ (b_Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3.1== 2)
+ (b_Collision_avoidance_blind_spot[[s]]
+ elderly_Collision_avoidance_blind_spot * (age_5099 == 1)) * (Att3.1== 3)
+ b_cost[[s]] * ((Att4.1== 1) * 400
+(Att4.1== 2) * 600
+(Att4.1== 3) * 800
+(Att4.1== 4) * 1000))
V[["C"]] = (asc_C)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(A=1, B=2, C=3),
##avail = list(alt1=Concept, alt2=Concept.1),
choiceVar = choice,
utilities = V
)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs, functionality)
### Average across inter-individual draws within classes
P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
}
### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
### Average across inter-individual draws in class allocation probabilities
P[["model"]] = apollo_avgInterDraws(P[["model"]], 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)
I put the results here:
Code: Select all
Model run by Georgios using Apollo 0.2.8 on R 4.3.0 for Windows.
www.ApolloChoiceModelling.com
Model name : Model_3-1
Model description : MLC model with values
Model run at : 2023-07-17 23:08:22.343639
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 1398
Number of rows in database : 8388
Number of modelled outcomes : 8388
Number of cores used : 1
Number of inter-individual draws : 100 (mlhs)
LL(start) : -7392
LL (whole model) at equal shares, LL(0) : -9215.16
LL (whole model) at observed shares, LL(C) : -8658.59
LL(final, whole model) : -6128.43
Rho-squared vs equal shares : 0.335
Adj.Rho-squared vs equal shares : 0.3294
Rho-squared vs observed shares : 0.2922
Adj.Rho-squared vs observed shares : 0.2863
AIC : 12358.85
BIC : 12717.62
LL(0,Class_1) : -9215.16
LL(final,Class_1) : -6783.55
LL(0,Class_2) : -9215.16
LL(final,Class_2) : -9672.81
Estimated parameters : 51
Time taken (hh:mm:ss) : 00:49:34.71
pre-estimation : 00:07:30.18
estimation : 00:13:38.84
post-estimation : 00:28:25.69
Iterations : 107
Min abs eigenvalue of Hessian : 0.206297
Unconstrained optimisation.
These outputs have had the scaling used in estimation applied to them.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_A 0.000000 NA NA NA NA
asc_B 0.068184 0.055523 1.228031 0.056549 1.205748
asc_C -3.260916 0.227206 -14.352264 0.282741 -11.533245
mu_Assistance_emergency_unit_c1 0.000000 NA NA NA NA
mu_Assistance_emergency_call_c1 -0.021802 0.220501 -0.098877 0.242524 -0.089898
mu_Assistance_smart_routes_c1 -0.012349 0.272081 -0.045387 0.267448 -0.046173
mu_Automatic_speed_safe_distance_c1 0.000000 NA NA NA NA
mu_Automatic_speed_speed_limit_c1 0.501592 0.143629 3.492268 0.148755 3.371936
mu_Automatic_speed_risky_areas_c1 0.455136 0.141385 3.219121 0.152812 2.978400
mu_Collision_avoidance_front_side_c1 0.000000 NA NA NA NA
mu_Collision_avoidance_rear_side_c1 0.528960 0.102433 5.163953 0.103405 5.115416
mu_Collision_avoidance_blind_spot_c1 0.321619 0.085397 3.766163 0.086793 3.705576
mu_cost_c1 -0.004428 2.5276e-04 -17.518653 2.8582e-04 -15.492487
sigma_Assistance_emergency_unit_c1 -0.264765 0.151697 -1.745357 0.132556 -1.997377
sigma_Assistance_emergency_call_c1 -0.256201 0.151806 -1.687685 0.153805 -1.665754
sigma_Assistance_smart_routes_c1 -0.958721 0.126626 -7.571276 0.143008 -6.703954
sigma_Automatic_speed_safe_distance_c1 0.766124 0.132048 5.801845 0.142692 5.369075
sigma_Automatic_speed_speed_limit_c1 -0.354600 0.146418 -2.421836 0.148834 -2.382517
sigma_Automatic_speed_risky_areas_c1 0.134324 0.446621 0.300757 0.925275 0.145172
sigma_Collision_avoidance_front_side_c1 -0.719742 0.113781 -6.325703 0.124803 -5.767048
sigma_Collision_avoidance_rear_side_c1 -0.245900 0.160737 -1.529829 0.164823 -1.491900
sigma_Collision_avoidance_blind_spot_c1 -0.607919 0.135160 -4.497764 0.146815 -4.140706
sigma_cost_c1 -0.003797 2.3622e-04 -16.073879 2.8278e-04 -13.427139
mu_Assistance_emergency_unit_c2 0.000000 NA NA NA NA
mu_Assistance_emergency_call_c2 -0.111852 0.481632 -0.232236 0.618904 -0.180726
mu_Assistance_smart_routes_c2 3.248035 0.749571 4.333194 1.142110 2.843890
mu_Automatic_speed_safe_distance_c2 0.000000 NA NA NA NA
mu_Automatic_speed_speed_limit_c2 -3.445219 0.704670 -4.889126 1.214211 -2.837414
mu_Automatic_speed_risky_areas_c2 -2.252976 0.474435 -4.748756 0.555419 -4.056356
mu_Collision_avoidance_front_side_c2 0.000000 NA NA NA NA
mu_Collision_avoidance_rear_side_c2 -0.748747 0.501057 -1.494335 0.755032 -0.991676
mu_Collision_avoidance_blind_spot_c2 0.598913 0.398281 1.503746 0.488573 1.225841
mu_cost_c2 -0.020864 0.003284 -6.352931 0.006818 -3.060237
sigma_Assistance_emergency_unit_c2 -0.857641 0.479739 -1.787725 0.814517 -1.052945
sigma_Assistance_emergency_call_c2 1.343277 0.796152 1.687212 1.958300 0.685940
sigma_Assistance_smart_routes_c2 -6.677812 1.321139 -5.054586 2.709657 -2.464449
sigma_Automatic_speed_safe_distance_c2 -4.921870 1.047876 -4.696998 2.244725 -2.192639
sigma_Automatic_speed_speed_limit_c2 0.399994 0.374721 1.067443 0.456894 0.875463
sigma_Automatic_speed_risky_areas_c2 -3.045113 0.704485 -4.322470 1.475954 -2.063149
sigma_Collision_avoidance_front_side_c2 0.518611 0.346895 1.495005 0.463785 1.118213
sigma_Collision_avoidance_rear_side_c2 -3.578468 0.776385 -4.609141 1.516594 -2.359542
sigma_Collision_avoidance_blind_spot_c2 -2.572449 0.685286 -3.753834 1.358869 -1.893081
sigma_cost_c2 0.016406 0.003028 5.417617 0.006387 2.568386
elderly_Assistance_emergency_call -0.746165 0.289252 -2.579637 0.332087 -2.246899
elderly_Assistance_smart_routes -0.649014 0.336826 -1.926854 0.345642 -1.877708
male_Assistance_smart_routes 0.173787 0.141417 1.228898 0.156526 1.110271
male_Automatic_speed_speed_limit -0.241577 0.119316 -2.024676 0.123508 -1.955960
elderly_Automatic_speed_risky_areas -0.148144 0.124704 -1.187974 0.127143 -1.165180
male_Automatic_speed_safe_distance 0.417133 0.148052 2.817486 0.159783 2.610620
elderly_Automatic_speed_safe_distance -0.209253 0.149990 -1.395115 0.147791 -1.415875
elderly_Collision_avoidance_rear_side -0.143469 0.119664 -1.198936 0.118841 -1.207239
elderly_Collision_avoidance_blind_spot 0.001044 0.139486 0.007487 0.144550 0.007225
age3049_Assistance_emergency_call -0.047164 0.238662 -0.197620 0.260058 -0.181361
age3049_Assistance_smart_routes -0.015808 0.290751 -0.054371 0.290134 -0.054486
elderly_Assistance_emergency_unit -0.705022 0.213361 -3.304366 0.253515 -2.780986
ownebike_emergency_call 0.190400 0.119205 1.597252 0.114820 1.658239
delta_class_1 0.251464 0.091581 2.745812 0.120368 2.089130
delta_class_2 0.000000 NA NA NA NA
gamma_GR_1 1.282798 0.250258 5.125907 0.261537 4.904839
gamma_GR_2 0.000000 NA NA NA NA
Summary of class allocation for model component :
Mean prob.
Class_1 0.6010
Class_2 0.3990
Best regards,
Georgios
-
- Site Admin
- Posts: 1042
- Joined: 24 Apr 2020, 16:29
Re: erros in Latent class model
Hi
you seem to haev 22 random parameters. This is much more than almost any other study. I wouldn't expect this model to converge to a sensible solution unless you use many thousands of draws to cover the mult-dimensional space of integration adequately
Best wishes
Stephane
you seem to haev 22 random parameters. This is much more than almost any other study. I wouldn't expect this model to converge to a sensible solution unless you use many thousands of draws to cover the mult-dimensional space of integration adequately
Best wishes
Stephane
Re: erros in Latent class model
Dear Stephane,
Thank you for the help.
OK, this is clear. I see in the literature I can use LC model without LC mixing model. is that correct? So I can go with the LC model.
Maybe I forgot to mention, my aim is to calculate the WTP for some functionalities for bicycles out of this model. So, my SP is unlabeled with four attributes (3 with 3 levels and 1 with 4 levels). As I understand, in order to calculate the WTP, I have to use the fixed parameters rather than the random coefficients for the LC.
I tried to put the cost within the estimation (wtp space) but the results that come out of this model are some extreme values with high s.e. and estimates, here are the results
here is the code
So, as I mentioned above, I can use the LC model, first to calculate the utilities in preference space and then to get wtp (outside); the model I would like to use is the following one:
Once I get the utilities for the two classes, I am a bit lost. Should I use 1) the delta method in order to get the wtp, or 2) to get the distribution using apollo_unconditionals? and do I need to repeat this for each parameter?
for 1)
The results from this piece of code are (class 1):
Running Delta method computation for user-defined function:
Expression Value Robust s.e. Rob t-ratio (0)
WTP_Assistance_smart_routes_c1 -40.9666 38.2392 -1.07
Does this mean that people have negative wtp? -40.9666 €
For class 2
the results for class 2 are :
Running Delta method computation for user-defined function:
Expression Value Robust s.e. Rob t-ratio (0)
WTP_Assistance_smart_routes_c2 65.0924 164.5114 0.4
Does this mean that people have negative wtp? 65.0924 € ?
However, the utilities for c1_Assistance_smart_routes from class 1 are 0.305517 and c2_Assistance_smart_routes are -0.089946, so I was expecting the other way around, namely class 1 to have higher wtp for Assistance_smart_routes.
Is this the correct way to calculate the wtp?
for 2)
Thank you and kind regards,
Georgios
Thank you for the help.
OK, this is clear. I see in the literature I can use LC model without LC mixing model. is that correct? So I can go with the LC model.
Maybe I forgot to mention, my aim is to calculate the WTP for some functionalities for bicycles out of this model. So, my SP is unlabeled with four attributes (3 with 3 levels and 1 with 4 levels). As I understand, in order to calculate the WTP, I have to use the fixed parameters rather than the random coefficients for the LC.
I tried to put the cost within the estimation (wtp space) but the results that come out of this model are some extreme values with high s.e. and estimates, here are the results
Code: Select all
Model name : Model_2_3
Model description : LC model with socios_ALL
Model run at : 2023-07-26 22:04:10.643141
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 1169
Number of rows in database : 7014
Number of modelled outcomes : 7014
Number of cores used : 1
Model without mixing
LL(start) : -7705.67
LL (whole model) at equal shares, LL(0) : -7705.67
LL (whole model) at observed shares, LL(C) : -7257.65
LL(final, whole model) : -5405.27
Rho-squared vs equal shares : 0.2985
Adj.Rho-squared vs equal shares : 0.2948
Rho-squared vs observed shares : 0.2552
Adj.Rho-squared vs observed shares : 0.2512
AIC : 10868.55
BIC : 11067.36
LL(0,Class_1) : -7705.67
LL(final,Class_1) : -10308.97
LL(0,Class_2) : -7705.67
LL(final,Class_2) : -10794.44
Estimated parameters : 29
Time taken (hh:mm:ss) : 00:01:3.47
pre-estimation : 00:00:12.99
estimation : 00:00:25.41
post-estimation : 00:00:25.07
Iterations : 117
Min abs eigenvalue of Hessian : 5.4e-05
Unconstrained optimisation.
These outputs have had the scaling used in estimation applied to them.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_A 0.000000 NA NA NA NA
asc_B 12.015701 14.42879 0.83276 15.06651 0.79751
asc_C -2.230852 0.12556 -17.76734 0.14680 -15.19638
c1_Assistance_emergency_unit 0.000000 NA NA NA NA
c1_Assistance_emergency_call 5.075823 62.01968 0.08184 65.02220 0.07806
c1_Assistance_smart_routes -131.539950 75.73199 -1.73691 113.14968 -1.16253
c1_Automatic_speed_safe_distance 0.000000 NA NA NA NA
c1_Automatic_speed_speed_limit 21.088830 49.07517 0.42973 48.63832 0.43358
c1_Automatic_speed_risky_areas 2.513139 49.21539 0.05106 50.77761 0.04949
c1_Collision_avoidance_front_side 0.000000 NA NA NA NA
c1_Collision_avoidance_rear_side -150.739467 43.47320 -3.46741 44.53761 -3.38454
c1_Collision_avoidance_blind_spot -143.174406 40.13369 -3.56744 43.40051 -3.29891
c1_b_cost -0.001424 1.1700e-04 -12.16810 1.7467e-04 -8.15061
c2_Assistance_emergency_unit 0.000000 NA NA NA NA
c2_Assistance_emergency_call 56.825234 52.11520 1.09038 54.65888 1.03963
c2_Assistance_smart_routes -36.784673 69.37504 -0.53023 109.98572 -0.33445
c2_Automatic_speed_safe_distance 0.000000 NA NA NA NA
c2_Automatic_speed_speed_limit 37.723864 28.91347 1.30472 28.51106 1.32313
c2_Automatic_speed_risky_areas 45.935928 29.22081 1.57203 33.92535 1.35403
c2_Collision_avoidance_front_side 0.000000 NA NA NA NA
c2_Collision_avoidance_rear_side 40.419436 25.70547 1.57241 24.25552 1.66640
c2_Collision_avoidance_blind_spot -2.720955 19.61055 -0.13875 19.95039 -0.13639
c2_b_cost -0.006772 3.0658e-04 -22.08882 3.7667e-04 -17.97863
elderly_Assistance_emergency_call 56.893966 56.45982 1.00769 61.86108 0.91971
elderly_Assistance_smart_routes 46.600926 70.78095 0.65838 109.21173 0.42670
male_Assistance_smart_routes -41.162025 31.87764 -1.29125 39.77484 -1.03488
male_Automatic_speed_speed_limit 68.466048 24.19390 2.82989 29.74262 2.30195
elderly_Automatic_speed_risky_areas 33.486718 30.26044 1.10662 31.29469 1.07004
male_Automatic_speed_safe_distance -14.216167 33.67806 -0.42212 38.58391 -0.36845
elderly_Automatic_speed_safe_distance 100.864683 38.38610 2.62764 41.94402 2.40474
elderly_Collision_avoidance_rear_side -53.630956 29.97912 -1.78894 28.36324 -1.89086
elderly_Collision_avoidance_blind_spot -32.889621 31.01654 -1.06039 31.32004 -1.05011
age3049_Assistance_emergency_call -35.096603 53.74925 -0.65297 59.37455 -0.59111
age3049_Assistance_smart_routes -29.698409 69.23271 -0.42897 110.12101 -0.26969
elderly_Assistance_emergency_unit 86.439314 31.30514 2.76119 37.03305 2.33411
delta_class_1 0.000000 NA NA NA NA
delta_class_2 0.112370 0.06544 1.71727 0.07507 1.49696
Summary of class allocation for model component :
Mean prob.
Class_1 0.4719
Class_2 0.5281
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################# ################################ #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Model_2",
modelDescr = "LC model with socios wtp space",
indivID = "ID"
)
# ################################################################# #
#### 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 = apollo_modeChoiceData
database = read.csv("data",header=TRUE)
### for data dictionary, use ?apollo_modeChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_A = 0,
asc_B = 0,
asc_C = 0,
c1_Assistance_emergency_unit = 0,
c1_Assistance_emergency_call = 0,
c1_Assistance_smart_routes = 0,
c1_Automatic_speed_safe_distance = 0,
c1_Automatic_speed_speed_limit = 0,
c1_Automatic_speed_risky_areas = 0,
c1_Collision_avoidance_front_side = 0,
c1_Collision_avoidance_rear_side = 0,
c1_Collision_avoidance_blind_spot = 0,
c1_b_cost = 0,
c2_Assistance_emergency_unit = 0,
c2_Assistance_emergency_call = 0,
c2_Assistance_smart_routes = 0,
c2_Automatic_speed_safe_distance = 0,
c2_Automatic_speed_speed_limit = 0,
c2_Automatic_speed_risky_areas = 0,
c2_Collision_avoidance_front_side = 0,
c2_Collision_avoidance_rear_side = 0,
c2_Collision_avoidance_blind_spot = 0,
c2_b_cost = 0,
elderly_Assistance_emergency_call = 0,
elderly_Assistance_smart_routes = 0,
male_Assistance_smart_routes = 0,
male_Automatic_speed_speed_limit = 0,
elderly_Automatic_speed_risky_areas = 0,
male_Automatic_speed_safe_distance = 0,
elderly_Automatic_speed_safe_distance = 0,
elderly_Collision_avoidance_rear_side = 0,
elderly_Collision_avoidance_blind_spot = 0,
age3049_Assistance_emergency_call = 0,
age3049_Assistance_smart_routes = 0,
elderly_Assistance_emergency_unit = 0,
delta_class_1 = 0,
delta_class_2 = 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("asc_A",
"c1_Assistance_emergency_unit",
"c1_Automatic_speed_safe_distance",
"c1_Collision_avoidance_front_side",
"c2_Assistance_emergency_unit",
"c2_Automatic_speed_safe_distance",
"c2_Collision_avoidance_front_side",
"delta_class_1")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["Assistance_emergency_unit"]] = list(c1_Assistance_emergency_unit,c2_Assistance_emergency_unit)
lcpars[["Assistance_emergency_call"]] = list(c1_Assistance_emergency_call,c2_Assistance_emergency_call)
lcpars[["Assistance_smart_routes"]] = list(c1_Assistance_smart_routes,c2_Assistance_smart_routes)
lcpars[["Automatic_speed_speed_limit"]] = list(c1_Automatic_speed_speed_limit,c2_Automatic_speed_speed_limit)
lcpars[["Automatic_speed_risky_areas"]] = list(c1_Automatic_speed_risky_areas,c2_Automatic_speed_risky_areas)
lcpars[["Automatic_speed_safe_distance"]] = list(c1_Automatic_speed_safe_distance,c2_Automatic_speed_safe_distance)
lcpars[["Collision_avoidance_front_side"]] = list(c1_Collision_avoidance_front_side,c2_Collision_avoidance_front_side)
lcpars[["Collision_avoidance_rear_side"]] = list(c1_Collision_avoidance_rear_side,c2_Collision_avoidance_rear_side)
lcpars[["Collision_avoidance_blind_spot"]] = list(c1_Collision_avoidance_blind_spot,c2_Collision_avoidance_blind_spot)
lcpars[["b_cost"]] = list(c1_b_cost,c2_b_cost)
V=list()
V[["class_1"]] = delta_class_1
V[["class_2"]] = delta_class_2
classAlloc_settings = list(
# mnl_settings = list(
alternatives = c(class_1=1, class_2=2),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
# lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")
# lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
return(lcpars)
}
# ################################################################# #
#### 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()
### Loop over classes
for(s in 1:2){
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["A"]] = b_cost[[s]] * (asc_A
+ (Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1== 1)
+ (Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1)) * (Att1== 2)
+ (Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1)) * (Att1== 3)
+ (Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2== 1)
+ (Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2== 2)
+ (Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2== 3)
+ Collision_avoidance_front_side[[s]] * (Att3== 1)
+ (Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3== 2)
+ Collision_avoidance_blind_spot[[s]] * (Att3== 3)
+ (Att4== 1) * 400
+(Att4== 2) * 600
+(Att4== 3) * 800
+(Att4== 4) * 1000)
V[["B"]] = b_cost[[s]] * (asc_B
+ (Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1.1== 1)
+ (Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1) ) * (Att1.1== 2)
+ (Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1) ) * (Att1.1== 3)
+ (Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2.1== 1)
+ (Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2.1== 2)
+ (Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2.1== 3)
+ Collision_avoidance_front_side[[s]] * (Att3.1== 1)
+ (Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3.1== 2)
+ (Collision_avoidance_blind_spot[[s]]
+ elderly_Collision_avoidance_blind_spot * (age_5099 == 1)) * (Att3.1== 3)
+ (Att4.1== 1) * 400
+(Att4.1== 2) * 600
+(Att4.1== 3) * 800
+(Att4.1== 4) * 1000)
V[["C"]] = (asc_C)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(A=1, B=2, C=3),
##avail = list(alt1=Concept, alt2=Concept.1),
choiceVar = choice,
utilities = V
)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs, functionality)
}
### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, 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)
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################# ################################ #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Model_2_1",
modelDescr = "LC model with socios",
indivID = "ID"
)
# ################################################################# #
#### 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 = apollo_modeChoiceData
database = read.csv("data",header=TRUE)
### for data dictionary, use ?apollo_modeChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_A = 0,
asc_B = 0,
asc_C = 0,
c1_Assistance_emergency_unit = 0,
c1_Assistance_emergency_call = 0,
c1_Assistance_smart_routes = 0,
c1_Automatic_speed_safe_distance = 0,
c1_Automatic_speed_speed_limit = 0,
c1_Automatic_speed_risky_areas = 0,
c1_Collision_avoidance_front_side = 0,
c1_Collision_avoidance_rear_side = 0,
c1_Collision_avoidance_blind_spot = 0,
c1_b_cost = 0,
c2_Assistance_emergency_unit = 0,
c2_Assistance_emergency_call = 0,
c2_Assistance_smart_routes = 0,
c2_Automatic_speed_safe_distance = 0,
c2_Automatic_speed_speed_limit = 0,
c2_Automatic_speed_risky_areas = 0,
c2_Collision_avoidance_front_side = 0,
c2_Collision_avoidance_rear_side = 0,
c2_Collision_avoidance_blind_spot = 0,
c2_b_cost = 0,
elderly_Assistance_emergency_call = 0,
elderly_Assistance_smart_routes = 0,
male_Assistance_smart_routes = 0,
male_Automatic_speed_speed_limit = 0,
elderly_Automatic_speed_risky_areas = 0,
male_Automatic_speed_safe_distance = 0,
elderly_Automatic_speed_safe_distance = 0,
elderly_Collision_avoidance_rear_side = 0,
elderly_Collision_avoidance_blind_spot = 0,
age3049_Assistance_emergency_call = 0,
age3049_Assistance_smart_routes = 0,
elderly_Assistance_emergency_unit = 0,
delta_class_1 = 0,
delta_class_2 = 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("asc_A",
"c1_Assistance_emergency_unit",
"c1_Automatic_speed_safe_distance",
"c1_Collision_avoidance_front_side",
"c2_Assistance_emergency_unit",
"c2_Automatic_speed_safe_distance",
"c2_Collision_avoidance_front_side",
"delta_class_1")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["Assistance_emergency_unit"]] = list(c1_Assistance_emergency_unit,c2_Assistance_emergency_unit)
lcpars[["Assistance_emergency_call"]] = list(c1_Assistance_emergency_call,c2_Assistance_emergency_call)
lcpars[["Assistance_smart_routes"]] = list(c1_Assistance_smart_routes,c2_Assistance_smart_routes)
lcpars[["Automatic_speed_speed_limit"]] = list(c1_Automatic_speed_speed_limit,c2_Automatic_speed_speed_limit)
lcpars[["Automatic_speed_risky_areas"]] = list(c1_Automatic_speed_risky_areas,c2_Automatic_speed_risky_areas)
lcpars[["Automatic_speed_safe_distance"]] = list(c1_Automatic_speed_safe_distance,c2_Automatic_speed_safe_distance)
lcpars[["Collision_avoidance_front_side"]] = list(c1_Collision_avoidance_front_side,c2_Collision_avoidance_front_side)
lcpars[["Collision_avoidance_rear_side"]] = list(c1_Collision_avoidance_rear_side,c2_Collision_avoidance_rear_side)
lcpars[["Collision_avoidance_blind_spot"]] = list(c1_Collision_avoidance_blind_spot,c2_Collision_avoidance_blind_spot)
lcpars[["b_cost"]] = list(c1_b_cost,c2_b_cost)
V=list()
V[["class_1"]] = delta_class_1
V[["class_2"]] = delta_class_2
classAlloc_settings = list(
# mnl_settings = list(
alternatives = c(class_1=1, class_2=2),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
# lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")
# lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
return(lcpars)
}
# ################################################################# #
#### 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()
### Loop over classes
for(s in 1:2){
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["A"]] = (asc_A
+ (Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1== 1)
+ (Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1)) * (Att1== 2)
+ (Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1)) * (Att1== 3)
+ (Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2== 1)
+ (Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2== 2)
+ (Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2== 3)
+ Collision_avoidance_front_side[[s]] * (Att3== 1)
+ (Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3== 2)
+ Collision_avoidance_blind_spot[[s]] * (Att3== 3)
+ b_cost[[s]] * ((Att4== 1) * 400
+(Att4== 2) * 600
+(Att4== 3) * 800
+(Att4== 4) * 1000))
V[["B"]] = (asc_B
+ (Assistance_emergency_unit[[s]]
+ elderly_Assistance_emergency_unit * (age_5099 == 1)) * (Att1.1== 1)
+ (Assistance_emergency_call[[s]]
+ elderly_Assistance_emergency_call * (age_5099 == 1)
+ age3049_Assistance_emergency_call * (age_3049 == 1) ) * (Att1.1== 2)
+ (Assistance_smart_routes[[s]]
+ elderly_Assistance_smart_routes * (age_5099 == 1)
+ age3049_Assistance_smart_routes * (age_3049 == 1)
+ male_Assistance_smart_routes * (gender == 1) ) * (Att1.1== 3)
+ (Automatic_speed_speed_limit[[s]]
+ male_Automatic_speed_speed_limit * (gender == 1)) * (Att2.1== 1)
+ (Automatic_speed_risky_areas[[s]]
+ elderly_Automatic_speed_risky_areas * (age_5099 == 1)) * (Att2.1== 2)
+ (Automatic_speed_safe_distance[[s]]
+ male_Automatic_speed_safe_distance * (gender == 1)
+ elderly_Automatic_speed_safe_distance * (age_5099 == 1)) * (Att2.1== 3)
+ Collision_avoidance_front_side[[s]] * (Att3.1== 1)
+ (Collision_avoidance_rear_side[[s]]
+ elderly_Collision_avoidance_rear_side * (age_5099 == 1)) * (Att3.1== 2)
+ (Collision_avoidance_blind_spot[[s]]
+ elderly_Collision_avoidance_blind_spot * (age_5099 == 1)) * (Att3.1== 3)
+ b_cost[[s]] * ((Att4.1== 1) * 400
+(Att4.1== 2) * 600
+(Att4.1== 3) * 800
+(Att4.1== 4) * 1000))
V[["C"]] = (asc_C)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(A=1, B=2, C=3),
##avail = list(alt1=Concept, alt2=Concept.1),
choiceVar = choice,
utilities = V
)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs, functionality)
}
### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, 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)
for 1)
Code: Select all
deltaMethod_settings=list(expression=c(WTP_Assistance_smart_routes_c1="c1_Assistance_smart_routes/c1_b_cost"))
apollo_deltaMethod(model, deltaMethod_settings)
Running Delta method computation for user-defined function:
Expression Value Robust s.e. Rob t-ratio (0)
WTP_Assistance_smart_routes_c1 -40.9666 38.2392 -1.07
Does this mean that people have negative wtp? -40.9666 €
For class 2
Code: Select all
deltaMethod_settings=list(expression=c(WTP_Assistance_smart_routes_c2="c2_Assistance_smart_routes/c2_b_cost"))
apollo_deltaMethod(model, deltaMethod_settings)
Running Delta method computation for user-defined function:
Expression Value Robust s.e. Rob t-ratio (0)
WTP_Assistance_smart_routes_c2 65.0924 164.5114 0.4
Does this mean that people have negative wtp? 65.0924 € ?
However, the utilities for c1_Assistance_smart_routes from class 1 are 0.305517 and c2_Assistance_smart_routes are -0.089946, so I was expecting the other way around, namely class 1 to have higher wtp for Assistance_smart_routes.
Is this the correct way to calculate the wtp?
for 2)
Code: Select all
### Compute unconditionals
unconditionals = apollo_lcUnconditionals(model, apollo_probabilities, apollo_inputs)
vrc_class_1 = unconditionals[["Assistance_smart_routes"]][[1]]/unconditionals[["b_cost"]][[1]]
vrc_class_2 = unconditionals[["Assistance_smart_routes"]][[2]]/unconditionals[["b_cost"]][[2]]
vrc_unconditional = unconditionals[["pi_values"]][[1]]*vrc_class_1 + unconditionals[["pi_values"]][[2]]*vrc_class_2
Georgios
Last edited by georkapo on 01 Aug 2023, 12:56, edited 5 times in total.
-
- Site Admin
- Posts: 1042
- Joined: 24 Apr 2020, 16:29
Re: errors in Latent class model
Hi
maybe the WTP space model is going to a poor local optimum. How does the fit compare.
apollo_deltaMethod gives you standard errors of functions. Of course, you can also compute the WTP yourself, including from apollo_unconditionals. The results will be the same
Stephane
maybe the WTP space model is going to a poor local optimum. How does the fit compare.
apollo_deltaMethod gives you standard errors of functions. Of course, you can also compute the WTP yourself, including from apollo_unconditionals. The results will be the same
Stephane
Re: errors in Latent class model
Dear Stephane,
Thank you for your reply.
Both models have almost the same LL.
Perhaps I will calculate wtp through the preference space model, do I need to use the weight (probability to fall in a class) of each class in the wtp estimation? if yes, can I do this in Apollo?
As I mentioned previously, the utility for c1_Assistance_smart_routes from class 1 is 0.305517 and c2_Assistance_smart_routes is -0.089946, so I was expecting the wtp estimation to be the other way around, namely class 1 to have higher wtp for Assistance_smart_routes. Your opinion on this would be greatly appreciated. Is this the correct way to calculate the wtp?
Thank you for your time.
Best regards,
Georgios
Thank you for your reply.
Both models have almost the same LL.
Perhaps I will calculate wtp through the preference space model, do I need to use the weight (probability to fall in a class) of each class in the wtp estimation? if yes, can I do this in Apollo?
As I mentioned previously, the utility for c1_Assistance_smart_routes from class 1 is 0.305517 and c2_Assistance_smart_routes is -0.089946, so I was expecting the wtp estimation to be the other way around, namely class 1 to have higher wtp for Assistance_smart_routes. Your opinion on this would be greatly appreciated. Is this the correct way to calculate the wtp?
Thank you for your time.
Best regards,
Georgios
Re: errors in Latent class model
Additionally, the wtp is only one number, e.g., $40 or a range of price $40-45?
Best regards,
Georgios
Best regards,
Georgios
Last edited by georkapo on 17 Aug 2023, 07:12, edited 1 time in total.
-
- Site Admin
- Posts: 1042
- Joined: 24 Apr 2020, 16:29
Re: errors in Latent class model
Hi
this is a question that you would have easily found the solution to in the online examples and the manual, I believe.
See http://apollochoicemodelling.com/files/ ... variates.r for example
Stephane
this is a question that you would have easily found the solution to in the online examples and the manual, I believe.
See http://apollochoicemodelling.com/files/ ... variates.r for example
Stephane
Re: errors in Latent class model
Dear Stephane,
Thank you. I found the solution. I really appreciate your help to me and to all of us.
Best regards,
Georgios
Thank you. I found the solution. I really appreciate your help to me and to all of us.
Best regards,
Georgios
Last edited by georkapo on 24 Aug 2023, 13:55, edited 6 times in total.
Re: errors in Latent class model
Dear Stephane,
One more question related to the LC model.
I have a dataset from five countries, which I use to calculate the model. So far, everything runs fine and also, I can calculate the posterior probability for the aggregated data (all countries).
Now, I would like to calculate the posterior probability for each country, is this possible through Apollo?
I could create five choice models in order to get the posterior probability for each country but I am not sure if this is the correct way. Also, the number of respondents varies among countries. In addition, I do not use the countries in class allocation membership probabilities.
For the class allocation probabilities, I use other variables such as gender, age, high education level as dummies. Can I calculate the posterior probability for these variables individually (e.g. for gender)?
Thank you again for your time.
Best regards,
Georgios
One more question related to the LC model.
I have a dataset from five countries, which I use to calculate the model. So far, everything runs fine and also, I can calculate the posterior probability for the aggregated data (all countries).
Now, I would like to calculate the posterior probability for each country, is this possible through Apollo?
I could create five choice models in order to get the posterior probability for each country but I am not sure if this is the correct way. Also, the number of respondents varies among countries. In addition, I do not use the countries in class allocation membership probabilities.
For the class allocation probabilities, I use other variables such as gender, age, high education level as dummies. Can I calculate the posterior probability for these variables individually (e.g. for gender)?
Thank you again for your time.
Best regards,
Georgios