I want to add the panel term in the latent class model, the panel term is assumed to vary over individuals but be constant over choice situation for each individual, the results show that the efficients values of one class are very high while another are normal. Could you please tell me how to add the panel term in the latent class model? Thanks very much!
Code: Select all
### Initialise
rm(list = ls())
library(apollo)
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "social demo as class membership",
modelDescr = "social demo as class membership",
indivID = "ID",
nCores = 16,
outputDirectory = "output"
)
database = read.csv("apollo_data.csv",header=TRUE)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(# Choice parameters
asc_1_A = 0, asc_1_B = 0,
asc_2_A = 0, asc_2_B = 0,
sigma_panel = 0,
b_traveltime_A = 0, b_traveltime_B = 0,
b_sd_A = 0, b_sd_B = 0,
b_crowded_1_A = 0, b_crowded_1_B = 0,
b_crowded_2_A = 0, b_crowded_2_B = 0,
b_crowded_3_A = 0, b_crowded_3_B = 0,
b_cost_A = 0, b_cost_B = 0,
b_transfers_A = 0, b_transfers_B = 0,
b_transfert_A = 0, b_transfert_B = 0,
b_seat_A = 0, b_seat_B = 0,
b_familiar_A = 0, b_familiar_B = 0,
b_tt_pur_A = 0, b_tt_pur_B = 0,
b_tt_pre_A = 0, b_tt_pre_B = 0,
beta_gender_male_A = 0, beta_gender_male_B = 0,
beta_stay_1_A = 0, beta_stay_1_B = 0,
beta_driver_A = 0, beta_driver_B = 0,
beta_income_1_A = 0, beta_income_1_B = 0,
beta_income_2_A = 0, beta_income_2_B = 0,
beta_age_1_A = 0, beta_age_1_B = 0,
beta_com_metro_A = 0, beta_com_metro_B = 0,
beta_ncom_metro_A = 0, beta_ncom_metro_B = 0,
# Class allocation parameters
delta_A = 0, delta_B = 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_2_A","asc_2_B","delta_B","beta_gender_male_B","beta_age_1_B","beta_income_1_B","beta_income_2_B","beta_stay_1_B","beta_driver_B","beta_com_metro_B","beta_ncom_metro_B")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interNormDraws = c("draws_hsk1","draws_hsk2"))
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["hsk1"]] = sigma_panel*draws_hsk1
randcoeff[["hsk2"]] = sigma_panel*draws_hsk2
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["asc_1" ]] = list(asc_1_A , asc_1_B )
lcpars[["asc_2" ]] = list(asc_2_A , asc_2_B )
lcpars[["b_traveltime"]] = list(b_traveltime_A , b_traveltime_B)
lcpars[["b_sd" ]] = list(b_sd_A , b_sd_B )
lcpars[["b_crowded_1" ]] = list(b_crowded_1_A , b_crowded_1_B )
lcpars[["b_crowded_2" ]] = list(b_crowded_2_A , b_crowded_2_B )
lcpars[["b_crowded_3" ]] = list(b_crowded_3_A , b_crowded_3_B )
lcpars[["b_cost" ]] = list(b_cost_A , b_cost_B )
lcpars[["b_transfers" ]] = list(b_transfers_A , b_transfers_B )
lcpars[["b_transfert" ]] = list(b_transfert_A , b_transfert_B )
lcpars[["b_seat" ]] = list(b_seat_A , b_seat_B )
lcpars[["b_familiar" ]] = list(b_familiar_A , b_familiar_B)
lcpars[["b_tt_pur" ]] = list(b_tt_pur_A , b_tt_pur_B)
lcpars[["b_tt_pre" ]] = list(b_tt_pre_A , b_tt_pre_B)
V = list(class_a = delta_A+beta_gender_male_A*gender_male+beta_age_1_A*age_e+beta_income_1_A*income_1+beta_income_2_A*income_2+beta_stay_1_A*stay_e+beta_driver_A*driver+beta_com_metro_A*com_metro+beta_ncom_metro_A*ncom_metro,
class_b = delta_B+beta_gender_male_B*gender_male+beta_age_1_B*age_e+beta_income_1_B*income_1+beta_income_2_B*income_2+beta_stay_1_B*stay_e+beta_driver_B*driver+beta_com_metro_B*com_metro+beta_ncom_metro_B*ncom_metro)
### Settings for class allocation models
classAlloc_settings = list(
classes = c(class_a=1, class_b=2),
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"){
### Initialise
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1 = 1, alt2 = 1),
choiceVar = best
)
S <- 2
for(s in 1:S){
### Utilities for alternatives
V = list()
V[["alt1"]] = asc_1[[s]] +b_traveltime[[s]]*R1_traveltime+b_sd[[s]]*R1_sd+b_crowded_1[[s]]*R1_crowded_1+ b_crowded_2[[s]]*R1_crowded_2+ b_crowded_3[[s]]*R1_crowded_3+b_cost[[s]]*R1_cost+ b_transfers[[s]]*R1_transfers + b_transfert[[s]]*R1_transtime + b_seat[[s]]*R1_seat + b_familiar[[s]]*R1_familiar+ b_tt_pur[[s]]*R1_traveltime*pur + b_tt_pre[[s]]*R1_traveltime*pre+hsk1
V[["alt2"]] = asc_2[[s]] +b_traveltime[[s]]*R2_traveltime+b_sd[[s]]*R2_sd+b_crowded_1[[s]]*R2_crowded_1+ b_crowded_2[[s]]*R2_crowded_2+ b_crowded_3[[s]]*R2_crowded_3+b_cost[[s]]*R2_cost+ b_transfers[[s]]*R2_transfers + b_transfert[[s]]*R2_transtime + b_seat[[s]]*R2_seat + b_familiar[[s]]*R2_familiar+ b_tt_pur[[s]]*R2_traveltime*pur + b_tt_pre[[s]]*R2_traveltime*pre+hsk2
### Define settings for MNL model component
mnl_settings$utilities = V
mnl_settings$componentName = paste0("Class_",s)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
}
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
P = apollo_panelProd(P, apollo_inputs, functionality)
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
estimate_settings = list(maxIterations = 400)
model = apollo_estimate(apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
estimate_settings=estimate_settings)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Code: Select all
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_1_A -12.265959 135.887139 -0.09027 1.761668 -6.96270
asc_1_B 0.095975 0.041253 2.32650 0.037517 2.55820
asc_2_A 0.000000 NA NA NA NA
asc_2_B 0.000000 NA NA NA NA
sigma_panel -0.003908 0.062434 -0.06259 0.002676 -1.46036
b_traveltime_A -40.069785 417.817473 -0.09590 5.105042 -7.84906
b_traveltime_B -0.127703 0.008018 -15.92605 0.009797 -13.03485
b_sd_A -6.150171 59.345161 -0.10363 0.817306 -7.52493
b_sd_B -0.040716 0.012580 -3.23669 0.012841 -3.17072
b_crowded_1_A 189.510608 1947.191969 0.09733 24.335499 7.78741
b_crowded_1_B 0.552527 0.056367 9.80239 0.061553 8.97642
b_crowded_2_A 40.171986 420.835024 0.09546 4.841213 8.29792
b_crowded_2_B 0.119101 0.060908 1.95545 0.058396 2.03953
b_crowded_3_A -57.866281 596.305548 -0.09704 7.321333 -7.90379
b_crowded_3_B -0.347249 0.061037 -5.68920 0.066292 -5.23819
b_cost_A 180.873897 1877.033936 0.09636 23.040848 7.85014
b_cost_B -0.628583 0.064329 -9.77130 0.062828 -10.00474
b_transfers_A -157.078402 1625.458541 -0.09664 19.727091 -7.96257
b_transfers_B -0.402618 0.059174 -6.80399 0.062401 -6.45210
b_transfert_A 10.318179 107.955343 0.09558 1.198330 8.61047
b_transfert_B -0.065262 0.015727 -4.14971 0.015134 -4.31240
b_seat_A 58.228285 597.252332 0.09749 7.435896 7.83070
b_seat_B 0.487150 0.031054 15.68727 0.035592 13.68722
b_familiar_A 40.000055 418.488401 0.09558 5.096076 7.84919
b_familiar_B 0.164488 0.032391 5.07815 0.035545 4.62756
b_tt_pur_A -46.937469 488.218721 -0.09614 5.995559 -7.82871
b_tt_pur_B 0.073335 0.011794 6.21784 0.010776 6.80517
b_tt_pre_A -6.731735 70.533965 -0.09544 0.808283 -8.32844
b_tt_pre_B -0.061951 0.008757 -7.07414 0.008140 -7.61046
beta_gender_male_A -0.048532 0.059183 -0.82003 0.062268 -0.77941
beta_gender_male_B 0.000000 NA NA NA NA
beta_stay_1_A 0.123463 0.063634 1.94020 0.072095 1.71251
beta_stay_1_B 0.000000 NA NA NA NA
beta_driver_A -0.034268 0.073338 -0.46727 0.076106 -0.45027
beta_driver_B 0.000000 NA NA NA NA
beta_income_1_A 0.059468 0.091000 0.65350 0.096040 0.61920
beta_income_1_B 0.000000 NA NA NA NA
beta_income_2_A -0.007143 0.089019 -0.08024 0.095551 -0.07475
beta_income_2_B 0.000000 NA NA NA NA
beta_age_1_A 0.023061 0.072817 0.31671 0.081670 0.28237
beta_age_1_B 0.000000 NA NA NA NA
beta_com_metro_A -0.040796 0.073363 -0.55609 0.075496 -0.54038
beta_com_metro_B 0.000000 NA NA NA NA
beta_ncom_metro_A -0.003066 0.060130 -0.05099 0.064939 -0.04722
beta_ncom_metro_B 0.000000 NA NA NA NA
delta_A -0.834315 0.129857 -6.42487 0.136619 -6.10690
delta_B 0.000000 NA NA NA NA