Page 1 of 1

An error when using Mixed Logit model

Posted: 21 Mar 2021, 19:43
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)

Re: An error when using Mixed Logit model

Posted: 22 Mar 2021, 12:25
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

Re: An error when using Mixed Logit model

Posted: 22 Mar 2021, 20:48
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)


Re: An error when using Mixed Logit model

Posted: 25 Mar 2021, 14:05
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