Important: Read this before posting to this forum

  1. 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.
  2. 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
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. 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
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. 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.

latent class with latent variable

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
lxiaolei
Posts: 7
Joined: 21 Sep 2023, 11:22

latent class with latent variable

Post by lxiaolei »

Hi,

I am trying to run a hybrid latent class choice model (just like Hybrid_LC_with_OL examples), and the latent variables(LVs) is the part of the class membership model instead of a class-specific model, in other words, the class allocation model is a function of the LV, however, the results show that estimated parameter values (beta) of class membership became unreasonably high(such as beta_age_2_A, beta_stay_2a) and the t.rat.(0) of few estimated parameter values is NAN (such as gamma1_stay_1, gamma1_income_1). Could you please give me some advice to deal with it?

I attach my code and the result of the estimation. I would be immensely appreciative if you could assist me with this challenge.

Code: Select all

### Initialise
rm(list = ls())
library(apollo)

apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  = "0922Hybrid_Route_choice _model_2trait",
  modelDescr = "Route choice model, using ordered measurement model for indicators",
  indivID    = "ID",
  nCores     = 5,
  outputDirectory = "output"
)
database = read.csv("D:/apollo_data_f_2trait_binary.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_pressure_A          = 0, b_pressure_B          = 0,
  b_purpose_A           = 0, b_purpose_B           = 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,
  beta_gender_male_A    = 0, beta_gender_male_B    = 0,
  beta_age_1_A          = 0, beta_age_1_B          = 0,
  beta_age_2_A          = 0, beta_age_2_B          = 0,
  beta_stay_1_A         = 0, beta_stay_1_B         = 0,
  beta_stay_2_A         = 0, beta_stay_2_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,
  lambda_cur_A          = 0.2, lambda_cur_B          = 0,
  lambda_adv_A          = 0.1, lambda_adv_B          = 0,
  #lambda_ada_A          = 0, lambda_ada_B         = 0,
  gamma1_age_1           = 0, gamma1_age_2         = 0,
  gamma1_gender_male     = 0, #gamma1_driver       = 0,
  gamma1_income_1        = 0, gamma1_income_2      = 0,
  gamma1_stay_1          = 0, gamma1_stay_2        = 0,
  gamma2_age_1           = 0, gamma2_age_2         = 0,
  gamma2_gender_male     = 0, #gamma2_driver       = 0,
  gamma2_income_1        = 0, gamma2_income_2      = 0,
  gamma2_stay_1          = 0, gamma2_stay_2        = 0,
  sigma_eta1            =1, sigma_eta2           = 1,  

  # Class allocation parameters
  delta_A = 0, delta_B = 0,
  # Measurement equations parameters
  zeta_cur1      = 1, zeta_cur2   = 1, zeta_cur3  = 1,
  tau_cur1_1     =-2, tau_cur1_2     =-1, tau_cur1_3     = 1, tau_cur1_4     = 2,
  tau_cur2_1     =-2, tau_cur2_2     =-1, tau_cur2_3     = 1, tau_cur2_4     = 2, 
  tau_cur3_1     =-2, tau_cur3_2     =-1, tau_cur3_3     = 1, tau_cur3_4     = 2,
  zeta_adv1      = 1, zeta_adv2   = 1, zeta_adv3  = 1, zeta_adv4  = 1,
  tau_adv1_1     =-2, tau_adv1_2     =-1, tau_adv1_3     = 1, tau_adv1_4     = 2,
  tau_adv2_1     =-2, tau_adv2_2     =-1, tau_adv2_3     = 1, tau_adv2_4     = 2, 
  tau_adv3_1     =-2, tau_adv3_2     =-1, tau_adv3_3     = 1, tau_adv3_4     = 2, 
  tau_adv4_1     =-2, tau_adv4_2     =-1, tau_adv4_3     = 1, tau_adv4_4     = 2)

### 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_age_2_B","beta_income_1_B","beta_income_2_B","beta_stay_1_B","beta_stay_2_B","beta_driver_B","lambda_cur_B","lambda_adv_B")#,,"lambda_ada_B"
# ################################################################# #

#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "sobol", 
  interNDraws    = 100,
  interNormDraws = c("draw_eta1","draw_eta2","draws_hsk1","draws_hsk2"))

### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
  randcoeff = list()
  randcoeff[["LV1"]] = gamma1_age_1*age_1+gamma1_age_2*age_2+gamma1_income_1*income_1+gamma1_income_2*income_2+gamma1_stay_1*stay_1+gamma1_stay_2*stay_2+gamma1_gender_male*gender_male + sigma_eta1*draw_eta1
  randcoeff[["LV2"]] = gamma2_age_1*age_1+gamma2_age_2*age_2+gamma2_income_1*income_1+gamma2_income_2*income_2+gamma2_stay_1*stay_1+gamma2_stay_2*stay_2+gamma2_gender_male*gender_male + sigma_eta2*draw_eta2
  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_pressure"  ]] = list(b_pressure_A   , b_pressure_B  )
  lcpars[["b_purpose"   ]] = list(b_purpose_A    , b_purpose_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  )
  
  V = list()
  V[["class_a"]] = delta_A+beta_gender_male_A*gender_male+beta_age_1_A*age_1+beta_age_2_A*age_2+beta_income_1_A*income_1+beta_income_2_A*income_2+beta_stay_1_A*stay_1+beta_stay_2_A*stay_2+ beta_driver_A*driver+lambda_cur_A*LV1+lambda_adv_A*LV2
  V[["class_b"]] = delta_B+beta_gender_male_B*gender_male+beta_age_1_B*age_1+beta_age_2_B*age_2+beta_income_1_B*income_1+beta_income_2_B*income_2+beta_stay_1_B*stay_1+beta_stay_2_B*stay_2+ beta_driver_B*driver+lambda_cur_B*LV1+lambda_adv_B*LV2
  ### 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()
  ### Likelihood of indicators
  ol_settings1 = list(outcomeOrdered = cur1, 
                      V              = zeta_cur1*LV1, 
                      tau            = list(tau_cur1_1, tau_cur1_2, tau_cur1_3, tau_cur1_4),
                      rows           = (task==1))
  ol_settings2 = list(outcomeOrdered = cur2, 
                      V              = zeta_cur2*LV1, 
                      tau            = list(tau_cur2_1, tau_cur2_2, tau_cur2_3, tau_cur2_4), 
                      rows           = (task==1))
  ol_settings3 = list(outcomeOrdered = cur3, 
                      V              = zeta_cur3*LV1, 
                      tau            = list(tau_cur3_1, tau_cur3_2, tau_cur3_3, tau_cur3_4), 
                      rows           = (task==1))
  ol_settings4 = list(outcomeOrdered = adv1, 
                      V              = zeta_adv1*LV2, 
                      tau            = list(tau_adv1_1, tau_adv1_2, tau_adv1_3, tau_adv1_4), 
                      rows           = (task==1))
  ol_settings5 = list(outcomeOrdered = adv2, 
                      V              = zeta_adv2*LV2, 
                      tau            = list(tau_adv2_1, tau_adv2_2, tau_adv2_3, tau_adv2_4), 
                      rows           = (task==1))
  ol_settings6 = list(outcomeOrdered = adv3, 
                      V              = zeta_adv3*LV2, 
                      tau            = list(tau_adv3_1, tau_adv3_2, tau_adv3_3, tau_adv3_4),
                      rows           = (task==1))
  ol_settings7 = list(outcomeOrdered = adv4, 
                      V              = zeta_adv4*LV2, 
                      tau            = list(tau_adv4_1, tau_adv4_2, tau_adv4_3, tau_adv4_4),
                      rows           = (task==1))
  P[["indic_cur1"]] = apollo_ol(ol_settings1, functionality)
  P[["indic_cur2"]] = apollo_ol(ol_settings2, functionality)
  P[["indic_cur3"]] = apollo_ol(ol_settings3, functionality)
  P[["indic_adv1"]] = apollo_ol(ol_settings4, functionality)
  P[["indic_adv2"]] = apollo_ol(ol_settings5, functionality)
  P[["indic_adv3"]] = apollo_ol(ol_settings6, functionality)
  P[["indic_adv4"]] = apollo_ol(ol_settings7, functionality)
  
  P[["indic_cur1"]] = apollo_panelProd(P[["indic_cur1"]], apollo_inputs, functionality)
  P[["indic_cur2"]] = apollo_panelProd(P[["indic_cur2"]], apollo_inputs, functionality)
  P[["indic_cur3"]] = apollo_panelProd(P[["indic_cur3"]], apollo_inputs, functionality)
  P[["indic_adv1"]] = apollo_panelProd(P[["indic_adv1"]], apollo_inputs, functionality)
  P[["indic_adv2"]] = apollo_panelProd(P[["indic_adv2"]], apollo_inputs, functionality)
  P[["indic_adv3"]] = apollo_panelProd(P[["indic_adv3"]], apollo_inputs, functionality)
  P[["indic_adv4"]] = apollo_panelProd(P[["indic_adv4"]], apollo_inputs, functionality)
  
  ### Likelihood of choices inside each class
  S <- 2
  for(s in 1:S){
    ### Utilities for alternatives
    V = list()
    V[["alt1"]] = asc_1[[s]] + b_pressure[[s]]*pre+b_purpose[[s]]*pur+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 + hsk1[[s]]
    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 + hsk2[[s]]
    ### Define settings for MNL model component
    mnl_settings = list(
      alternatives = c(alt1=1, alt2=2),
      avail       = list(alt1 = 1, alt2 = 1),
      choiceVar    = best,
      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)
    
    ### 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[paste0("Class_", 1:S)], classProb=pi_values)
  P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Comment out as necessary
  P = apollo_combineModels(P, apollo_inputs, functionality)
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### CALCULATE LL AT STARTING VALUES                             ####
# ################################################################# #

apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

outputs:

Code: Select all

Estimates:
                      Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
asc_1_A              -0.039893    0.039397    -1.01259    0.047408      -0.84148
asc_1_B              -0.050460    0.038607    -1.30701    0.045197      -1.11645
asc_2_A               0.000000          NA          NA          NA            NA
asc_2_B               0.000000          NA          NA          NA            NA
sigma_panel          -0.029148    0.012517    -2.32858    0.020298      -1.43602
b_pressure_A          0.149372    0.045324     3.29567    0.058547       2.55131
b_pressure_B          0.119424    0.044807     2.66529    0.058278       2.04921
b_purpose_A           0.025852    0.046473     0.55628    0.058303       0.44341
b_purpose_B           0.133614    0.045571     2.93202    0.058743       2.27456
b_traveltime_A       -0.144596    0.004465   -32.38665    0.006909     -20.92741
b_traveltime_B       -0.122074    0.004263   -28.63618    0.006777     -18.01392
b_sd_A               -0.038755    0.007197    -5.38495    0.008864      -4.37223
b_sd_B               -0.032785    0.007138    -4.59273    0.008820      -3.71714
b_crowded_1_A         1.133788    0.051083    22.19509    0.073789      15.36528
b_crowded_1_B         1.032829    0.049217    20.98509    0.076393      13.51999
b_crowded_2_A         0.638636    0.046774    13.65362    0.058698      10.88012
b_crowded_2_B         0.558682    0.046067    12.12764    0.061602       9.06919
b_crowded_3_A         0.435881    0.050645     8.60663    0.069767       6.24770
b_crowded_3_B         0.413918    0.048956     8.45488    0.071564       5.78390
b_cost_A             -0.308637    0.031436    -9.81785    0.040948      -7.53729
b_cost_B             -0.237101    0.031052    -7.63568    0.037992      -6.24073
b_transfers_A        -0.595704    0.032144   -18.53234    0.039997     -14.89389
b_transfers_B        -0.557935    0.031435   -17.74881    0.041840     -13.33483
b_transfert_A        -0.025098    0.008788    -2.85596    0.010532      -2.38300
b_transfert_B        -0.007340    0.008679    -0.84566    0.010577      -0.69393
b_seat_A              0.939289    0.034292    27.39107    0.049676      18.90815
b_seat_B              0.862335    0.033336    25.86825    0.052280      16.49468
b_familiar_A          0.486327    0.036100    13.47153    0.048811       9.96341
b_familiar_B          0.444920    0.034854    12.76512    0.049581       8.97361
beta_gender_male_A  -26.844289    8.065819    -3.32815    9.133750      -2.93902
beta_gender_male_B    0.000000          NA          NA          NA            NA
beta_age_1_A          3.639315   36.394790     0.10000   50.552253       0.07199
beta_age_1_B          0.000000          NA          NA          NA            NA
beta_age_2_A         43.164409    9.731764     4.43541   48.405431       0.89173
beta_age_2_B          0.000000          NA          NA          NA            NA
beta_stay_1_A       -20.945266   11.846079    -1.76812   16.085827      -1.30209
beta_stay_1_B         0.000000          NA          NA          NA            NA
beta_stay_2_A      -153.780088    6.965050   -22.07882    3.290498     -46.73459
beta_stay_2_B         0.000000          NA          NA          NA            NA
beta_driver_A      -147.452551    4.949349   -29.79231    2.795206     -52.75195
beta_driver_B         0.000000          NA          NA          NA            NA
beta_income_1_A       6.664383   14.699682     0.45337   33.674117       0.19791
beta_income_1_B       0.000000          NA          NA          NA            NA
beta_income_2_A      71.342750   14.479661     4.92710   32.936725       2.16605
beta_income_2_B       0.000000          NA          NA          NA            NA
lambda_cur_A         -1.351149         NaN         NaN    0.983799      -1.37340
lambda_cur_B          0.000000          NA          NA          NA            NA
lambda_adv_A        -65.263184    1.805382   -36.14924    1.117285     -58.41229
lambda_adv_B          0.000000          NA          NA          NA            NA
gamma1_age_1         -0.992068    9.448086    -0.10500    9.384747      -0.10571
gamma1_age_2         -0.555660    9.502822    -0.05847    9.532885      -0.05829
gamma1_gender_male    0.767131    1.462775     0.52444    1.818196       0.42192
gamma1_income_1      -7.183109         NaN         NaN    7.021029      -1.02308
gamma1_income_2      -1.131559    2.381899    -0.47507    2.815721      -0.40187
gamma1_stay_1        -4.047862         NaN         NaN    3.880835      -1.04304
gamma1_stay_2        -4.779323         NaN         NaN    4.153672      -1.15063
gamma2_age_1         -1.070795    0.228711    -4.68187    0.145042      -7.38266
gamma2_age_2         -1.231593    0.238957    -5.15403    0.179523      -6.86038
gamma2_gender_male    0.032861    0.073058     0.44980    0.078401       0.41914
gamma2_income_1      -0.399487    0.103622    -3.85523    0.107913      -3.70193
gamma2_income_2      -0.001100    0.105160    -0.01046    0.104990      -0.01047
gamma2_stay_1        -0.190162    0.093590    -2.03185    0.103844      -1.83123
gamma2_stay_2         0.035005    0.084725     0.41315    0.080982       0.43225
sigma_eta1           21.840130         NaN         NaN   17.792217       1.22751
sigma_eta2            1.142020         NaN         NaN    0.044502      25.66249
delta_A              38.751674   52.360741     0.74009   42.594304       0.90979
delta_B               0.000000          NA          NA          NA            NA
zeta_cur1             0.120446         NaN         NaN    0.098894       1.21792
zeta_cur2             0.090662         NaN         NaN    0.074030       1.22466
zeta_cur3             0.085019         NaN         NaN    0.069150       1.22948
tau_cur1_1           -8.284693    1.263093    -6.55905    1.212597      -6.83219
tau_cur1_2           -5.381595    1.183656    -4.54659    1.131645      -4.75555
tau_cur1_3           -2.349337    1.146995    -2.04825    1.113317      -2.11021
tau_cur1_4            0.918264    1.147787     0.80003    1.121877       0.81851
tau_cur2_1           -6.319922    0.944003    -6.69481    0.917605      -6.88741
tau_cur2_2           -4.192761    0.898342    -4.66722    0.872840      -4.80359
tau_cur2_3           -1.975126    0.871978    -2.26511    0.848516      -2.32774
tau_cur2_4            0.626114    0.862205     0.72618    0.835645       0.74926
tau_cur3_1           -5.951373    0.872117    -6.82405    0.852054      -6.98474
tau_cur3_2           -3.923394    0.833455    -4.70738    0.810266      -4.84211
tau_cur3_3           -1.518294    0.813718    -1.86587    0.795190      -1.90935
tau_cur3_4            1.643939    0.811357     2.02616    0.796827       2.06311
zeta_adv1             0.903667    0.062080    14.55659    0.078624      11.49352
zeta_adv2             1.066225    0.066498    16.03398    0.086462      12.33176
zeta_adv3             3.505775    0.277847    12.61766    0.336343      10.42322
zeta_adv4             4.449595    0.519472     8.56560    0.602516       7.38502
tau_adv1_1           -4.692933    0.327418   -14.33314    0.273404     -17.16484
tau_adv1_2           -2.408972    0.285626    -8.43399    0.228935     -10.52249
tau_adv1_3           -0.601684    0.273257    -2.20190    0.212384      -2.83299
tau_adv1_4            1.445020    0.279389     5.17208    0.222450       6.49593
tau_adv2_1           -5.036039    0.369834   -13.61704    0.303982     -16.56690
tau_adv2_2           -3.263596    0.336514    -9.69825    0.267111     -12.21811
tau_adv2_3           -1.549738    0.321371    -4.82227    0.250184      -6.19438
tau_adv2_4            0.585733    0.316455     1.85092    0.242946       2.41096
tau_adv3_1           -9.642209    1.259403    -7.65618    1.093547      -8.81737
tau_adv3_2           -6.158388    1.130483    -5.44757    0.928054      -6.63581
tau_adv3_3           -2.149920    1.026870    -2.09366    0.782420      -2.74778
tau_adv3_4            0.851479    0.997524     0.85359    0.731746       1.16363
tau_adv4_1           -9.815360    1.616936    -6.07035    1.372504      -7.15142
tau_adv4_2           -6.329045    1.424497    -4.44300    1.112205      -5.69054
tau_adv4_3           -2.085414    1.279170    -1.63029    0.925873      -2.25238
tau_adv4_4            1.241097    1.268905     0.97808    0.935864       1.32615
Thanks very much for your time. Looking forward to hearing from you.
Best wishes,
Xiaolei
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: latent class with latent variable

Post by stephanehess »

Hi

did you first try a latent class model without the LVs?

There's clearly an identification issue somewhere, and it would be best to gradually build up the complexity

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
lxiaolei
Posts: 7
Joined: 21 Sep 2023, 11:22

Re: latent class with latent variable

Post by lxiaolei »

Hi, Stephane,

Thanks very much for your reply.
Yes, I tried to build the latent class model without latent variable(LV), and the model performed well. I also built the hybrid choice model including LV, which didn’t include the latent class sub-model, the model ran normally as well. However, when I combine these two models, the new model has issues.
Now, I am entirely sure that the issue lies in the class allocation (latent class) model part. I tried many times and found that the estimated parameter values of class allocation parts are always high no matter adding socio-demographic variables or latent variables.

Best wishes,
Xiaolei
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: latent class with latent variable

Post by stephanehess »

hi

can you show those outputs too, please?

Thanks
--------------------------------
Stephane Hess
www.stephanehess.me.uk
lxiaolei
Posts: 7
Joined: 21 Sep 2023, 11:22

Re: latent class with latent variable

Post by lxiaolei »

Dear Stephane,

Thanks very much for your help.
I think I solved the problem by setting the separate probability functions. It took me a long time to solve this problem, thanks for your advice and the post in the forum, which gives me lots of inspiration.
Now the the coefficient value seems fine, see below, however, a new issue arises, there are many NANs in the model estimation results(specifically, s.e. t.rat.(0)) though the model is converged.
Could you tell me how to solve this problem? I tried 100,200,300,400 draws of Halton, Sobol, and Mlhs, but the issue is still not resolved.

Best wishes,
Xiaolei

Code: Select all

                      Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
asc_1_A              -0.456470    0.156229     -2.9218    0.211577       -2.1575
asc_1_B               0.152822    0.032544      4.6958    0.040036        3.8171
asc_2_A               0.000000          NA          NA          NA            NA
asc_2_B               0.000000          NA          NA          NA            NA
sigma_panel           0.033255    0.017783      1.8700    0.017329        1.9190
b_traveltime_A       -0.426653    0.048377     -8.8194    0.064117       -6.6543
b_traveltime_B       -0.106430    0.007138    -14.9107    0.010911       -9.7541
b_sd_A               -0.160649    0.044008     -3.6504    0.061948       -2.5933
b_sd_B               -0.013107         NaN         NaN    0.017223       -0.7610
b_crowded_1_A         1.649765    0.257815      6.3990    0.387942        4.2526
b_crowded_1_B         0.444514    0.041919     10.6042    0.058390        7.6128
b_crowded_2_A         1.106911    0.211548      5.2324    0.307096        3.6044
b_crowded_2_B        -0.029839    0.029885     -0.9985    0.042692       -0.6989
b_crowded_3_A        -1.154917    0.261016     -4.4247    0.371626       -3.1077
b_crowded_3_B        -0.021455    0.016458     -1.3036    0.025497       -0.8415
b_cost_A              0.375599    0.189423      1.9829    0.233160        1.6109
b_cost_B             -0.391027    0.043499     -8.9893    0.054492       -7.1759
b_transfers_A        -1.843568    0.223691     -8.2416    0.343474       -5.3674
b_transfers_B        -0.489880    0.022944    -21.3511    0.034632      -14.1453
b_transfert_A         0.003928    0.005305      0.7404    0.005391        0.7286
b_transfert_B        -0.002061         NaN         NaN    0.003988       -0.5169
b_seat_A              0.464758    0.100982      4.6024    0.150460        3.0889
b_seat_B              0.496073    0.025396     19.5335    0.031606       15.6956
b_familiar_A          1.286677    0.216573      5.9411    0.343296        3.7480
b_familiar_B          0.132044    0.025554      5.1673    0.034211        3.8597
beta_gender_male_A    0.055790    0.021957      2.5408    0.082891        0.6730
beta_gender_male_B    0.000000          NA          NA          NA            NA
beta_age_1_A          2.401713         NaN         NaN    4.924980        0.4877
beta_age_1_B          0.000000          NA          NA          NA            NA
beta_age_2_A          1.898847         NaN         NaN    4.841337        0.3922
beta_age_2_B          0.000000          NA          NA          NA            NA
beta_education_A      0.181288    0.162919      1.1127    0.231272        0.7839
beta_education_B      0.000000          NA          NA          NA            NA
beta_stay_1_A        -0.141939         NaN         NaN    0.147464       -0.9625
beta_stay_1_B         0.000000          NA          NA          NA            NA
beta_stay_2_A        -0.069246    0.019651     -3.5237    0.064956       -1.0660
beta_stay_2_B         0.000000          NA          NA          NA            NA
lambda_cur_A         -0.453875    0.082178     -5.5231    0.229308       -1.9793
lambda_cur_B          0.000000          NA          NA          NA            NA
lambda_adv_A         -0.151989    0.074901     -2.0292    0.093120       -1.6322
lambda_adv_B          0.000000          NA          NA          NA            NA
gamma1_age_1         -0.254826    0.034710     -7.3416    0.239516       -1.0639
gamma1_age_2         -0.257043    0.051912     -4.9515    0.306487       -0.8387
gamma1_gender_male    0.016480         NaN         NaN    0.020222        0.8150
gamma1_income_1      -0.216403    0.055488     -3.9000    0.099927       -2.1656
gamma1_income_2       0.086810         NaN         NaN    0.113733        0.7633
gamma2_age_1         -0.170401    0.070426     -2.4196    0.145546       -1.1708
gamma2_age_2         -0.199579    0.075466     -2.6446    0.185542       -1.0757
gamma2_gender_male    0.008968         NaN         NaN    0.013262        0.6762
gamma2_income_1      -0.220693    0.047319     -4.6640    0.054838       -4.0244
gamma2_income_2       0.123209    0.046639      2.6417    0.048885        2.5204
delta_A              -3.687571         NaN         NaN    4.945963       -0.7456
delta_B               0.000000          NA          NA          NA            NA
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: latent class with latent variable

Post by stephanehess »

Hi

good to see you're making progress. In relation to your remaining issues, I would again start building up complexity and see when the problems start appearing

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
lxiaolei
Posts: 7
Joined: 21 Sep 2023, 11:22

Re: latent class with latent variable

Post by lxiaolei »

Hi, Stephane,

Thanks for your previous reply. I want to use the socio-demographic(gender, income) and latent variable to allocate participants (class-membership model), other alternative variables are used for class-specific model utility. I tried many times, but the results always have NaN.However, when I separated the hybrid latent class choice model into hybrid choice model and latent class model, the outcomes appear to be within the expected range of results. I don't know where the issue lies in, could you please help me check it and give me some advice? Thanks very much!

Here are the code and result including latent variable and latent class, and the latent variables are used in the class allocation model.

Code: Select all

### Initialise
rm(list = ls())
library(apollo)

apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  = "1010_HLCM",
  modelDescr = "Route choice model, using ordered measurement model for indicators",
  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,
  beta_gender_male_A    = 0, beta_gender_male_B    = 0,
  beta_stay_1_A         = 0, beta_stay_1_B         = 0,
  beta_stay_2_A         = 0, beta_stay_2_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,
  lambda_cur_A          = 0, lambda_cur_B          = 0,
  lambda_adv_A          = 0, lambda_adv_B          = 0,
  gamma1_gender_male    = 0, 
  gamma2_gender_male    = 0, 
  sigma_eta1            = 0.1, sigma_eta2          = 0.2,  
  
  # Class allocation parameters
  delta_A = 0, delta_B = 0,
  # Measurement equations parameters
  zeta_cur1      = 1, zeta_cur2   = 1, zeta_cur3  = 1,
  tau_cur1_1     =-2, tau_cur1_2     =-1, tau_cur1_3     = 1, tau_cur1_4     = 2,
  tau_cur2_1     =-2, tau_cur2_2     =-1, tau_cur2_3     = 1, tau_cur2_4     = 2, 
  tau_cur3_1     =-2, tau_cur3_2     =-1, tau_cur3_3     = 1, tau_cur3_4     = 2,
  zeta_adv1      = 1, zeta_adv2   = 1, zeta_adv3  = 1, zeta_adv4  = 1,
  tau_adv1_1     =-2, tau_adv1_2     =-1, tau_adv1_3     = 1, tau_adv1_4     = 2,
  tau_adv2_1     =-2, tau_adv2_2     =-1, tau_adv2_3     = 1, tau_adv2_4     = 2, 
  tau_adv3_1     =-2, tau_adv3_2     =-1, tau_adv3_3     = 1, tau_adv3_4     = 2, 
  tau_adv4_1     =-2, tau_adv4_2     =-1, tau_adv4_3     = 1, tau_adv4_4     = 2)

### 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_income_1_B","beta_income_2_B","beta_stay_1_B","beta_stay_2_B","beta_driver_B","lambda_cur_B","lambda_adv_B")

#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton", 
  interNDraws    = 400,
  interNormDraws = c("draw_eta1","draw_eta2","draws_hsk1","draws_hsk2"))

### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
  randcoeff = list()
  randcoeff[["LV1"]] = gamma1_gender_male*gender_male + sigma_eta1*draw_eta1
  randcoeff[["LV2"]] = gamma2_gender_male*gender_male + sigma_eta2*draw_eta2
  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  )

  V = list()
  V = list(class_a = delta_A+beta_gender_male_A*gender_male+beta_income_1_A*income_1+beta_income_2_A*income_2+beta_stay_1_A*stay_1+beta_stay_2_A*stay_2+ beta_driver_A*driver+lambda_cur_A*LV1+lambda_adv_A*LV2, 
           class_b = delta_B+beta_gender_male_B*gender_male+beta_income_1_B*income_1+beta_income_2_B*income_2+beta_stay_1_B*stay_1+beta_stay_2_B*stay_2+ beta_driver_B*driver+lambda_cur_B*LV1+lambda_adv_B*LV2)
  
  ### 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()
  ### Likelihood of indicators
  ol_settings1 = list(outcomeOrdered = cur1, 
                      V              = zeta_cur1*LV1, 
                      tau            = list(tau_cur1_1, tau_cur1_2, tau_cur1_3, tau_cur1_4),
                      rows           = (task==1),
                      componentName  = "indic_cur1")
  ol_settings2 = list(outcomeOrdered = cur2, 
                      V              = zeta_cur2*LV1, 
                      tau            = list(tau_cur2_1, tau_cur2_2, tau_cur2_3, tau_cur2_4), 
                      rows           = (task==1),
                      componentName  = "indic_cur2")
  ol_settings3 = list(outcomeOrdered = cur3, 
                      V              = zeta_cur3*LV1, 
                      tau            = list(tau_cur3_1, tau_cur3_2, tau_cur3_3, tau_cur3_4), 
                      rows           = (task==1),
                      componentName  = "indic_cur3")
  ol_settings4 = list(outcomeOrdered = adv1, 
                      V              = zeta_adv1*LV2, 
                      tau            = list(tau_adv1_1, tau_adv1_2, tau_adv1_3, tau_adv1_4), 
                      rows           = (task==1),
                      componentName  = "indic_adv1")
  ol_settings5 = list(outcomeOrdered = adv2, 
                      V              = zeta_adv2*LV2, 
                      tau            = list(tau_adv2_1, tau_adv2_2, tau_adv2_3, tau_adv2_4), 
                      rows           = (task==1),
                      componentName  = "indic_adv2")
  ol_settings6 = list(outcomeOrdered = adv3, 
                      V              = zeta_adv3*LV2, 
                      tau            = list(tau_adv3_1, tau_adv3_2, tau_adv3_3, tau_adv3_4),
                      rows           = (task==1),
                      componentName  = "indic_adv3")
  ol_settings7 = list(outcomeOrdered = adv4, 
                      V              = zeta_adv4*LV2, 
                      tau            = list(tau_adv4_1, tau_adv4_2, tau_adv4_3, tau_adv4_4),
                      rows           = (task==1),
                      componentName  = "indic_adv4")
  P[["indic_cur1"]] = apollo_ol(ol_settings1, functionality)
  P[["indic_cur2"]] = apollo_ol(ol_settings2, functionality)
  P[["indic_cur3"]] = apollo_ol(ol_settings3, functionality)
  P[["indic_adv1"]] = apollo_ol(ol_settings4, functionality)
  P[["indic_adv2"]] = apollo_ol(ol_settings5, functionality)
  P[["indic_adv3"]] = apollo_ol(ol_settings6, functionality)
  P[["indic_adv4"]] = apollo_ol(ol_settings7, functionality)
  
  ### Combined model
  P = apollo_combineModels(P, apollo_inputs, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Rename model
  names(P)[which(names(P)=="model")] <- "Measurement_model"
  
  ### Likelihood of choices inside each class
  P_within<-list()
  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 + hsk1[[s]]
    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 + hsk2[[s]]
    ### Define settings for MNL model component
    mnl_settings = list(
      alternatives = c(alt1=1, alt2=2),
      avail       = list(alt1 = 1, alt2 = 1),
      choiceVar    = best,
      utilities    = V
    )
    ### mnl_settings$componentName = paste0("Class_",s)
    ### Compute within-class choice probabilities using MNL model
    P_within[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    P_within[[paste0("Class_",s)]] = apollo_panelProd(P_within[[paste0("Class_",s)]], apollo_inputs ,functionality)
  }
  
  ### Compute latent class model probabilities
  lc_settings  = list(inClassProb = P_within, classProb=pi_values)
  P[["LC_model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  ### Comment out as necessary
  P = apollo_combineModels(P, apollo_inputs, functionality)
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### CALCULATE LL AT STARTING VALUES                             ####
# ################################################################# #

apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #
estimate_settings = list(hessianRoutine = "maxLik",
                         estimationRoutine = "bfgs",
                         maxIterations = 300,
                         printLevel = 3)
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)
the results:

Code: Select all

Estimates:
                      Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
asc_1_A               0.450356    0.079845      5.6404     0.10534       4.27544
asc_1_B               0.009587    0.004388      2.1848     0.05483       0.17487
asc_2_A               0.000000          NA          NA          NA            NA
asc_2_B               0.000000          NA          NA          NA            NA
sigma_panel           0.031397    0.010129      3.0997     0.13044       0.24070
b_traveltime_A       -0.019016    0.038479     -0.4942     0.19765      -0.09621
b_traveltime_B       -0.174174    0.008043    -21.6556     0.01208     -14.42031
b_sd_A                0.007551    0.006411      1.1779     0.03722       0.20287
b_sd_B               -0.053565    0.012229     -4.3803     0.03783      -1.41597
b_crowded_1_A         0.252669    0.312225      0.8093     1.55884       0.16209
b_crowded_1_B         1.375700    0.051115     26.9138     0.07663      17.95150
b_crowded_2_A         0.028570         NaN         NaN     0.01166       2.45029
b_crowded_2_B         0.805456    0.037267     21.6132     0.04417      18.23702
b_crowded_3_A        -0.097130         NaN         NaN     0.43216      -0.22476
b_crowded_3_B         0.557981    0.060341      9.2472     0.07640       7.30324
b_cost_A             -0.109097    0.016601     -6.5717     0.07649      -1.42636
b_cost_B             -0.337636    0.045490     -7.4222     0.07754      -4.35452
b_transfers_A         0.053449    0.019718      2.7106     0.18814       0.28409
b_transfers_B        -0.795962    0.093266     -8.5343     0.50709      -1.56966
b_transfert_A         0.029802    0.011989      2.4857     0.06295       0.47340
b_transfert_B        -0.013574    0.018836     -0.7206     0.11379      -0.11928
b_seat_A              0.501176    0.187700      2.6701     0.80323       0.62395
b_seat_B              1.107124    0.057227     19.3461     0.11520       9.61018
b_familiar_A          0.238207    0.129413      1.8407     0.16902       1.40932
b_familiar_B          0.578881    0.058580      9.8820     0.10730       5.39491
beta_gender_male_A    0.007106    0.013796      0.5151     0.09627       0.07382
beta_gender_male_B    0.000000          NA          NA          NA            NA
beta_stay_1_A        -0.049125    0.013710     -3.5831     0.05127      -0.95807
beta_stay_1_B         0.000000          NA          NA          NA            NA
beta_stay_2_A         0.422734    0.410179      1.0306     2.21932       0.19048
beta_stay_2_B         0.000000          NA          NA          NA            NA
beta_driver_A         0.363780    0.479440      0.7588     2.09683       0.17349
beta_driver_B         0.000000          NA          NA          NA            NA
beta_income_1_A       0.578216    0.828942      0.6975     4.12313       0.14024
beta_income_1_B       0.000000          NA          NA          NA            NA
beta_income_2_A       0.533638    0.894951      0.5963     4.91440       0.10859
beta_income_2_B       0.000000          NA          NA          NA            NA
lambda_cur_A          0.001963         NaN         NaN     0.03609       0.05440
lambda_cur_B          0.000000          NA          NA          NA            NA
lambda_adv_A          0.113119    0.023871      4.7388     0.02723       4.15360
lambda_adv_B          0.000000          NA          NA          NA            NA
gamma1_gender_male    0.108270         NaN         NaN     0.16295       0.66445
gamma2_gender_male    0.402964         NaN         NaN     2.37659       0.16956
sigma_eta1            1.851948         NaN         NaN     1.12094       1.65214
sigma_eta2            7.030651    0.471458     14.9126     1.02310       6.87192
delta_A              -3.047936         NaN         NaN     1.32377      -2.30246
delta_B               0.000000          NA          NA          NA            NA
zeta_cur1             1.561511         NaN         NaN     0.95124       1.64156
zeta_cur2             1.065316         NaN         NaN     0.66222       1.60870
zeta_cur3             0.978557         NaN         NaN     0.59063       1.65681
tau_cur1_1           -8.307181    0.698074    -11.9001     0.91369      -9.09190
tau_cur1_2           -5.074778    0.429008    -11.8291     0.51776      -9.80142
tau_cur1_3           -1.678765    0.184679     -9.0902     0.32533      -5.16022
tau_cur1_4            1.804143    0.184197      9.7946     0.21545       8.37390
tau_cur2_1           -5.892660    0.360897    -16.3278     0.37106     -15.88045
tau_cur2_2           -3.680414    0.224061    -16.4260     0.23745     -15.49972
tau_cur2_3           -1.398786    0.124913    -11.1980     0.20343      -6.87586
tau_cur2_4            1.200887    0.111291     10.7905     0.14457       8.30676
tau_cur3_1           -5.461469    0.281731    -19.3854     0.32317     -16.89958
tau_cur3_2           -3.402982    0.180419    -18.8616     0.19166     -17.75568
tau_cur3_3           -0.970248    0.102542     -9.4620     0.12438      -7.80096
tau_cur3_4            2.153664    0.132496     16.2546     0.17965      11.98783
zeta_adv1             0.149083    0.013045     11.4286     0.02465       6.04778
zeta_adv2             0.177173    0.017159     10.3256     0.03930       4.50806
zeta_adv3             0.623694    0.076302      8.1740     0.09775       6.38068
zeta_adv4             0.665103    0.087692      7.5845     0.19257       3.45383
tau_adv1_1           -3.533599    0.166359    -21.2407     0.20155     -17.53199
tau_adv1_2           -1.239817    0.083139    -14.9126     0.17833      -6.95228
tau_adv1_3            0.572214    0.071928      7.9553     0.08980       6.37241
tau_adv1_4            2.622328    0.117915     22.2392     0.13244      19.80023
tau_adv2_1           -3.686582    0.171859    -21.4512     0.19714     -18.70042
tau_adv2_2           -1.899313    0.098941    -19.1965     0.13172     -14.41907
tau_adv2_3           -0.169378    0.074907     -2.2612     0.12254      -1.38219
tau_adv2_4            1.977006    0.095900     20.6153     0.13236      14.93633
tau_adv3_1           -5.508705    0.477846    -11.5282     0.78087      -7.05461
tau_adv3_2           -1.726083    0.190504     -9.0606     0.37260      -4.63249
tau_adv3_3            2.576807    0.275536      9.3520     0.50746       5.07786
tau_adv3_4            5.766007    0.524764     10.9878     0.88613       6.50698
tau_adv4_1           -3.747895    0.382651     -9.7945     0.69124      -5.42202
tau_adv4_2           -0.510148    0.135126     -3.7753     0.26262      -1.94250
tau_adv4_3            3.390017    0.341084      9.9389     0.53724       6.31012
tau_adv4_4            6.447031    0.622602     10.3550     0.94777       6.80228
Here is the result with latent class but not latent variable:

Code: Select all

Estimates:
                      Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
asc_1_A              -0.556214    0.389141    -1.42934     3.39683     -0.163745
asc_1_B               0.140659    0.036089     3.89758     0.13473      1.044025
asc_2_A               0.000000          NA          NA          NA            NA
asc_2_B               0.000000          NA          NA          NA            NA
sigma_panel           0.002441    0.005467     0.44654     0.05572      0.043813
b_traveltime_A       -0.486793    0.215102    -2.26308     1.94362     -0.250456
b_traveltime_B       -0.109029    0.014753    -7.39052     0.12387     -0.880209
b_sd_A               -0.170300    0.117659    -1.44740     1.01037     -0.168553
b_sd_B               -0.015973    0.019894    -0.80294     0.16002     -0.099818
b_crowded_1_A         3.495489    1.749432     1.99807    15.74685      0.221980
b_crowded_1_B         0.872448    0.105124     8.29925     0.73660      1.184426
b_crowded_2_A         3.076781    1.608046     1.91337    14.54765      0.211497
b_crowded_2_B         0.380832    0.106393     3.57950     0.79401      0.479629
b_crowded_3_A         0.165296    0.157178     1.05165     0.27171      0.608344
b_crowded_3_B         0.380384    0.069601     5.46524     0.15208      2.501241
b_cost_A              0.431707    0.557121     0.77489     4.93146      0.087541
b_cost_B             -0.372131    0.069571    -5.34896     0.49355     -0.753984
b_transfers_A        -2.242189    1.529424    -1.46604    14.07129     -0.159345
b_transfers_B        -0.494284    0.211141    -2.34101     1.93470     -0.255483
b_transfert_A         0.037138    0.181699     0.20439     1.54549      0.024030
b_transfert_B        -0.003562    0.083133    -0.04284     0.77384     -0.004602
b_seat_A              1.055423    0.238347     4.42810     0.60503      1.744408
b_seat_B              0.983712    0.053187    18.49528     0.18483      5.322209
b_familiar_A          3.055793    1.725319     1.77115    15.62585      0.195560
b_familiar_B          0.279556    0.112063     2.49462     0.94380      0.296201
beta_gender_male_A    0.078400    0.088535     0.88552     0.36643      0.213956
beta_gender_male_B    0.000000          NA          NA          NA            NA
beta_stay_1_A        -0.182526    0.154903    -1.17833     0.37456     -0.487302
beta_stay_1_B         0.000000          NA          NA          NA            NA
beta_stay_2_A        -0.107116    0.252414    -0.42437     2.39500     -0.044725
beta_stay_2_B         0.000000          NA          NA          NA            NA
beta_driver_A         0.024347    0.354909     0.06860     3.30890      0.007358
beta_driver_B         0.000000          NA          NA          NA            NA
beta_income_1_A       0.286251    0.159442     1.79533     1.54895      0.184803
beta_income_1_B       0.000000          NA          NA          NA            NA
beta_income_2_A       0.158109    0.283726     0.55726     2.89391      0.054635
beta_income_2_B       0.000000          NA          NA          NA            NA
delta_A              -1.340603    0.999300    -1.34154     9.26890     -0.144635
delta_B               0.000000          NA          NA          NA            NA
Here is the result with latent variable but not latent class,

Code: Select all

Estimates:
                      Estimate        s.e.   t.rat.(0)    Rob.s.e. Rob.t.rat.(0)
asc_1                 0.053506    0.187466      0.2854     2.33525       0.02291
asc_2                 0.000000          NA          NA          NA            NA
sigma_panel          -0.008890    0.013333     -0.6668     0.13661      -0.06508
b_traveltime         -0.133072    0.005602    -23.7533     0.02728      -4.87724
b_sd                 -0.037002    0.010186     -3.6325     0.06453      -0.57340
b_crowded_1           1.080535    0.059797     18.0699     0.08679      12.44938
b_crowded_2           0.606090    0.061467      9.8605     0.33020       1.83551
b_crowded_3           0.414632    0.065884      6.2933     0.36515       1.13552
b_cost               -0.276833    0.038289     -7.2300     0.10448      -2.64954
b_transfers          -0.581467    0.143411     -4.0545     1.74751      -0.33274
b_transfert          -0.014646    0.045917     -0.3190     0.56681      -0.02584
b_seat                0.901345    0.041496     21.7215     0.12083       7.45947
b_familiar            0.471922    0.041906     11.2614     0.05740       8.22111
gamma1_age_1         -0.825616    0.537750     -1.5353     6.73646      -0.12256
gamma1_age_2         -0.733477    0.540279     -1.3576     6.69547      -0.10955
gamma1_gender_male    0.050582    0.398186      0.1270     4.97627       0.01016
gamma2_age_1         -1.129610    0.603858     -1.8707     7.33652      -0.15397
gamma2_age_2         -1.075429    1.589204     -0.6767    19.67833      -0.05465
gamma2_gender_male    0.165988    0.567398      0.2925     7.31791       0.02268
sigma_eta1            1.470971    1.921874      0.7654    24.54500       0.05993
sigma_eta2            4.080800    8.751459      0.4663   109.45574       0.03728
zeta_cur1             1.923888    2.624559      0.7330    33.41997       0.05757
zeta_cur2             1.374058    1.714125      0.8016    21.86603       0.06284
zeta_cur3             1.246925    1.648620      0.7563    21.03453       0.05928
tau_cur1_1           -9.596356    1.823251     -5.2633    20.98305      -0.45734
tau_cur1_2           -6.527474    1.623625     -4.0203    19.39278      -0.33659
tau_cur1_3           -3.214168    1.498766     -2.1445    18.58438      -0.17295
tau_cur1_4            0.202598    1.449108      0.1398    18.27260       0.01109
tau_cur2_1           -7.010613    0.907308     -7.7268    10.04917      -0.69763
tau_cur2_2           -4.821674    0.783720     -6.1523     9.11071      -0.52923
tau_cur2_3           -2.528798    0.785156     -3.2208     9.61872      -0.26290
tau_cur2_4            0.096560    0.659601      0.1464     8.23228       0.01173
tau_cur3_1           -6.451503    0.965181     -6.6842    11.38621      -0.56661
tau_cur3_2           -4.417200    0.926073     -4.7698    11.29000      -0.39125
tau_cur3_3           -1.986884    0.873092     -2.2757    10.86553      -0.18286
tau_cur3_4            1.144994    0.820408      1.3956    10.27290       0.11146
zeta_adv1             0.256730    0.562077      0.4568     7.02884       0.03653
zeta_adv2             0.308060    0.670959      0.4591     8.39288       0.03670
zeta_adv3             1.130181    2.660630      0.4248    33.26969       0.03397
zeta_adv4             1.089988    2.090981      0.5213    26.10102       0.04176
tau_adv1_1           -3.822548    0.866083     -4.4136    10.60609      -0.36041
tau_adv1_2           -1.529687    0.821840     -1.8613    10.20846      -0.14985
tau_adv1_3            0.280374    0.828788      0.3383    10.33143       0.02714
tau_adv1_4            2.328203    0.738343      3.1533     9.11574       0.25540
tau_adv2_1           -4.043655    0.968171     -4.1766    11.88751      -0.34016
tau_adv2_2           -2.251782    0.871139     -2.5849    10.78110      -0.20886
tau_adv2_3           -0.517622    0.808023     -0.6406    10.02823      -0.05162
tau_adv2_4            1.633567    0.808874      2.0196    10.00559       0.16327
tau_adv3_1           -7.034785    4.573550     -1.5381    56.74191      -0.12398
tau_adv3_2           -3.084494    3.808042     -0.8100    47.45023      -0.06500
tau_adv3_3            1.402804    3.033657      0.4624    37.68638       0.03722
tau_adv3_4            4.722445    2.375052      1.9884    28.85035       0.16369
tau_adv4_1           -4.818131    2.126652     -2.2656    25.65928      -0.18777
tau_adv4_2           -1.724725    2.595055     -0.6646    32.10765      -0.05372
tau_adv4_3            2.002276    3.463584      0.5781    43.17695       0.04637
tau_adv4_4            4.911714    3.971670      1.2367    49.43354       0.09936
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: latent class with latent variable

Post by stephanehess »

Hi

your model is overspecified. You cannot estimate sigma_eta1 and sigma_eta2 unless you fix one of the zetas to 1 for each indicator. So fix both sigmas to 1 as discussed in the literature.

Also, your sigma_panel term is clearly not working. This is partly as you are referring to hsk1[[1]] as if it was distributed across classes when it's not. But I would drop it anyway as you otherwise need integration both inside and outside classes

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
lxiaolei
Posts: 7
Joined: 21 Sep 2023, 11:22

Re: latent class with latent variable

Post by lxiaolei »

Hi, Stephane,

I revised the code according to your suggestion, the problem was solved! Thanks very much for your help!

Best wishes,
Xiaolei
Post Reply