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.

An error when using Mixed Logit model

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Zhenyu
Posts: 3
Joined: 07 Mar 2021, 20:48

An error when using Mixed Logit model

Post by Zhenyu »

Hi everyone,

#My estimation reports an error message that "Error in v[r] : object of type 'closure' is not subsettable".
#please find the attached dataset for your reference
# Thanks for your help in advance.

#Here is my entire code

Code: Select all


rm(list = ls())
library(apollo)
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName ="redtide_uncorrelated",
  modelDescr ="Mixed logit model, uncorrelated Lognormals in utility space",
  indivID   ="id",  
  mixing    = TRUE, 
  nCores    = 4
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #
library(readxl)
database<-read.csv("D:\\cleaned_data.csv")

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(mu_log_b_cov    =-3,
                sigma_log_b_cov = 0,
                mu_log_b_acc1    =-3,
                sigma_log_b_acc1 = 0,
                mu_log_b_acc2    =-3,
                sigma_log_b_acc2 = 0,
                mu_log_b_bid    =-3,
                sigma_log_b_bid = 0)

### 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 = "halton",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_cov","draws_acc1","draws_acc2","draws_bid"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_cov"]] = -exp( mu_log_b_cov + sigma_log_b_cov * draws_cov )
  randcoeff[["b_acc1"]] = -exp( mu_log_b_acc1 + sigma_log_b_acc1 * draws_acc1 )
  randcoeff[["b_acc2"]] = -exp( mu_log_b_acc2 + sigma_log_b_acc2 * draws_acc2 )
  randcoeff[["b_bid"]] = -exp( mu_log_b_bid + sigma_log_b_bid * draws_bid )
  
  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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["alt1"]] = b_cov * cov_1 + b_acc1 * acc1_1 + b_acc2 * acc2_1 + b_bid * bid_1
  V[["alt2"]] = b_cov * cov_2 + b_acc1 * acc1_2 + b_acc2 * acc2_2 + b_bid * bid_2
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1,alt2=2),
    avail          = list(alt1=1, alt2=1),
    choiceVar  = choice,
    V              = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, 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)
Attachments
cleaned_data.rar
(906 Bytes) Downloaded 404 times
stephanehess
Site Admin
Posts: 1042
Joined: 24 Apr 2020, 16:29

Re: An error when using Mixed Logit model

Post by stephanehess »

Hi

what version of Apollo are you using?

it's not immediately clear why you are getting this error message, but I noticed that in your data, you only have one row per individual, but you are still treating the data as if you had multiple rows per person (i.e. using apollo_panelProd). Was this just an extract of your data?

Also, and unrelated to the error you are getting, trying to estimate a mixed logit model with four randomly distributed coefficients on data with just 159 observations is likely to get you into trouble.

Also, in your data, you have columns for a third alternative, but you are only using two in your model. Is that correct?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Zhenyu
Posts: 3
Joined: 07 Mar 2021, 20:48

Re: An error when using Mixed Logit model

Post by Zhenyu »

Hi Stephane,

Thanks for your reply. I use Apollo 0.2.4. I edit my script based on your reply and find your suggestions help me a lot.

1) There is only one row per individual in my dataset and this is a subset of my dataset. It should not be treated as a panel structure. Therefore, if I want to continue this non-panel structure dataset, I should comment on the "apollo_panel(Prod)".
2) You are right and only 159 observations are not enough to estimate a mixed logit model with four randomly distributed coefficients. I use all observations (502*4=2008 observations) in my model to do the estimation.
3) The third column is the option of status quo which are zeros for all four random variables. I neglect it at the very beginning but soon find this option should be included as option 1 and option 2.

One question I have is that I have to clean the dataset outside of my main script, save it as a CSV file and then import it directly in the main script otherwise there are some errors reporting. Is it because the dataset in the main script needs to be named as "database"?

Based on your suggestions, I edit my main script as follows. The results are quite interesting as estimates are reported but anything else (such as s.e., t.rat.(0), etc) is NA. What are the reasons for these NA values? The NA normally signals convergence issues in other packages but I am not sure the reasons here.

Please find the dataset in the attachment.

Code: Select all

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName ="RT_InterIntraHetero",
  modelDescr ="Mixed logit model, inter and intra-individual heterogeneity",
  indivID   ="id",  
  mixing    = TRUE, 
  nCores    = 4
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #
library(readxl)
database<-read.csv("D:\\RT_setall.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc1=0,
                asc2=0,
                asc3=0,
                mu_log_b_cov    =-3,
                sigma_log_b_cov_inter = 0,
                sigma_log_b_cov_intra = 0,
                mu_log_b_acc1    =-3,
                sigma_log_b_acc1_inter = 0,
                sigma_log_b_acc1_intra = 0,
                mu_log_b_acc2    =-3,
                sigma_log_b_acc2_inter = 0,
                sigma_log_b_acc2_intra = 0,
                mu_log_b_bid    =-3,
                sigma_log_b_bid_inter = 0,
                sigma_log_b_bid_intra = 0)

### 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("asc3")

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 500,
  interUnifDraws = c("draws_cov_intra","draws_acc1_intra","draws_acc2_intra","draws_bid_intra"),
  interNormDraws = c("draws_cov_inter","draws_acc1_inter","draws_acc2_inter","draws_bid_inter"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_cov"]] = -exp( mu_log_b_cov + sigma_log_b_cov_inter * draws_cov_inter +
                                 sigma_log_b_cov_intra * draws_cov_intra)
  
  randcoeff[["b_acc1"]] = -exp( mu_log_b_acc1 + sigma_log_b_acc1_inter * draws_acc1_inter +
                                  sigma_log_b_acc1_intra * draws_acc1_intra)
  
  randcoeff[["b_acc2"]] = -exp( mu_log_b_acc2 + sigma_log_b_acc2_inter * draws_acc2_inter  +
                                  sigma_log_b_acc2_intra * draws_acc2_intra)
  
  randcoeff[["b_bid"]] = -exp( mu_log_b_bid + sigma_log_b_bid_inter * draws_bid_inter  +
                                 sigma_log_b_bid_intra * draws_bid_intra)
  
  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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["alt1"]] = asc1 + b_cov * cov_1 + b_acc1 * acc1_1 + b_acc2 * acc2_1 + b_bid * bid_1
  V[["alt2"]] = asc2 + b_cov * cov_2 + b_acc1 * acc1_2 + b_acc2 * acc2_2 + b_bid * bid_2
  V[['alt3']] = asc3 + b_cov * cov_3 + b_acc1 * acc1_3 + b_acc2 * acc2_3 + b_bid * bid_3
  
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(alt1=1,alt2=2,alt3=3),
    avail         = list(alt1=1, alt2=1,alt3=1),
    choiceVar     = choice,
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, 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)

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

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

apollo_modelOutput(model)

Attachments
RT_setall.rar
(6.6 KiB) Downloaded 386 times
stephanehess
Site Admin
Posts: 1042
Joined: 24 Apr 2020, 16:29

Re: An error when using Mixed Logit model

Post by stephanehess »

Hi

can you please report your estimate, otherwise we cannot help diagnose the problem.

Similarly, when you say some errors are reported, you need to be more specific, and copy them here

Thanks

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