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.
  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 when using mixed logit with only one observation per individual

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
victornielsen
Posts: 7
Joined: 19 Apr 2022, 15:49

Error when using mixed logit with only one observation per individual

Post by victornielsen » 09 Aug 2022, 14:29

Hello

I am trying to build a mixed ordered logit model with a random error term.

This is my model:

Code: Select all

#### nautos ####
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())
s12 <- read_csv("discretechoice.csv")


### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MOLnautos",
  modelDescr      = "Mixed ordered logit model fitted to MOCOLODO questionnaire",
  indivID         = "uuid2",
  mixing          = TRUE,
  nCores          = 4, 
  outputDirectory = "output"
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
database = s12
### for data dictionary, use ?apollo_drugChoiceData

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

#ref cat strategy: don't use one that is too small and think of what you want to explore. Otherwise choose the middle or extremes.

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
  sigma_error                                       = 0,
  #beta_randstad_change_randstad                    = 0,
  beta_randstad_change_notrandstad                    = 0,
  beta_randstad_change_torandstad                    = 0,
  beta_randstad_change_fromrandstad                    = 0,
  # beta_leeftijd_group_10_19               = 0,
  # beta_leeftijd_group_20_29               = 0,
  # #beta_leeftijd_group_30_39               = 0,
  # beta_leeftijd_group_40_49               = 0,
  # beta_leeftijd_group_50_59               = 0,
  # beta_leeftijd_group_60_69               = 0,
  # beta_leeftijd_group_70_79               = 0,
  # beta_leeftijd_group_80_                 = 0,
  # beta_leeftijd_group_10_29               = 0,
  # #beta_leeftijd_group_30_49               = 0,
  # beta_leeftijd_group_50_69               = 0,
  # beta_leeftijd_group_70_                 = 0,
  # #beta_huishoudensinkomen__980            = 0,
  # beta_huishoudensinkomen_980_1870        = 0,
  # beta_huishoudensinkomen_1870_2680       = 0,
  # beta_huishoudensinkomen_2680_3800       = 0,
  # beta_huishoudensinkomen_3800_5460       = 0,
  # beta_huishoudensinkomen_5460_           = 0,
  beta_huishoudensinkomen__1870           = 0,
  #beta_huishoudensinkomen_1870_3800       = 0,
  beta_huishoudensinkomen_3800_           = 0,
  #beta_nelectrischefietsen_change_2       = 0,
  beta_nelectrischefietsen_change_3       = 0,
  beta_nelectrischefietsen_change_1      = 0,
  #beta_nnormalefietsen_change_2           = 0,
  beta_nnormalefietsen_change_3           = 0,
  beta_nnormalefietsen_change_1          = 0,
  # #beta_sted_woon_nu_1                     = 0,
  # beta_sted_woon_nu_2                     = 0,
  # beta_sted_woon_nu_3                     = 0,
  # beta_sted_woon_nu_4                     = 0,
  # beta_sted_woon_nu_5                     = 0,
  # beta_sted_woon_nu_NA                    = 0,
  beta_huishoudenstype_anders             = 0,
  beta_huishoudenstype_eengezinshuishouden= 0,
  beta_huishoudenstype_eengezinshuishoudenmetkinderen    = 0,
  beta_huishoudenstype_meergezinshuishoudenmetkinderen   = 0,
  #beta_huishoudenstype_meergezinshuishoudenzonderkinderen= 0,
  beta_weekdist_woonwerk_changecat_decreasedcommute    = 0,
  beta_weekdist_woonwerk_changecat_increasedcommute    = 0,
  beta_weekdist_woonwerk_changecat_samecommute         = 0,
  #beta_weekdist_woonwerk_changecat_nojob              = 0,
  beta_weekdist_woonwerk_changecat_lostjob             = 0,
  beta_weekdist_woonwerk_changecat_gotjob              = 0,
  beta_weekdist_woonwerk_changecat_alwaysworkfromhome  = 0,
  beta_weekdist_woonwerk_changecat_workfromhometocommuting = 0,
  beta_weekdist_woonwerk_changecat_commutingtoworkfromhome = 0,
  beta_weekdist_woonwerk_changecat_differingaddresses  = 0,
  tau_nautos_change_1                = 0,
  tau_nautos_change_2                = 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()

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "sobol",
  interNDraws    = 10,
  interUnifDraws = c(),
  interNormDraws = paste0("draws_", c("error")),
  intraDrawsType = "",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["error"]]  = sigma_error*draws_error
  
  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()
  
  ### Calculate probabilities using Ordered Logit model
  ol_settings = list(outcomeOrdered = nautos_change,
                     utility        =
                       error +
                       beta_randstad_change_notrandstad*randstad_change_notrandstad +
                       beta_randstad_change_torandstad*randstad_change_torandstad +
                       beta_randstad_change_fromrandstad*randstad_change_fromrandstad +
                       #beta_randstad_randstad_randstad_randstad +
                       # beta_leeftijd_group_10_19*leeftijd_group_10_19 +
                       # beta_leeftijd_group_20_29*leeftijd_group_20_29 +
                       # #beta_leeftijd_group_30_39*leeftijd_group_30_39 +
                       # beta_leeftijd_group_40_49*leeftijd_group_40_49 +
                       # beta_leeftijd_group_50_59*leeftijd_group_50_59 +
                       # beta_leeftijd_group_60_69*leeftijd_group_60_69 +
                       # beta_leeftijd_group_70_79*leeftijd_group_70_79 +
                       # beta_leeftijd_group_80_*leeftijd_group_80_ +
                       # beta_leeftijd_group_10_29*leeftijd_group_10_29 +
                       # #beta_leeftijd_group_30_49*leeftijd_group_30_49 +
                     # beta_leeftijd_group_50_69*leeftijd_group_50_69 +
                     # beta_leeftijd_group_70_*leeftijd_group_70_ +
                     # #beta_huishoudensinkomen__980*huishoudensinkomen__980 +
                     # beta_huishoudensinkomen_980_1870*huishoudensinkomen_980_1870 +
                     # beta_huishoudensinkomen_1870_2680*huishoudensinkomen_1870_2680 +
                     # beta_huishoudensinkomen_2680_3800*huishoudensinkomen_2680_3800 +
                     # beta_huishoudensinkomen_3800_5460*huishoudensinkomen_3800_5460 +
                     # beta_huishoudensinkomen_5460_*huishoudensinkomen_5460_ +
                     beta_huishoudensinkomen__1870*huishoudensinkomen__1870 +
                       #beta_huishoudensinkomen_1870_3800*huishoudensinkomen_1870_3800 +
                       beta_huishoudensinkomen_3800_*huishoudensinkomen_3800_ +
                       #beta_nelectrischefietsen_change_2*nelectrischefietsen_change_2 +
                       beta_nelectrischefietsen_change_3*nelectrischefietsen_change_3 +
                       beta_nelectrischefietsen_change_1*nelectrischefietsen_change_1 +
                       #beta_nnormalefietsen_change_2*nnormalefietsen_change_2 +
                       beta_nnormalefietsen_change_3*nnormalefietsen_change_3 +
                       beta_nnormalefietsen_change_1*nnormalefietsen_change_1 +
                       # #beta_sted_woon_nu_1*sted_woon_nu_1 +
                       # beta_sted_woon_nu_2*sted_woon_nu_2 +
                       # beta_sted_woon_nu_3*sted_woon_nu_3 +
                       # beta_sted_woon_nu_4*sted_woon_nu_4 +
                       # beta_sted_woon_nu_5*sted_woon_nu_5 +
                       # beta_sted_woon_nu_NA*sted_woon_nu_NA +
                       beta_huishoudenstype_anders*huishoudenstype_anders +
                       beta_huishoudenstype_eengezinshuishouden*huishoudenstype_eengezinshuishouden +
                       beta_huishoudenstype_eengezinshuishoudenmetkinderen*huishoudenstype_eengezinshuishoudenmetkinderen +
                       beta_huishoudenstype_meergezinshuishoudenmetkinderen*huishoudenstype_meergezinshuishoudenmetkinderen +
                       #beta_huishoudenstype_meergezinshuishoudenzonderkinderen*huishoudenstype_meergezinshuishoudenzonderkinderen +
                       beta_weekdist_woonwerk_changecat_decreasedcommute*weekdist_woonwerk_changecat_decreasedcommute +
                       beta_weekdist_woonwerk_changecat_increasedcommute*weekdist_woonwerk_changecat_increasedcommute +
                       beta_weekdist_woonwerk_changecat_samecommute*weekdist_woonwerk_changecat_samecommute +
                       #beta_weekdist_woonwerk_changecat_nojob*weekdist_woonwerk_changecat_nojob +
                       beta_weekdist_woonwerk_changecat_lostjob*weekdist_woonwerk_changecat_lostjob +
                       beta_weekdist_woonwerk_changecat_gotjob*weekdist_woonwerk_changecat_gotjob +
                       beta_weekdist_woonwerk_changecat_alwaysworkfromhome*weekdist_woonwerk_changecat_alwaysworkfromhome +
                       beta_weekdist_woonwerk_changecat_workfromhometocommuting*weekdist_woonwerk_changecat_workfromhometocommuting +
                       beta_weekdist_woonwerk_changecat_commutingtoworkfromhome*weekdist_woonwerk_changecat_commutingtoworkfromhome +
                       beta_weekdist_woonwerk_changecat_differingaddresses*weekdist_woonwerk_changecat_differingaddresses,
                     tau             = list(tau_nautos_change_1, tau_nautos_change_2))
  #rows           = (task==1))
  
  P[["model"]] = apollo_ol(ol_settings, functionality)
  
  ### Take product across observation for same individual
  #P = apollo_panelProd(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)

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

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

