Error in Latent Class Model with LV
Posted: 13 Aug 2021, 13:50
Hi,
I am trying to estimate a latent class model where a latent variable explains the class allocation. I don't know if there are any errors in my code, but after the estimation result a lot of Nas appear. It that, in the output file NAs appear between "Estimates" and "Summary of class allocation for LC model component choice". And them between "Summary of class allocation for LC model component choice" and "Classical covariance matrix".
I attach my code and the result of the estimation. I really appreciate the help.
I am trying to estimate a latent class model where a latent variable explains the class allocation. I don't know if there are any errors in my code, but after the estimation result a lot of Nas appear. It that, in the output file NAs appear between "Estimates" and "Summary of class allocation for LC model component choice". And them between "Summary of class allocation for LC model component choice" and "Classical covariance matrix".
I attach my code and the result of the estimation. I really appreciate the help.
Code: Select all
### Clear memory
rm(list = ls())
##-------------------------------------------------------------------------------------##
## LOADING PACKAGES ##
require(apollo)
library(apollo)
##-------------------------------------------------------------------------------------##
### INITIALICE CODE
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="model",
modelDescr ="latent variable in latent class",
indivID ="ID_Global",
mixing = TRUE,
analyticGrad = FALSE,
nCores = 4
)
#### DEFINE MODEL PARAMETERS
apollo_beta=c(
# Choice class params
c1_asc_Alt1 = 3,
c1_asc_Alt2 = 2,
c1_asc_Alt4 = 0,
c1_asc_Alt3 = 0,
c2_asc_Alt3 = 3,
c2_asc_Alt1 = 2,
c2_asc_Alt4 = 1,
c2_asc_Alt2 = 0,
c1_b_distancia_B = -0.3,
c2_b_distancia_B = -0.2,
c1_b_costo_B = -0.3,
c2_b_costo_B = -0.2,
c1_b_busqueda_B = 0,
c2_b_busqueda_B = 0,
c1_b_descarga_B1 = 0.26,
c2_b_descarga_B1 = 0,
c1_b_costo_S = 0,
c2_b_costo_S = 0,
c1_b_realizadas_NP = 0,
c2_b_realizadas_NP = 0,
c1_b_ventana_S = 0,
c2_b_ventana_S = 0,
# Class allocation model params
c1_delta=0,
c2_delta=0.5,
delta_COND = 0,
# LV Structural equation params
# LV measurement model params
zeta_EstProh_COND = 0.106,
tau_EstProh_1 = -3,
tau_EstProh_2 = -2,
tau_EstProh_3 = -1,
tau_EstProh_4 = 0,
zeta_EstSeg_COND = 0.166,
tau_EstSeg_1 = -3,
tau_EstSeg_2 = -2,
tau_EstSeg_3 = -1,
tau_EstSeg_4 = 0,
zeta_CauCong_COND = 0.748,
tau_CauCong_1 = -5,
tau_CauCong_2 = 0.05,
tau_CauCong_3 = 1,
tau_CauCong_4 = 7,
zeta_DescVia_COND = 0.462,
tau_DescVia_1 = -4,
tau_DescVia_2 = 0,
tau_DescVia_3 = 0.35,
tau_DescVia_4 = 4,
zeta_EstAnd_COND = 0.368,
tau_EstAnd_1 = -3,
tau_EstAnd_2 = 0.08,
tau_EstAnd_3 = 0.15,
tau_EstAnd_4 = 3
)
apollo_fixed = c("c1_asc_Alt3", "c1_delta", "c2_asc_Alt2", "c2_b_descarga_B1", "c1_b_costo_S", "c1_b_ventana_S")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
apollo_draws = list(
interDrawsType="mlhs",
interNDraws=1000,
interUnifDraws=c(),
interNormDraws=c("eta1"),
intraDrawsType="mlhs",
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["COND"]] = eta1
return(randcoeff)
}
################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_asc_Alt4"]]=list(c1_asc_Alt4,c2_asc_Alt4)
lcpars[["b_asc_Alt1"]]=list(c1_asc_Alt1,c2_asc_Alt1)
lcpars[["b_asc_Alt2"]]=list(c1_asc_Alt2,c2_asc_Alt2)
lcpars[["b_asc_Alt3"]]=list(c1_asc_Alt3,c2_asc_Alt3)
lcpars[["b_distancia_B"]]=list(c1_b_distancia_B,c2_b_distancia_B)
lcpars[["b_costo_B"]]=list(c1_b_costo_B,c2_b_costo_B)
lcpars[["b_busqueda_B"]]=list(c1_b_busqueda_B,c2_b_busqueda_B)
lcpars[["b_descarga_B1"]]=list(c1_b_descarga_B1,c2_b_descarga_B1)
lcpars[["b_costo_S"]]=list(c1_b_costo_S,c2_b_costo_S)
lcpars[["b_realizadas_NP"]]=list(c1_b_realizadas_NP,c2_b_realizadas_NP)
lcpars[["b_ventana_S"]]=list(c1_b_ventana_S,c2_b_ventana_S)
V=list()
V[["class_1"]] = c1_delta
V[["class_2"]] = c2_delta + delta_COND * COND
Cla = list(
alternatives = c(class_1=1, class_2=2),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_mnl(Cla, 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))
P = list()
PClass = list()
### Likelihood of indicators
{ ol_settings1 = list(outcomeOrdered=Estac_Prohibido,
V=zeta_EstProh_COND*COND,
tau=list(tau_EstProh_1, tau_EstProh_2, tau_EstProh_3, tau_EstProh_4),
coding = list(1,2,3,4,5),
rows=(Escenario==1),
componentName = "indic_Estac_Prohibido")}
{ ol_settings2 = list(outcomeOrdered=Estac_Seguro,
V=zeta_EstSeg_COND*COND,
tau=list(tau_EstSeg_1, tau_EstSeg_2, tau_EstSeg_3, tau_EstSeg_4),
coding = list(1,2,3,4,5),
rows=(Escenario==1),
componentName = "indic_Estac_Seguro")}
{ ol_settings3 = list(outcomeOrdered=Causa_Congestion,
V=zeta_CauCong_COND*COND,
tau=list(tau_CauCong_1, tau_CauCong_2, tau_CauCong_3, tau_CauCong_4),
coding = list(1,2,3,4,5),
rows=(Escenario==1),
componentName = "indic_Causa_Congestion")}
{ ol_settings4 = list(outcomeOrdered=Descarga_EnVia,
V=zeta_DescVia_COND*COND,
tau=list(tau_DescVia_1, tau_DescVia_2, tau_DescVia_3, tau_DescVia_4),
coding = list(1,2,3,4,5),
rows=(Escenario==1),
componentName = "indic_Descarga_EnVia")}
{ ol_settings5 = list(outcomeOrdered=Estac_Anden,
V=zeta_EstAnd_COND*COND,
tau=list(tau_EstAnd_1, tau_EstAnd_2, tau_EstAnd_3, tau_EstAnd_4),
coding = list(1,2,3,4,5),
rows=(Escenario==1),
componentName = "indic_Estac_Anden")}
### Keep only the first row, as it is the only one that matters
{
P[["indic_Estac_Prohibido"]] = apollo_firstRow(apollo_ol(ol_settings1, functionality), apollo_inputs )
P[["indic_Estac_Seguro"]] = apollo_firstRow(apollo_ol(ol_settings2, functionality), apollo_inputs )
P[["indic_Causa_Congestion"]] = apollo_firstRow(apollo_ol(ol_settings3, functionality), apollo_inputs )
P[["indic_Descarga_EnVia"]] = apollo_firstRow(apollo_ol(ol_settings4, functionality), apollo_inputs )
P[["indic_Estac_Anden"]] = apollo_firstRow(apollo_ol(ol_settings5, functionality), apollo_inputs )
}
for(s in 1:2){
Transf_Tdescarga = log(T_Descarga)
asc_Alt4 = b_asc_Alt4[[s]]
asc_Alt1 = b_asc_Alt1[[s]]
asc_Alt2 = b_asc_Alt2[[s]]
asc_Alt3 = b_asc_Alt3[[s]]
Beta_Distancia = b_distancia_B[[s]]
Beta_Costo = b_costo_B[[s]]
Beta_Busqueda = b_busqueda_B[[s]]
Beta_Descarga = b_descarga_B1[[s]]
Beta_CostoMulta = b_costo_S[[s]]
Beta_Ventana = b_ventana_S[[s]]
Beta_Realizadas = b_realizadas_NP[[s]]
V = list()
V[['Alt1']] = asc_Alt1 + Beta_Distancia * Distancia1 + Beta_Costo * Costo1 + Beta_Busqueda * T_busquedaSP + Beta_Descarga * Transf_Tdescarga
V[['Alt2']] = asc_Alt2 + Beta_Distancia * Distancia2 + Beta_Costo * Costo2 + Beta_Busqueda * T_busquedaSP
V[['Alt3']] = asc_Alt3 + Beta_CostoMulta * Costo_multa + Beta_Ventana * T_ventana
V[['Alt4']] = asc_Alt4 + Beta_Realizadas * N_Realizadas
Cho = list(
alternatives = c(Alt1=1, Alt2=2, Alt3=3, Alt4=4),
avail = 1,
choiceVar = Choice,
V = V
)
PClass[[paste0("Class_",s)]] = apollo_mnl(Cho, functionality)
PClass[[paste0("Class_",s)]] = apollo_panelProd(PClass[[paste0("Class_",s)]], apollo_inputs ,functionality)
}
lc_settings = list(inClassProb=PClass, classProb=pi_values)
P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
P = apollo_combineModels(P, apollo_inputs, functionality)
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION AND OUTPUT ####
# ################################################################# #
s = list(writeIter=FALSE, hessianRoutine="maxLik")
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=s)
apollo_modelOutput(model)
Code: Select all
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 232
Number of rows in database : 1392
Number of modelled outcomes : 0
Number of cores used : 4
Number of inter-individual draws : 1000 (mlhs)
LL(start) : -3000.255
LL(0, whole model) : Not applicable
LL(final, whole model) : -2213.946
Rho-square (0) : Not applicable
Adj.Rho-square (0) : Not applicable
AIC : 4515.89
BIC : -Inf
LL(0,indic_Estac_Prohibido) : Not applicable
LL(final,indic_Estac_Prohibido) : -187.3616
LL(0,indic_Estac_Seguro) : Not applicable
LL(final,indic_Estac_Seguro) : -148.5431
LL(0,indic_Causa_Congestion) : Not applicable
LL(final,indic_Causa_Congestion) : -241.5213
LL(0,indic_Descarga_EnVia) : Not applicable
LL(final,indic_Descarga_EnVia) : -247.3904
LL(0,indic_Estac_Anden) : Not applicable
LL(final,indic_Estac_Anden) : -258.7416
LL(0,choice) : -1929.722
LL(final,choice) : -1197.325
Estimated parameters : 44
Time taken (hh:mm:ss) : 00:36:19.61
pre-estimation : 00:00:15.86
estimation : 00:17:54.9
post-estimation : 00:18:8.85
Iterations : 67
Min abs eigenvalue of Hessian : 0.158839
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)
c1_asc_Alt1 3.89456 0.761095 5.117 0.782929 4.974
c1_asc_Alt2 3.09162 0.718145 4.305 0.648786 4.765
c1_asc_Alt4 0.78612 0.497974 1.579 0.606661 1.296
c1_asc_Alt3 0.00000 NA NA NA NA
c2_asc_Alt3 3.09179 0.582839 5.305 0.468083 6.605
c2_asc_Alt1 1.15339 0.429636 2.685 0.493792 2.336
c2_asc_Alt4 1.17678 0.561634 2.095 0.461613 2.549
c2_asc_Alt2 0.00000 NA NA NA NA
c1_b_distancia_B -0.32401 0.186774 -1.735 0.154648 -2.095
c2_b_distancia_B -0.33330 0.184432 -1.807 0.138277 -2.410
c1_b_costo_B -2.91558 1.323555 -2.203 1.274323 -2.288
c2_b_costo_B -3.74226 1.805568 -2.073 1.763677 -2.122
c1_b_busqueda_B 0.03850 0.016340 2.356 0.015673 2.456
c2_b_busqueda_B 0.02492 0.008834 2.821 0.008386 2.972
c1_b_descarga_B1 0.27283 0.106815 2.554 0.207931 1.312
c2_b_descarga_B1 0.00000 NA NA NA NA
c1_b_costo_S 0.00000 NA NA NA NA
c2_b_costo_S -0.02487 0.011074 -2.245 0.007063 -3.521
c1_b_realizadas_NP 0.03792 0.011426 3.318 0.010901 3.478
c2_b_realizadas_NP 0.02670 0.008881 3.007 0.014045 1.901
c1_b_ventana_S 0.00000 NA NA NA NA
c2_b_ventana_S -0.01660 0.005187 -3.200 0.003112 -5.334
c1_delta 0.00000 NA NA NA NA
c2_delta -0.58745 0.162773 -3.609 0.172236 -3.411
delta_COND -0.89267 0.231729 -3.852 0.307975 -2.899
zeta_EstProh_COND 0.56500 0.202217 2.794 0.262163 2.155
tau_EstProh_1 -4.90195 0.721978 -6.790 0.771544 -6.353
tau_EstProh_2 -2.20425 0.230855 -9.548 0.260521 -8.461
tau_EstProh_3 -2.07603 0.221241 -9.384 0.240758 -8.623
tau_EstProh_4 2.05501 0.217787 9.436 0.224467 9.155
zeta_EstSeg_COND 1.01989 0.243435 4.190 0.333000 3.063
tau_EstSeg_1 -5.27864 0.755264 -6.989 0.886779 -5.953
tau_EstSeg_2 -3.70392 0.414851 -8.928 0.452692 -8.182
tau_EstSeg_3 -3.27384 0.361028 -9.068 0.361102 -9.066
tau_EstSeg_4 2.30246 0.260666 8.833 0.310463 7.416
zeta_CauCong_COND 3.18670 0.968029 3.292 1.131795 2.816
tau_CauCong_1 -7.79369 1.866200 -4.176 2.133603 -3.653
tau_CauCong_2 -2.85794 0.724525 -3.945 0.839872 -3.403
tau_CauCong_3 -2.27036 0.609080 -3.728 0.692008 -3.281
tau_CauCong_4 4.86430 1.193871 4.074 1.298885 3.745
zeta_DescVia_COND 1.48486 0.263147 5.643 0.337978 4.393
tau_DescVia_1 -5.74120 0.805374 -7.129 0.702537 -8.172
tau_DescVia_2 -1.29085 0.214847 -6.008 0.228039 -5.661
tau_DescVia_3 -0.96402 0.202328 -4.765 0.207673 -4.642
tau_DescVia_4 3.19690 0.356553 8.966 0.420823 7.597
zeta_EstAnd_COND 1.02294 0.205432 4.979 0.247644 4.131
tau_EstAnd_1 -3.75682 0.402556 -9.332 0.374720 -10.026
tau_EstAnd_2 -0.87257 0.167421 -5.212 0.167208 -5.218
tau_EstAnd_3 -0.80342 0.165497 -4.855 0.163333 -4.919
tau_EstAnd_4 2.35513 0.250420 9.405 0.275018 8.564
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
...
Summary of class allocation for LC model component choice:
Mean prob.
Class_1 0.6230
Class_2 0.3770
NA
NA
NA
NA
NA
NA
NA
NA
...
Classical covariance matrix: