I try to estimate a latent class model, but the results show that the coefficients of context variables used in class allocation model are zero, therefore, I want to ask how I can solve the problem? or only the socio-demographic variables can be used in class allocation model? Besides, the results show some NAN and the possibilities of class 3 being selected is about 1, so, does it mean there are some code errors in the programming?
the code and results are shown below.
Code: Select all
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
apollo_control = list(
modelName = "LC_sequencial choice_3 class",
modelDescr = "",
indivID = "ID",
nCores = 3,
outputDirectory = "output_less variables"
)
# ################################################################# #
#### 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("all data after cleanning_774_3.0.csv",header=TRUE)
### for data dictionary, use ?apollo_swissRouteChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(c_sim_cary = 0, c_sim_carn = 0, c_sim_bikey = 0, c_sim_biken = 0, c_sim_ebikey = 0, c_sim_ebiken = 0,
c_sim_haily = 0, c_sim_hailn = 0, c_sim_fcary = 0, c_sim_fcarn = 0, c_sim_fhopy = 0, c_sim_fhopn = 0,
c_sim_febiy = 0, c_sim_febin = 0, c_sim_metroy = 0, c_sim_metron = 0, c_sim_busy = 0, c_sim_busn = 0,
c_sim_shopy = 0, c_sim_shopn = 0, c_sim_sebiy = 0, c_sim_sebin = 0,
c_seqt_car = 0, c_seqt_bike = 0, c_seqt_ebike = 0, c_seqt_hail = 0, c_seqt_fcar = 0, c_seqt_fhop = 0,
c_seqt_febi = 0, c_seqt_metro = 0, c_seqt_bus = 0, c_seqt_shop = 0, c_seqt_sebi = 0,
c_seqp_yellow = 0, c_seqp_blue = 0, c_seqp_decline = 0,
delta_sim = 0, delta_seqt = 0,
# delta_seqp = 0,
gamma_sim_age14 = 0, gamma_sim_age18 = 0, gamma_sim_age40 = 0, gamma_sim_age65 = 0,
gamma_sim_edu_middle = 0, gamma_sim_edu_high = 0,
gamma_sim_diredist1 = 0, gamma_sim_diredist2 = 0, gamma_sim_diredist3 = 0,
gamma_sim_purpose1 = 0, gamma_sim_purpose2 = 0, gamma_sim_purpose3 = 0,
gamma_sim_accom1 = 0, gamma_sim_accom2 = 0, gamma_sim_accom3 = 0,
gamma_sim_weather1 = 0, gamma_sim_weather2 = 0, gamma_sim_weather3 = 0,
gamma_seqt_age14 = 0, gamma_seqt_age18 = 0, gamma_seqt_age40 = 0, gamma_seqt_age65 = 0,
gamma_seqt_edu_middle = 0, gamma_seqt_edu_high = 0,
gamma_seqt_diredist1 = 0, gamma_seqt_diredist2 = 0, gamma_seqt_diredist3 = 0,
gamma_seqt_purpose1 = 0, gamma_seqt_purpose2 = 0, gamma_seqt_purpose3 = 0,
gamma_seqt_accom1 = 0, gamma_seqt_accom2 = 0, gamma_seqt_accom3 = 0,
gamma_seqt_weather1 = 0, gamma_seqt_weather2 = 0, gamma_seqt_weather3 = 0
# gamma_seqp_age14 = 0, gamma_seqp_age18 = 0, gamma_seqp_age40 = 0, gamma_seqp_age65 = 0,
# gamma_seqp_edu_middle = 0, gamma_seqp_edu_high = 0,
# gamma_seqp_diredist1 = 0, gamma_seqp_diredist2 = 0, gamma_seqp_diredist3 = 0,
# gamma_seqp_purpose1 = 0, gamma_seqp_purpose2 = 0, gamma_seqp_purpose3 = 0,
# gamma_seqp_accom1 = 0, gamma_seqp_accom2 = 0, gamma_seqp_accom3 = 0,
# gamma_seqp_weather1 = 0, gamma_seqp_weather2 = 0, gamma_seqp_weather3 = 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("c_sim_febin","c_seqt_febi","c_seqp_decline"
# ,"delta_seqp",
# "gamma_seqp_diredist1", "gamma_seqp_diredist2", "gamma_seqp_diredist3",
# "gamma_seqp_purpose1","gamma_seqp_purpose2","gamma_seqp_purpose3",
# "gamma_seqp_accom1","gamma_seqp_accom2","gamma_seqp_accom3",
# "gamma_seqp_weather1","gamma_seqp_weather2","gamma_seqp_weather3",
# "gamma_seqp_age14", "gamma_seqp_age18", "gamma_seqp_age40","gamma_seqp_age65",
# "gamma_seqp_edu_middle", "gamma_seqp_edu_high"
)
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["c_sim"]] = list(c_sim_cary, c_sim_carn, c_sim_bikey, c_sim_biken, c_sim_ebikey, c_sim_ebiken,
c_sim_haily, c_sim_hailn, c_sim_fcary, c_sim_fcarn, c_sim_fhopy, c_sim_fhopn,
c_sim_febiy, c_sim_febin, c_sim_metroy, c_sim_metron, c_sim_busy, c_sim_busn,
c_sim_shopy, c_sim_shopn, c_sim_sebiy, c_sim_sebin)
lcpars[["c_seqt"]] = list(c_seqt_car, c_seqt_bike, c_seqt_ebike, c_seqt_hail, c_seqt_fcar, c_seqt_fhop,
c_seqt_febi, c_seqt_metro, c_seqt_bus, c_seqt_shop, c_seqt_sebi)
lcpars[["c_seqp"]] = list(c_seqp_yellow, c_seqp_blue, c_seqp_decline)
### Utilities of class allocation model
V=list()
V[["class_sim"]] = delta_sim + gamma_sim_age14*age1417 + gamma_sim_age18*age1839 + gamma_sim_age40*age4064 + gamma_sim_age65*age6574 +
gamma_sim_edu_middle*edu_1 + gamma_sim_edu_high*edu_2 + gamma_sim_diredist1*diredist1 + gamma_sim_diredist2*diredist2 + gamma_sim_diredist3*diredist3 +
gamma_sim_purpose1*purpose1 + gamma_sim_purpose2*purpose2 + gamma_sim_purpose3*purpose3 +
gamma_sim_accom1*accom1 + gamma_sim_accom2*accom2 + gamma_sim_accom3*accom3 +
gamma_sim_weather1*weather1 + gamma_sim_weather2*weather2 + gamma_sim_weather3*weather3
V[["class_seqt"]] = delta_seqt + gamma_seqt_age14*age1417 + gamma_seqt_age18*age1839 + gamma_seqt_age40*age4064 + gamma_seqt_age65*age6574 +
gamma_seqt_edu_middle*edu_1 + gamma_seqt_edu_high*edu_2 + gamma_seqt_diredist1*diredist1 + gamma_seqt_diredist2*diredist2 + gamma_seqt_diredist3*diredist3 +
gamma_seqt_purpose1*purpose1 + gamma_seqt_purpose2*purpose2 + gamma_seqt_purpose3*purpose3 +
gamma_seqt_accom1*accom1 + gamma_seqt_accom2*accom2 + gamma_seqt_accom3*accom3 +
gamma_seqt_weather1*weather1 + gamma_seqt_weather2*weather2 + gamma_seqt_weather3*weather3
V[["class_seqp"]] = 0
# delta_seqp + gamma_seqp_age14*age1417 + gamma_seqp_age18*age1839 + gamma_seqp_age40*age4064 + gamma_seqp_age65*age6574 +
# gamma_seqp_edu_middle*edu_1 + gamma_seqp_edu_high*edu_2 + gamma_seqp_diredist1*diredist1 + gamma_seqp_diredist2*diredist2 + gamma_seqp_diredist3*diredist3 +
# gamma_seqp_purpose1*purpose1 + gamma_seqp_purpose2*purpose2 + gamma_seqp_purpose3*purpose3 +
# gamma_seqp_accom1*accom1 + gamma_seqp_accom2*accom2 + gamma_seqp_accom3*accom3 +
# gamma_seqp_weather1*weather1 + gamma_seqp_weather2*weather2 + gamma_seqp_weather3*weather3
### Settings for class allocation models
classAlloc_settings = list(
classes = c(class_sim=1, class_seqt=2, class_seqp=3),
utilities = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
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()
### Define settings for MNL model component that are generic across classes
mnl_settings = list()
### Loop over classes
{
### Compute class-specific utilities
V=list()
V[["sim_cary"]] = c_sim_cary
V[["sim_carn"]] = c_sim_carn
V[["sim_bikey"]] = c_sim_bikey
V[["sim_biken"]] = c_sim_biken
V[["sim_ebikey"]] = c_sim_ebikey
V[["sim_ebiken"]] = c_sim_ebiken
V[["sim_haily"]] = c_sim_haily
V[["sim_hailn"]] = c_sim_hailn
V[["sim_fcary"]] = c_sim_fcary
V[["sim_fcarn"]] = c_sim_fcarn
V[["sim_fhopy"]] = c_sim_fhopy
V[["sim_fhopn"]] = c_sim_fhopn
V[["sim_febiy"]] = c_sim_febiy
V[["sim_febin"]] = c_sim_febin
V[["sim_metroy"]] = c_sim_metroy
V[["sim_metron"]] = c_sim_metron
V[["sim_busy"]] = c_sim_busy
V[["sim_busn"]] = c_sim_busn
V[["sim_shopy"]] = c_sim_shopy
V[["sim_shopn"]] = c_sim_shopn
V[["sim_sebiy"]] = c_sim_sebiy
V[["sim_sebin"]] = c_sim_sebin
mnl_settings$alternatives = c(sim_cary=1, sim_bikey=2, sim_ebikey=3, sim_haily=4, sim_fcary=5, sim_fhopy=6, sim_febiy=7,
sim_metroy=8, sim_busy=9, sim_shopy=10, sim_sebiy=11, sim_carn=12, sim_biken=13, sim_ebiken=14,
sim_hailn=15, sim_fcarn=16, sim_fhopn=17, sim_febin=18, sim_metron=19, sim_busn=20, sim_shopn=21, sim_sebin=22)
mnl_settings$avail = list(sim_cary=av_car, sim_carn=av_car, sim_bikey=av_bike, sim_biken=av_bike, sim_ebikey=av_ebike, sim_ebiken=av_ebike,
sim_fcary=av_fcar, sim_fcarn=av_fcar, sim_haily=1, sim_hailn=1, sim_fhopy=1, sim_fhopn=1, sim_febiy=1, sim_febin=1,
sim_metroy=1, sim_metron=1,sim_busy=1, sim_busn=1, sim_shopy=1, sim_shopn=1, sim_sebiy=1, sim_sebin=1)
mnl_settings$choiceVar = choice1
mnl_settings$utilities = V
mnl_settings$componentName = "class_sim"
### Compute within-class choice probabilities using MNL model
P[["class_sim"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[["class_sim"]] = apollo_panelProd(P[["class_sim"]], apollo_inputs ,functionality)
}
{
### Compute class-specific utilities
V=list()
V[["seqt_car"]] = c_seqt_car
V[["seqt_bike"]] = c_seqt_bike
V[["seqt_ebike"]] = c_seqt_ebike
V[["seqt_hail"]] = c_seqt_hail
V[["seqt_fcar"]] = c_seqt_fcar
V[["seqt_fhop"]] = c_seqt_fhop
V[["seqt_febi"]] = c_seqt_febi
V[["seqt_metro"]] = c_seqt_metro
V[["seqt_bus"]] = c_seqt_bus
V[["seqt_shop"]] = c_seqt_shop
V[["seqt_sebi"]] = c_seqt_sebi
mnl_settings$alternatives = c(seqt_car=1, seqt_bike=2, seqt_ebike=3, seqt_hail=4, seqt_fcar=5, seqt_fhop=6, seqt_febi=7,
seqt_metro=8, seqt_bus=9, seqt_shop=10, seqt_sebi=11)
mnl_settings$avail = list(seqt_car=av_car, seqt_bike=av_bike, seqt_ebike=av_ebike, seqt_fcar=av_fcar, seqt_hail=1, seqt_fhop=1, seqt_febi=1,
seqt_metro=1, seqt_bus=1, seqt_shop=1, seqt_sebi=1)
mnl_settings$choiceVar = choice_onlymode
mnl_settings$utilities = V
mnl_settings$componentName = "class_seqt"
### Compute within-class choice probabilities using MNL model
P[["class_seqt"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[["class_seqt"]] = apollo_panelProd(P[["class_seqt"]], apollo_inputs ,functionality)
}
{
V=list()
V[["seqp_yellow"]] = c_seqp_yellow
V[["seqp_blue"]] = c_seqp_blue
V[["seqp_decline"]] = c_seqp_decline
mnl_settings$alternatives = c(seqp_yellow=1, seqp_blue=2, seqp_decline=3)
mnl_settings$avail = list(seqp_yellow=1, seqp_blue=1, seqp_decline=1)
mnl_settings$choiceVar = choice_parcel
mnl_settings$utilities = V
mnl_settings$componentName = "class_seqp"
### Compute within-class choice probabilities using MNL model
P[["class_seqp"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P[["class_seqp"]] = apollo_panelProd(P[["class_seqp"]], 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 ####
# ################################################################# #
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs)
### Show output in screen
apollo_modelOutput(model)
### Save output to file(s)
apollo_saveOutput(model)
# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()
Code: Select all
LL(start) : -7652.82
LL (whole model) at equal shares, LL(0) : -7652.82
LL (whole model) at observed shares, LL(C) : -6444.36
LL(final, whole model) : -5686.82
Rho-squared vs equal shares : 0.2569
Adj.Rho-squared vs equal shares : 0.2476
Rho-squared vs observed shares : 0.1176
Adj.Rho-squared vs observed shares : 0.1065
AIC : 11515.64
BIC : 11993.54
LL(0,class_sim) : -18153.41
LL(final,class_sim) : -19161.97
LL(0,class_seqt) : -13861.44
LL(final,class_seqt) : -61092.51
LL(0,class_seqp) : -6802.61
LL(final,class_seqp) : -5707.96
Estimated parameters : 71
Time taken (hh:mm:ss) : 00:03:3.31
pre-estimation : 00:00:34.39
estimation : 00:00:8.13
post-estimation : 00:02:20.78
Iterations : 27
Min abs eigenvalue of Hessian : 2.144806
Some eigenvalues of Hessian are positive, indicating potential problems!
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)
c_sim_cary 0.002260 4813.39070 4.695e-07 0.97919 0.002308
c_sim_carn -0.002397 4525.49026 -5.296e-07 0.95318 -0.002514
c_sim_bikey 0.007838 NaN NaN 0.55119 0.014220
c_sim_biken -0.005689 2.703e+04 -2.105e-07 3.25440 -0.001748
c_sim_ebikey 0.002976 2308.52840 1.289e-06 1.18026 0.002521
c_sim_ebiken -4.3429e-04 2831.24786 -1.534e-07 0.90248 -4.8122e-04
c_sim_haily -0.158185 87.92280 -0.001799 3.474e-05 -4552.979559
c_sim_hailn -0.176988 88.37218 -0.002003 4.106e-05 -4310.484679
c_sim_fcary 0.002557 3970.05120 6.441e-07 0.74665 0.003425
c_sim_fcarn -4.8414e-04 2816.04701 -1.719e-07 0.90114 -5.3725e-04
c_sim_fhopy -0.173744 88.29281 -0.001968 3.431e-05 -5064.547043
c_sim_fhopn -0.175544 88.33595 -0.001987 3.449e-05 -5090.387176
c_sim_febiy -0.175112 88.34851 -0.001982 6.9079e-04 -253.493989
c_sim_febin 0.000000 NA NA NA NA
c_sim_metroy -0.197171 88.86426 -0.002219 6.805e-05 -2897.591528
c_sim_metron -0.192326 88.74265 -0.002167 3.717e-05 -5174.280129
c_sim_busy 0.117350 79.27624 0.001480 0.14154 0.829079
c_sim_busn -0.189896 88.68448 -0.002141 8.946e-05 -2122.793467
c_sim_shopy 0.455032 70.22951 0.006479 0.20281 2.243632
c_sim_shopn -0.175978 88.34646 -0.001992 3.436e-05 -5121.512121
c_sim_sebiy 1.389058 59.93704 0.023175 0.19949 6.963002
c_sim_sebin -0.175463 88.33380 -0.001986 2.535e-05 -6920.477167
c_seqt_car 0.868854 NaN NaN 3.43333 0.253064
c_seqt_bike 4.931325 46.96719 0.104995 3.99898 1.233146
c_seqt_ebike 1.112013 NaN NaN 0.96498 1.152371
c_seqt_hail -3.194921 229.82773 -0.013901 0.09014 -35.443590
c_seqt_fcar -1.163665 121.45198 -0.009581 0.67460 -1.724970
c_seqt_fhop -1.061639 90.79176 -0.011693 0.09014 -11.777507
c_seqt_febi 0.000000 NA NA NA NA
c_seqt_metro -12.194283 2.024e+04 -6.0261e-04 0.09022 -135.166029
c_seqt_bus 10.412715 47.47197 0.219345 0.68819 15.130620
c_seqt_shop 7.735534 47.48225 0.162914 0.98062 7.888410
c_seqt_sebi -3.589986 278.59359 -0.012886 0.09095 -39.473087
c_seqp_yellow -0.215551 0.02676 -8.055773 0.04960 -4.345423
c_seqp_blue -1.795532 0.04736 -37.909128 0.07305 -24.579370
c_seqp_decline 0.000000 NA NA NA NA
delta_sim -11.186101 14.61193 -0.765546 0.05753 -194.437993
delta_seqt -11.014189 60.09725 -0.183273 1.55093 -7.101648
gamma_sim_age14 1.478712 27.92189 0.052959 0.23332 6.337781
gamma_sim_age18 -0.552674 21.32379 -0.025918 0.12269 -4.504722
gamma_sim_age40 -0.626504 21.94164 -0.028553 0.10776 -5.813744
gamma_sim_age65 0.454382 21.24178 0.021391 0.13041 3.484305
gamma_sim_edu_middle -0.251430 15.78620 -0.015927 0.07089 -3.546949
gamma_sim_edu_high 1.343784 15.80614 0.085017 0.11022 12.192307
gamma_sim_diredist1 0.000000 18.40729 0.000000 7.367e-09 0.000000
gamma_sim_diredist2 0.000000 18.40729 0.000000 6.169e-09 0.000000
gamma_sim_diredist3 0.000000 18.40729 0.000000 2.121e-09 0.000000
gamma_sim_purpose1 0.000000 18.37306 0.000000 6.026e-09 0.000000
gamma_sim_purpose2 0.000000 18.37306 0.000000 4.234e-09 0.000000
gamma_sim_purpose3 0.000000 18.37306 0.000000 2.887e-09 0.000000
gamma_sim_accom1 0.000000 18.38668 0.000000 5.318e-09 0.000000
gamma_sim_accom2 0.000000 18.38668 0.000000 4.053e-09 0.000000
gamma_sim_accom3 0.000000 18.38668 0.000000 8.480e-09 0.000000
gamma_sim_weather1 0.000000 18.43811 0.000000 6.838e-09 0.000000
gamma_sim_weather2 0.000000 18.43811 0.000000 6.218e-09 0.000000
gamma_sim_weather3 0.000000 18.43811 0.000000 4.481e-09 0.000000
gamma_seqt_age14 -1.501831 109.14038 -0.013761 2.07960 -0.722171
gamma_seqt_age18 -7.002340 223.78269 -0.031291 0.94401 -7.417641
gamma_seqt_age40 4.442438 59.89233 0.074174 0.83697 5.307741
gamma_seqt_age65 5.581937 59.90462 0.093180 1.40756 3.965697
gamma_seqt_edu_middle 0.791381 4.40824 0.179523 0.67590 1.170858
gamma_seqt_edu_high 2.130448 4.36812 0.487727 1.42954 1.490302
gamma_seqt_diredist1 0.000000 NaN NaN 1.947e-09 0.000000
gamma_seqt_diredist2 0.000000 NaN NaN 1.344e-09 0.000000
gamma_seqt_diredist3 0.000000 NaN NaN 1.356e-09 0.000000
gamma_seqt_purpose1 0.000000 NaN NaN 5.293e-08 0.000000
gamma_seqt_purpose2 0.000000 NaN NaN 5.286e-08 0.000000
gamma_seqt_purpose3 0.000000 NaN NaN 5.287e-08 0.000000
gamma_seqt_accom1 0.000000 0.63693 0.000000 9.196e-10 0.000000
gamma_seqt_accom2 0.000000 0.63693 0.000000 1.361e-09 0.000000
gamma_seqt_accom3 0.000000 0.63693 0.000000 1.372e-09 0.000000
gamma_seqt_weather1 0.000000 42.48892 0.000000 1.788e-08 0.000000
gamma_seqt_weather2 0.000000 42.48892 0.000000 1.588e-08 0.000000
gamma_seqt_weather3 0.000000 42.48892 0.000000 1.589e-08 0.000000
Summary of class allocation for model component :
Mean prob.
class_sim 1.225e-05
class_seqt 0.002763
class_seqp 0.997225
Qiang