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.

ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
hrrichard
Posts: 10
Joined: 26 Apr 2020, 15:03

ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Post by hrrichard »

Hi Stephane and David,

I am trying to estimate a hybrid choice model, and the multithreading process is not completed. Using 1 core, the model runs. When I estimate a Mixed-logit on the same data, I do not encounter this issue. Please see the code of the hybrid model below :

Code: Select all

# ################################################################# #
#### 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 ="HCM_10.3_Additve_spec",
  modelDescr ="HCM on HK mode choice" ,
  indivID   ="ID",  
  mixing    = TRUE,
  nCores    = 5,
  seed      = 1
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

database = read.csv("Transp_data.csv",header=TRUE)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_MMB=8.07867264304393,
                asc_TX=14.1425505024284,
                asc_MB=8.37124321068007,
                asc_NO=0,
                LV_alig_2_sig=0.15,
                LV_alig_3_sig=0.1,
                LV_cont_2_sig=0.1,
                LV_cont_3_sig=0.3,
                LV_cont_4_sig=0.2,
                LV_tt_1_sig=-0.00690069481267281,
                LV_tt_2_sig=-0.0526678409831866,
                LV_tc_sig=-0.0153376953475673,
                LV_tt_t_sig=-0.0526678409831866,
                LV_tc_t_sig=-0.0153376953475673,
                
                
                gamma_Repeat_vis_LV_Transport_perception=-0.330606290602541,
                gamma_Long_stay_LV_Transport_perception=-0.419229905652882,
                gamma_att_long_stay=-1.04105157135528,
                gamma_att_Repeat_vis=1.44802784555093,
                gamma_ratt_2_long_stay=5.16792949094382,
                gamma_ratt_2_Repeat_vis=0.264547788636091,
                gamma_ratt_3_long_stay=0.910518293398389,
                gamma_ratt_3_Repeat_vis=-2.28861227368236,
                gamma_ratt_4_long_stay=-0.90320923093451,
                gamma_ratt_4_Repeat_vis=-1.21661259009885,
                gamma_tt_1_long_stay=0.0310079716670544,
                gamma_tt_1_Repeat_vis=0.00305837384742729,
                gamma_tt_2_long_stay=0.0531481122826535,
                gamma_tt_2_Repeat_vis=0.0496424976439684,
                gamma_tc_long_stay=0.0152064786451245,
                gamma_tc_Repeat_vis=0.0148539003396019,
                gamma_tt_t_long_stay=0.000873118038410258,
                gamma_tt_t_Repeat_vis=-0.0606427779063918,
                gamma_tc_t_long_stay=-0.000955604312717446,
                gamma_tc_t_Repeat_vis=0.00804111092832115,
                gamma_cont_2_long_stay=-1.05719156097221,
                gamma_cont_2_Repeat_vis=-0.2910588084329,
                gamma_cont_3_long_stay=-1.06520463049246,
                gamma_cont_3_Repeat_vis=-0.418864919754063,
                gamma_cont_4_long_stay=-0.853521890329236,
                gamma_cont_4_Repeat_vis=-0.243132925148644,
                gamma_alig_2_long_stay=-1.94525411755059,
                gamma_alig_2_Repeat_vis=-1.73449015629004,
                gamma_alig_3_long_stay=-1.2861021856139,
                gamma_alig_3_Repeat_vis=-1.44450104944019,
                zeta_lv_perc_1=1,
                tau_lv_perc_1_1=-3,
                tau_lv_perc_1_2=3,
                zeta_lv_perc_3=1.73160727845793,
                tau_lv_perc_3_1=-7.06980942882699,
                tau_lv_perc_3_2=-3.93173332063141,
                tau_lv_perc_3_3=-2.27856774291353,
                tau_lv_perc_3_4=1.10291753123815,
                att_mu=-2.13329277915583,
                att_sig=-1.60246131551844,
                ratt_1=0,
                ratt_2_mu=-7.04535402682224,
                ratt_2_sig=4.25099356938291,
                ratt_3_mu=4.91948650647052,
                ratt_3_sig=2.72711149986421,
                ratt_4_mu=4.7457409526998,
                ratt_4_sig=3.97937621097142,
                tt_1_mu=-0.0834060960217425,
                tt_1_sig=0.0298362540846351,
                tt_2_mu=-0.106817424568177,
                tt_2_sig=0.0827947571407638,
                tc_mu=-0.0828335208169694,
                tc_sig=-0.0237097011316197,
                tt_t_mu=-0.0974516470649308,
                tt_t_sig=0.117595735340974,
                tc_t_mu=-0.0677111031816054,
                tc_t_sig=-0.0136903559499059,
                alig_1=0,
                alig_2_mu=3.74916031794965,
                alig_2_sig=-0.113769248730282,
                alig_3_mu=2.63595392254908,
                alig_3_sig=0.0508798403249827,
                cont_1=0,
                cont_2_mu=1.12854769250774,
                cont_2_sig=0.663869560033205,
                cont_3_mu=0.507381018899945,
                cont_3_sig=1.15584140932361,
                cont_4_mu=1.55075862278489,
                cont_4_sig=-0.00244538458101513)
  

