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.

Error in bootstrapSE

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
intllawKuma
Posts: 6
Joined: 29 Apr 2022, 08:06

Error in bootstrapSE

Post by intllawKuma »

Hello,

I modified "LC_with_covariates" in Example Files and tried to estimate bootstrap standard errors.
However, I got the following error message:

Error in apollo_preprocess(inputs = mnl_settings, modelType, functionality, : Each element of "utilities" for model component "Class_1" needs to be a scalar or a vector/matrix/cube with one row per observation in the "database"

I would appreciate it if you could tell me what is wrong with the following code.

Code: Select all


library(apollo)
apollo_initialise()
apollo_control = list(
  modelName       = "LC_with_covariates",
  modelDescr      = "LC model with covariates",
  indivID         = "ID",
  nCores          = 3, 
  outputDirectory = "output"
)
database = apollo_swissRouteChoiceData

apollo_beta = c(asc_1           = 0,
                asc_2           = 0,
                beta_tt_a       = 0,
                beta_tt_b       = 0,
                beta_tc_a       = 0,
                beta_tc_b       = 0,
                beta_hw_a       =-0.0396,
                beta_hw_b       =-0.0479,
                beta_ch_a       =-0.7624,
                beta_ch_b       =-2.1725,
                delta_a         = 0.0329,
                gamma_commute_a = 0,
                gamma_car_av_a  = 0,
                delta_b         = 0,
                gamma_commute_b = 0,
                gamma_car_av_b  = 0)

apollo_fixed = c("asc_2","delta_b","gamma_commute_b","gamma_car_av_b")

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["beta_tt"]] = list(beta_tt_a, beta_tt_b)
  lcpars[["beta_tc"]] = list(beta_tc_a, beta_tc_b)
  lcpars[["beta_hw"]] = list(beta_hw_a, beta_hw_b)
  lcpars[["beta_ch"]] = list(beta_ch_a, beta_ch_b)
  
  ### Utilities of class allocation model
  V=list()
  V[["class_a"]] = delta_a + gamma_commute_a*commute + gamma_car_av_a*car_availability
  V[["class_b"]] = delta_b + gamma_commute_b*commute + gamma_car_av_b*car_availability
  
  ### 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)
}

apollo_inputs = apollo_validateInputs()

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()
  
  ### Define settings for MNL model component that are generic across classes
  mnl_settings = list(
    alternatives = c(alt1=1, alt2=2),
    avail        = list(alt1=1, alt2=1),
    choiceVar    = choice
  )
  
  ### Loop over classes
  for(s in 1:2){
    
    ### Compute class-specific utilities
    V=list()
    V[["alt1"]]  = asc_1 + beta_tc[[s]]*tc1 + beta_tt[[s]]*tt1 + beta_hw[[s]]*hw1 + beta_ch[[s]]*ch1
    V[["alt2"]]  = asc_2 + beta_tc[[s]]*tc2 + beta_tt[[s]]*tt2 + beta_hw[[s]]*hw2 + beta_ch[[s]]*ch2
    
    mnl_settings$utilities     = V
    mnl_settings$componentName = paste0("Class_",s)
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### 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, classProb=pi_values)
  P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs,estimate_settings=list(bootstrapSE=30))
apollo_modelOutput(model, list(printPVal=2))

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

Re: Error in bootstrapSE

Post by dpalma »

Hi,

You seem to have found a bug. This has been corrected in v0.2.8. That version is not released yet, but you can download it from this link (for windows).

Once you downloaded the file, you can installing using RStudio by going to Tools > Install packages... . Then, in the "Install From" field select "Package Archive file" and select the downloaded file in the file selection window that just opened. Then click the "Install" button.

Also, I recommend running the model using a single core. Using multiple cores is particularly beneficial for models with random coefficients or error terms. Not so much for simple latent class models.

Cheers
David
intllawKuma
Posts: 6
Joined: 29 Apr 2022, 08:06

Re: Error in bootstrapSE

Post by intllawKuma »

Dear Dr. Palma,

I successfully got bootstrap standard errors by v0.2.8 that I downloaded from your link.
Thank you.
rupch22
Posts: 2
Joined: 31 May 2022, 23:45

Re: Error in bootstrapSE

Post by rupch22 »

Tried executing this with the unreleased version, yet getting this error.
Estimation cycle 1 (8532 obs)
#############################################################################################
The scaling of the model led to differences in the results, and was thus not used.
This is unexpected. You may want to contact the developers.

... current process will resume in 5 seconds unless interrupted by the user
#############################################################################################
Error in stats::cov(paramStack[includeRow, ]) :
supply both 'x' and 'y' or a matrix-like 'x'
In addition: There were 24 warnings (use warnings() to see them)
###############################################################
Insights are welcome. Thank you
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error in bootstrapSE

Post by dpalma »

Hi,

Sorry for the belated reply.

You are right, there was a bug in the file I gave you access to. That bug has been fixed in the current version, which you can download from the following links:
Windows
Linux
The mac version will be uploaded to the webpage soon.

Cheers
David
rupch22
Posts: 2
Joined: 31 May 2022, 23:45

Re: Error in bootstrapSE

Post by rupch22 »

Hi Dr Palma,
I can't find the version you attached. Could you please attach it again?

Best regards
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error in bootstrapSE

Post by dpalma »

Hi,

Sorry about that, the file was probably updated so the link stopped working. Please try one of the following:
Windows
Linux (source)
macOS

Anyhow, v0.2.8 should be available from CRAN in the next few days.

Cheers
David
Post Reply