apollo_modelOutput(model)

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

apollo_saveOutput(model)
When I run it, I get this error:

Code: Select all

Pre-processing likelihood function...
Preparing workers for multithreading...

Testing influence of parametersError in checkForRemoteErrors(lapply(cl, recvResult)) : 
  4 nodes produced errors; first error: Need to average over inter-individual draws! (see ?apollo_avgInterDraws)
Seems to be a problem with the fact that I have one observation per individual. I don't know how to solve it since all the examples on the websites have multiple observations per individual.

I tried changing this section:

Code: Select all

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "sobol",
  interNDraws    = 10,
  interUnifDraws = c(),
  interNormDraws = paste0("draws_", c("error")),
  intraDrawsType = "",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)
By switching around the inter and intra values, but it gave a different error:

Code: Select all

All checks on database completed.
Error in apollo_makeDraws(apollo_inputs, silent = silent) : 
Intra-person draws are used without a panel structure. This is not allowed!
Any idea what I should do? I am trying to make a mixed ordered logit model.

Any help will be greatly appreciated, thank you!

stephanehess
Site Admin
Posts: 632
Joined: 24 Apr 2020, 16:29

Re: Error when using mixed logit with only one observation per individual

Post by stephanehess » 09 Aug 2022, 16:24

Hi

Apollo is trying to help you by saying

Code: Select all

Need to average over inter-individual draws!
You are missing the call to

Code: Select all

apollo_avgInterDraws
, i.e. you are estimating a mixed model without averaging over draws.

The draws are inter-individual as you only have one observation per person, so they couldn't be intra-individual

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk

victornielsen
Posts: 7
Joined: 19 Apr 2022, 15:49

Re: Error when using mixed logit with only one observation per individual

Post by victornielsen » 10 Aug 2022, 16:17

What goes into that call? And where do I add it?

stephanehess
Site Admin
Posts: 632
Joined: 24 Apr 2020, 16:29

Re: Error when using mixed logit with only one observation per individual

Post by stephanehess » 10 Aug 2022, 20:51

Please have a look at the mixed logit examples on the website
--------------------------------
Stephane Hess
www.stephanehess.me.uk

victornielsen
Posts: 7
Joined: 19 Apr 2022, 15:49

Re: Error when using mixed logit with only one observation per individual

Post by victornielsen » 11 Aug 2022, 13:45

Got it to work. Solution confirmed.

Post Reply