### 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_NO","ratt_1","alig_1","cont_1")

### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"HCM_10.2_additve_spec",overwriteFixed = FALSE)

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "pmc",
  interNDraws    = 100,
  interUnifDraws = c(),
  interNormDraws = c("draws_LV_Tr_perc",
                     "draws_tt_1","draws_tt_2","draws_tc","draws_tc_t","draws_tt_t",
                     "draws_att","draws_ratt_2","draws_ratt_3","draws_ratt_4",
                     "draws_alig_2","draws_alig_3",
                     "draws_cont_2","draws_cont_3","draws_cont_4"),
                     #"draws_asc_MMB","draws_asc_MB","draws_asc_TX"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["LV_Tr_perc"]] =  draws_LV_Tr_perc + gamma_Repeat_vis_LV_Transport_perception*Repeat_vis +
    gamma_Long_stay_LV_Transport_perception*four_nights_long
 
  randcoeff[["tt_1"]] = tt_1_mu +  tt_1_sig * draws_tt_1+gamma_tt_1_long_stay*four_nights_long +gamma_tt_1_Repeat_vis*Repeat_vis
  randcoeff[["tt_2"]] = tt_2_mu +  tt_2_sig * draws_tt_2+gamma_tt_2_long_stay*four_nights_long +gamma_tt_2_Repeat_vis*Repeat_vis
  randcoeff[["tc"]] = tc_mu +  tc_sig * draws_tc+gamma_tc_long_stay*four_nights_long +  gamma_tc_Repeat_vis*Repeat_vis
  randcoeff[["tt_t"]] = tt_t_mu +  tt_t_sig * draws_tt_t+gamma_tt_t_long_stay*four_nights_long +gamma_tt_t_Repeat_vis*Repeat_vis
  randcoeff[["tc_t"]] = tc_t_mu +  tc_t_sig * draws_tc_t+gamma_tc_t_long_stay*four_nights_long +gamma_tc_t_Repeat_vis*Repeat_vis
  randcoeff[["att"]] = att_mu +  att_sig * draws_att+gamma_att_long_stay*four_nights_long + gamma_att_Repeat_vis*Repeat_vis
  randcoeff[["ratt_2"]] = ratt_2_mu +  ratt_2_sig * draws_ratt_2+gamma_ratt_2_long_stay*four_nights_long +  gamma_ratt_2_Repeat_vis*Repeat_vis
  randcoeff[["ratt_3"]] = ratt_3_mu +  ratt_3_sig * draws_ratt_3+gamma_ratt_3_long_stay*four_nights_long +  gamma_ratt_3_Repeat_vis*Repeat_vis
  randcoeff[["ratt_4"]] = ratt_4_mu +  ratt_4_sig * draws_ratt_4+gamma_ratt_4_long_stay*four_nights_long +  gamma_ratt_4_Repeat_vis*Repeat_vis
  randcoeff[["cont_2"]] = cont_2_mu +  cont_2_sig * draws_cont_2+gamma_cont_2_long_stay*four_nights_long +  gamma_cont_2_Repeat_vis*Repeat_vis
  randcoeff[["cont_3"]] = cont_3_mu +  cont_3_sig * draws_cont_3+gamma_cont_3_long_stay*four_nights_long +  gamma_cont_3_Repeat_vis*Repeat_vis
  randcoeff[["cont_4"]] = cont_4_mu +  cont_4_sig * draws_cont_4+gamma_cont_4_long_stay*four_nights_long +  gamma_cont_4_Repeat_vis*Repeat_vis
  randcoeff[["alig_2"]] = alig_2_mu +  alig_2_sig * draws_alig_2+gamma_alig_2_long_stay*four_nights_long +  gamma_alig_2_Repeat_vis*Repeat_vis
  randcoeff[["alig_3"]] = alig_3_mu +  alig_3_sig * draws_alig_3+gamma_alig_3_long_stay*four_nights_long +  gamma_alig_3_Repeat_vis*Repeat_vis
  
  return(randcoeff)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){

  ### Function initialisation: do not change the following three commands
  ### 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()
  
  ## PERCVULNEV
  OL_settings1 = list(outcomeOrdered=Q49_1.1, 
                      V=zeta_lv_perc_1*LV_Tr_perc,
                      tau=c(tau_lv_perc_1_1,tau_lv_perc_1_2),
                      rows=(Sequence==1),
                      componentName = "indic_1")
  
  OL_settings3 = list(outcomeOrdered=Q49_3, 
                      V=zeta_lv_perc_3*LV_Tr_perc,
                      tau=c(tau_lv_perc_3_1,tau_lv_perc_3_2,tau_lv_perc_3_3,tau_lv_perc_3_4),
                      rows=(Sequence==1),
                      componentName = "indic_3")
  
  P[["indic_1"]] = apollo_ol(OL_settings1, functionality)
  P[["indic_3"]] = apollo_ol(OL_settings3, functionality)
  
  
  ### Creating interactions with latent variable
  tt_1_val=tt_1+LV_tt_1_sig*LV_Tr_perc
  tt_2_val=tt_2+LV_tt_2_sig*LV_Tr_perc
  tt_t_val=tt_t+LV_tt_t_sig*LV_Tr_perc

  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  
  V = list()
  V[['MMB']]  = asc_MMB  + tt_1_val*mm.tt1 + tt_2_val*mm.tt2 + tc_val*mm.tc + cont_1*(mm.cont==0) + cont_2_val*(mm.cont==1) + cont_3_val*(mm.cont==2) +cont_4_val*(mm.cont==3) + alig_1*(mm.aligm==0) + alig_2_val*(mm.aligm==1) + alig_3_val*(mm.aligm==2) 
  V[['MBUS']]  = asc_MB  + tt_1_val*mb.tt1 + tt_2_val*mb.tt2 + tc_val*mb.tc + cont_1*(mb.cont==0) + cont_2_val*(mb.cont==1) + cont_3_val*(mb.cont==2) +cont_4_val*(mb.cont==3) + alig_1*(mb.aligb==0) + alig_2_val*(mb.aligb==1) + alig_3_val*(mb.aligb==2) 
  V[['TAXI']]  = asc_TX + tt_t_val*tx.ttt + tc_t_val*tx.ttc 
  V[['NO']] = asc_NO + att*mm.attr + ratt_1*(mm.rattr==0) + ratt_2*(mm.rattr==1) + ratt_3*(mm.rattr==2) + ratt_4*(mm.rattr==3)
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(MMB=1, MBUS=2, TAXI=3, NO=4), 
    avail         = list(MMB=1, MBUS=1, TAXI=1, NO=1), 
    choiceVar     = Choice1,
    V             = V,
    componentName = "Mode_choice"
  )

  ### Compute probabilities for MNL model component
  P[["Mode_choice"]] = 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                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs) #estimate_settings = list(maxIterations=500,hessianRoutine="maxLik"))
