Error when estimating a ICLV model with two sources of information
Posted: 28 Jan 2021, 16:26
Dear Stephane and David,
Thank you for developing the Apollo package, and thank you in advance for taking the time to read this issue.
I'm trying to run a model with one (for the moment) LV. In this model, I have two sources of information where one is much bigger than the other (eodh = 21481 obs, and enc = 231 obs), and the LV is only applicable to the enc observations. At first, I tried running the code without estimating a factor scale, ignoring the fact that I have two different sources, and my model was successful. However, I want to have a scale factor for this two sources, so I review the Apollo_example_22.r file to create my code, and the Apollo_example_24.r to include the LV, but I get the following error when using apollo_estimate, which I haven't been able to debug or understand:
Testing likelihood functionError in checkForRemoteErrors(lapply(cl, recvResult)) :
5 nodes produced errors; first error: (subscript) logical subscript too long
My code is this:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "BE_LV_V1",
modelDescr = "Modelo estratégico con variables latentes",
indivID = "id_persona",
mixing = TRUE,
nCores = 6
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
setwd("~/Desktop/estrategico_LV")
database = read.csv("bd_conjunta_V1.csv", header = TRUE, dec = ".")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_cero = 0,
asc_moto = 0,
b_inc2_1 = 0,
b_inc3_1 = 0,
b_inc4_1 = 0,
b_inc5_1 = 0,
b_inc6_1 = 0,
b_inc7_1 = 0,
b_inc8_1 = 0,
b_inc9_1 = 0,
b_estr2_1 = 0,
b_estr3_1 = 0,
b_estr4_1 = 0,
b_acthom1_1 = 0,
b_acthom2_1 = 0,
b_acthom3m_1 = 0,
b_com500_1 = 0,
b_Dens_500_1 = 0,
b_Est_TM_1 = 0,
b_Est_SITP_1 = 0,
b_periferia_1 = 0,
asc_carro = 0,
b_inc2_2 = 0,
b_inc3_2 = 0,
b_inc4_2 = 0,
b_inc5_2 = 0,
b_inc6_2 = 0,
b_inc7_2 = 0,
b_inc8_2 = 0,
b_inc9_2 = 0,
b_estr2_2 = 0,
b_estr3_2 = 0,
b_estr4_2 = 0,
b_acthom1_2 = 0,
b_acthom2_2 = 0,
b_acthom3m_2 = 0,
b_com500_2 = 0,
b_Dens_500_2 = 0,
b_Est_TM_2 = 0,
b_Est_SITP_2 = 0,
b_periferia_2 = 0,
asc_ambos = 0,
b_inc2_3 = 0,
b_inc3_3 = 0,
b_inc4_3 = 0,
b_inc5_3 = 0,
b_inc6_3 = 0,
b_inc7_3 = 0,
b_inc8_3 = 0,
b_inc9_3 = 0,
b_estr2_3 = 0,
b_estr3_3 = 0,
b_estr4_3 = 0,
b_acthom1_3 = 0,
b_acthom2_3 = 0,
b_acthom3m_3 = 0,
b_com500_3 = 0,
b_Dens_500_3 = 0,
b_Est_TM_3 = 0,
b_Est_SITP_3 = 0,
b_periferia_3 = 0,
#Funcionalidad auto
lambda_FuncionalidadAuto = 1,
gamma_edad_FuncionalidadAuto = 0,
#gamma_hombre_FuncionalidadAuto = 0,
#gamma_claseBaja_FuncionalidadAuto = 0,
#gamma_claseMedia_FuncionalidadAuto = 0,
#gamma_numPersonas_FuncionalidadAuto = 0,
gamma_numAuto_FuncionalidadAuto = 0,
#gamma_numMoto_FuncionalidadAuto = 0,
gamma_IngDef_FuncionalidadAuto = 0,
#gamma_numVeh_FuncionalidadAuto = 0,
zeta_disp = 1, #p1
zeta_comodo = 1, #p5
zeta_rapido = 1, #p9
zeta_libertad = 1, #p13
tau_disp_1 = -2,
tau_disp_2 = -1,
tau_disp_3 = 1,
tau_disp_4 = 2,
tau_comodo_1 = -2,
tau_comodo_2 = -1,
tau_comodo_3 = 1,
tau_comodo_4 = 2,
tau_rapido_1 = -2,
tau_rapido_2 = -1,
tau_rapido_3 = 1,
tau_rapido_4 = 2,
tau_libertad_1 = -2,
tau_libertad_2 = -1,
tau_libertad_3 = 1,
tau_libertad_4 = 2,
mu_eodh = 1,
mu_enc = 1)
### 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_cero", "mu_eodh")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interUnifDraws = c(),
interNormDraws = c("eta_FuncionalidadAuto"),
intraDrawsType = '',
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["FuncionalidadAuto"]] = gamma_edad_FuncionalidadAuto*EDAD +
gamma_numAuto_FuncionalidadAuto*cantidad_vehiculos +
gamma_IngDef_FuncionalidadAuto*IngDef +
eta_FuncionalidadAuto
return(randcoeff)
}
# ################################################################# #
#### 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()
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = p1,
V = zeta_disp * FuncionalidadAuto,
tau = c(tau_disp_1, tau_disp_2, tau_disp_3, tau_disp_4),
rows = (enc==1),
componentName = "indic_disp")
ol_settings2 = list(outcomeOrdered = p5,
V = zeta_comodo * FuncionalidadAuto,
tau = c(tau_comodo_1, tau_comodo_2, tau_comodo_3, tau_comodo_4),
rows = (enc==1),
componentName = "indic_comodo")
ol_settings3 = list(outcomeOrdered = p9,
V = zeta_rapido * FuncionalidadAuto,
tau = c(tau_rapido_1, tau_rapido_2, tau_rapido_3, tau_rapido_4),
rows = (enc==1),
componentName = "indic_rapido")
ol_settings4 = list(outcomeOrdered = p13,
V = zeta_libertad * FuncionalidadAuto,
tau = c(tau_libertad_1, tau_libertad_2, tau_libertad_3, tau_libertad_4),
rows = (enc==1),
componentName = "indic_libertad")
P[["indic_disp"]] = apollo_ol(ol_settings1, functionality)
P[["indic_comodo"]] = apollo_ol(ol_settings2, functionality)
P[["indic_rapido"]] = apollo_ol(ol_settings3, functionality)
P[["indic_libertad"]] = apollo_ol(ol_settings4, functionality)
### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['cero']] = asc_cero
V[['moto']] = asc_moto + b_inc2_1 * inc2 + b_inc3_1 * inc3 + b_inc4_1 * inc4 + b_inc5_1 * inc5 + b_inc6_1 * inc6 + b_inc7_1 * inc7 + b_inc8_1 * inc8 + b_inc9_1 * inc9 + b_estr2_1 * estruct2 + b_estr3_1 * estruct3 + b_estr4_1 * estruct4 + b_com500_1 * com_500 + b_Dens_500_1 * Dens_500 + b_Est_TM_1 * Estaciones_TM_1000 + b_Est_SITP_1 * Estaciones_SITP_300 + b_periferia_1 * periferia + b_acthom1_1 * acthom1 + b_acthom2_1 * acthom2 + b_acthom3m_1 * acthom3m
V[['carro']] = asc_carro + b_inc2_2 * inc2 + b_inc3_2 * inc3 + b_inc4_2 * inc4 + b_inc5_2 * inc5 + b_inc6_2 * inc6 + b_inc7_2 * inc7 + b_inc8_2 * inc8 + b_inc9_2 * inc9 + b_estr2_2 * estruct2 + b_estr3_2 * estruct3 + b_estr4_2 * estruct4 + b_com500_2 * com_500 + b_Dens_500_2 * Dens_500 + b_Est_TM_2 * Estaciones_TM_1000 + b_Est_SITP_2 * Estaciones_SITP_300 + b_periferia_2 * periferia + b_acthom1_2 * acthom1 + b_acthom2_2 * acthom2 + b_acthom3m_2 * acthom3m + lambda_FuncionalidadAuto*FuncionalidadAuto
V[['ambos']] = asc_ambos + b_inc2_3 * inc2 + b_inc3_3 * inc3 + b_inc4_3 * inc4 + b_inc5_3 * inc5 + b_inc6_3 * inc6 + b_inc7_3 * inc7 + b_inc8_3 * inc8 + b_inc9_3 * inc9 + b_estr2_3 * estruct2 + b_estr3_3 * estruct3 + b_estr4_3 * estruct4 + b_com500_3 * com_500 + b_Dens_500_3 * Dens_500 + b_Est_TM_3 * Estaciones_TM_1000 + b_Est_SITP_3 * Estaciones_SITP_300 + b_periferia_3 * periferia + b_acthom1_3 * acthom1 + b_acthom2_3 * acthom2 + b_acthom3m_3 * acthom3m
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(cero = 0, moto = 1, carro = 2, ambos = 3),
avail = list(cero = 1, moto = 1, carro = 1, ambos = 1),
choiceVar = respuesta,
V = lapply(V, "*", mu_eodh),
rows = (eodh == 1),
componentName = "MNL_eodh"
)
### Compute probabilities using MNL model
P[['eodh']] = apollo_mnl(mnl_settings, functionality)
### Compute probabilities for the enc part of the data using MNL model
mnl_settings$V = lapply(V, "*", mu_enc)
mnl_settings$rows = (enc == 1)
mnl_settings$componentName = "MNL_enc"
P[['enc']] = apollo_mnl(mnl_settings, functionality)
### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)
### Take product across observation for same individual
# P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Optional: calculate LL before model estimation
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Thank you for developing the Apollo package, and thank you in advance for taking the time to read this issue.
I'm trying to run a model with one (for the moment) LV. In this model, I have two sources of information where one is much bigger than the other (eodh = 21481 obs, and enc = 231 obs), and the LV is only applicable to the enc observations. At first, I tried running the code without estimating a factor scale, ignoring the fact that I have two different sources, and my model was successful. However, I want to have a scale factor for this two sources, so I review the Apollo_example_22.r file to create my code, and the Apollo_example_24.r to include the LV, but I get the following error when using apollo_estimate, which I haven't been able to debug or understand:
Testing likelihood functionError in checkForRemoteErrors(lapply(cl, recvResult)) :
5 nodes produced errors; first error: (subscript) logical subscript too long
My code is this:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "BE_LV_V1",
modelDescr = "Modelo estratégico con variables latentes",
indivID = "id_persona",
mixing = TRUE,
nCores = 6
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
setwd("~/Desktop/estrategico_LV")
database = read.csv("bd_conjunta_V1.csv", header = TRUE, dec = ".")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_cero = 0,
asc_moto = 0,
b_inc2_1 = 0,
b_inc3_1 = 0,
b_inc4_1 = 0,
b_inc5_1 = 0,
b_inc6_1 = 0,
b_inc7_1 = 0,
b_inc8_1 = 0,
b_inc9_1 = 0,
b_estr2_1 = 0,
b_estr3_1 = 0,
b_estr4_1 = 0,
b_acthom1_1 = 0,
b_acthom2_1 = 0,
b_acthom3m_1 = 0,
b_com500_1 = 0,
b_Dens_500_1 = 0,
b_Est_TM_1 = 0,
b_Est_SITP_1 = 0,
b_periferia_1 = 0,
asc_carro = 0,
b_inc2_2 = 0,
b_inc3_2 = 0,
b_inc4_2 = 0,
b_inc5_2 = 0,
b_inc6_2 = 0,
b_inc7_2 = 0,
b_inc8_2 = 0,
b_inc9_2 = 0,
b_estr2_2 = 0,
b_estr3_2 = 0,
b_estr4_2 = 0,
b_acthom1_2 = 0,
b_acthom2_2 = 0,
b_acthom3m_2 = 0,
b_com500_2 = 0,
b_Dens_500_2 = 0,
b_Est_TM_2 = 0,
b_Est_SITP_2 = 0,
b_periferia_2 = 0,
asc_ambos = 0,
b_inc2_3 = 0,
b_inc3_3 = 0,
b_inc4_3 = 0,
b_inc5_3 = 0,
b_inc6_3 = 0,
b_inc7_3 = 0,
b_inc8_3 = 0,
b_inc9_3 = 0,
b_estr2_3 = 0,
b_estr3_3 = 0,
b_estr4_3 = 0,
b_acthom1_3 = 0,
b_acthom2_3 = 0,
b_acthom3m_3 = 0,
b_com500_3 = 0,
b_Dens_500_3 = 0,
b_Est_TM_3 = 0,
b_Est_SITP_3 = 0,
b_periferia_3 = 0,
#Funcionalidad auto
lambda_FuncionalidadAuto = 1,
gamma_edad_FuncionalidadAuto = 0,
#gamma_hombre_FuncionalidadAuto = 0,
#gamma_claseBaja_FuncionalidadAuto = 0,
#gamma_claseMedia_FuncionalidadAuto = 0,
#gamma_numPersonas_FuncionalidadAuto = 0,
gamma_numAuto_FuncionalidadAuto = 0,
#gamma_numMoto_FuncionalidadAuto = 0,
gamma_IngDef_FuncionalidadAuto = 0,
#gamma_numVeh_FuncionalidadAuto = 0,
zeta_disp = 1, #p1
zeta_comodo = 1, #p5
zeta_rapido = 1, #p9
zeta_libertad = 1, #p13
tau_disp_1 = -2,
tau_disp_2 = -1,
tau_disp_3 = 1,
tau_disp_4 = 2,
tau_comodo_1 = -2,
tau_comodo_2 = -1,
tau_comodo_3 = 1,
tau_comodo_4 = 2,
tau_rapido_1 = -2,
tau_rapido_2 = -1,
tau_rapido_3 = 1,
tau_rapido_4 = 2,
tau_libertad_1 = -2,
tau_libertad_2 = -1,
tau_libertad_3 = 1,
tau_libertad_4 = 2,
mu_eodh = 1,
mu_enc = 1)
### 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_cero", "mu_eodh")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interUnifDraws = c(),
interNormDraws = c("eta_FuncionalidadAuto"),
intraDrawsType = '',
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["FuncionalidadAuto"]] = gamma_edad_FuncionalidadAuto*EDAD +
gamma_numAuto_FuncionalidadAuto*cantidad_vehiculos +
gamma_IngDef_FuncionalidadAuto*IngDef +
eta_FuncionalidadAuto
return(randcoeff)
}
# ################################################################# #
#### 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()
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = p1,
V = zeta_disp * FuncionalidadAuto,
tau = c(tau_disp_1, tau_disp_2, tau_disp_3, tau_disp_4),
rows = (enc==1),
componentName = "indic_disp")
ol_settings2 = list(outcomeOrdered = p5,
V = zeta_comodo * FuncionalidadAuto,
tau = c(tau_comodo_1, tau_comodo_2, tau_comodo_3, tau_comodo_4),
rows = (enc==1),
componentName = "indic_comodo")
ol_settings3 = list(outcomeOrdered = p9,
V = zeta_rapido * FuncionalidadAuto,
tau = c(tau_rapido_1, tau_rapido_2, tau_rapido_3, tau_rapido_4),
rows = (enc==1),
componentName = "indic_rapido")
ol_settings4 = list(outcomeOrdered = p13,
V = zeta_libertad * FuncionalidadAuto,
tau = c(tau_libertad_1, tau_libertad_2, tau_libertad_3, tau_libertad_4),
rows = (enc==1),
componentName = "indic_libertad")
P[["indic_disp"]] = apollo_ol(ol_settings1, functionality)
P[["indic_comodo"]] = apollo_ol(ol_settings2, functionality)
P[["indic_rapido"]] = apollo_ol(ol_settings3, functionality)
P[["indic_libertad"]] = apollo_ol(ol_settings4, functionality)
### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['cero']] = asc_cero
V[['moto']] = asc_moto + b_inc2_1 * inc2 + b_inc3_1 * inc3 + b_inc4_1 * inc4 + b_inc5_1 * inc5 + b_inc6_1 * inc6 + b_inc7_1 * inc7 + b_inc8_1 * inc8 + b_inc9_1 * inc9 + b_estr2_1 * estruct2 + b_estr3_1 * estruct3 + b_estr4_1 * estruct4 + b_com500_1 * com_500 + b_Dens_500_1 * Dens_500 + b_Est_TM_1 * Estaciones_TM_1000 + b_Est_SITP_1 * Estaciones_SITP_300 + b_periferia_1 * periferia + b_acthom1_1 * acthom1 + b_acthom2_1 * acthom2 + b_acthom3m_1 * acthom3m
V[['carro']] = asc_carro + b_inc2_2 * inc2 + b_inc3_2 * inc3 + b_inc4_2 * inc4 + b_inc5_2 * inc5 + b_inc6_2 * inc6 + b_inc7_2 * inc7 + b_inc8_2 * inc8 + b_inc9_2 * inc9 + b_estr2_2 * estruct2 + b_estr3_2 * estruct3 + b_estr4_2 * estruct4 + b_com500_2 * com_500 + b_Dens_500_2 * Dens_500 + b_Est_TM_2 * Estaciones_TM_1000 + b_Est_SITP_2 * Estaciones_SITP_300 + b_periferia_2 * periferia + b_acthom1_2 * acthom1 + b_acthom2_2 * acthom2 + b_acthom3m_2 * acthom3m + lambda_FuncionalidadAuto*FuncionalidadAuto
V[['ambos']] = asc_ambos + b_inc2_3 * inc2 + b_inc3_3 * inc3 + b_inc4_3 * inc4 + b_inc5_3 * inc5 + b_inc6_3 * inc6 + b_inc7_3 * inc7 + b_inc8_3 * inc8 + b_inc9_3 * inc9 + b_estr2_3 * estruct2 + b_estr3_3 * estruct3 + b_estr4_3 * estruct4 + b_com500_3 * com_500 + b_Dens_500_3 * Dens_500 + b_Est_TM_3 * Estaciones_TM_1000 + b_Est_SITP_3 * Estaciones_SITP_300 + b_periferia_3 * periferia + b_acthom1_3 * acthom1 + b_acthom2_3 * acthom2 + b_acthom3m_3 * acthom3m
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(cero = 0, moto = 1, carro = 2, ambos = 3),
avail = list(cero = 1, moto = 1, carro = 1, ambos = 1),
choiceVar = respuesta,
V = lapply(V, "*", mu_eodh),
rows = (eodh == 1),
componentName = "MNL_eodh"
)
### Compute probabilities using MNL model
P[['eodh']] = apollo_mnl(mnl_settings, functionality)
### Compute probabilities for the enc part of the data using MNL model
mnl_settings$V = lapply(V, "*", mu_enc)
mnl_settings$rows = (enc == 1)
mnl_settings$componentName = "MNL_enc"
P[['enc']] = apollo_mnl(mnl_settings, functionality)
### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)
### Take product across observation for same individual
# P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Optional: calculate LL before model estimation
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)