Thank you for your help!

Richard
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Post by dpalma »

Hi Richard,

What version are you using? You can download the latest version from the webpage: http://www.apollochoicemodelling.com/code.html .
Please let us know if the problem persists after updating to the latest version.

Cheers
David
hrrichard
Posts: 10
Joined: 26 Apr 2020, 15:03

Re: ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Post by hrrichard »

Hi David,

Thank you for your reply.

I am using Apollo 0.2.4. This issue may be related to my machine. I experienced the error message after I updated my operating system on Mac to Big Sur. I also observed that this issue occurs in the ICLV model with ordered specification of the measurement model, while when the measurement model is specified with linear regression, the model runs with multiple cores. I have mentioned this here, as this information may be useful for other users.

Could you please help me what R version Apollo 0.2.5 is associated with? When I tried to install Apollo 0.2.5, I got an error message saying that apollo_0.2.5.tar is not available for this version of R. I have the most recent R version (4.0.5) installed.

Thank you!

Cheers,
Richard
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Post by dpalma »

Hi Richard,

There is a bug in Apollo v0.2.4 that prevents using apollo_ol with multiple cores. v0.2.5 hasn't been released on CRAN yet, but you can download the latest development version of Apollo from http://www.apollochoicemodelling.com/code.html. Note that you need to download the appropriate file for your operating system.

If you are using RStudio, you install this file simply by selecting Tool > Install Packages... in the menu bar, and then selecting Install from: Package Archive File, and select the file you downloaded.

If you are not using RStudio, then you can run the following code:

Code: Select all

install.packages("C:/path/to/file/apollo_0.2.5.zip", repos=NULL)
Where you need to change "C:/path/to/file/apollo_0.2.5.zip" to the appropriate path to the downloaded file.

Cheers
David
hrrichard
Posts: 10
Joined: 26 Apr 2020, 15:03

Re: ICLV estimation: Model components are inconsistent across workers. Try seting apollo_control$nCores=1

Post by hrrichard »

Hi David,

Thank you very much for your help and for the detailed explanation on the installation of the package update.

Cheers,
Richard
Post Reply