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.

How to cluster standard errors at individual level

Ask questions about how to estimate models and how to change your settings for estimation.
Post Reply
zx9203
Posts: 24
Joined: 13 Jun 2020, 09:52

How to cluster standard errors at individual level

Post by zx9203 »

My dataset contains 189 individuals' choice over 4 options (including non-purchase option) in each of the 24 scenarios.
After running a pooled MNL model, I would like to see how big the confidence intervals of parameters would become if I cluster the standard errors at the level of the individual.
How shall I do this in R? Do you have a specific syntax for clustering in Appollo package?

currently, here are my codes:


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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="2nd_pool",
modelDescr ="MNL model on SP choice data",
indivID ="id"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("I:/study/first_project/data/Pilot/project_1 pilot_2/cleaned data for project1pilot2.csv",header=TRUE)
# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #

choiceAnalysis_settings <- list(
alternatives = c(option_1=1, option_2=2, option_3=3, non_purchase=4),
avail = 1,
choiceVar = database$choice,
explanators = database[,c("age","female","income","edu","marry")]
)

apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_non_purchase = 0,
b_price =0,
b_free =0,
b_star =0,
b_safe =0,
b_wellknown =0,
b_loc =0,
b_clean =0,
b_noise =0,
b_pet =0,
b_rev =0,
b_q1 =0,
b_q2 =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 THE DRAWS ####
# ################################################################# #

apollo_draws=list(
interDrawsType="halton",
interNDraws=500
)

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

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['option_1']] = b_price * price_1 + b_free * free_1 + b_star * star_1 + b_safe * safe_1 + b_wellknown * wellknown_1 + b_loc * loc_1 + b_clean * clean_1 + b_noise * noise_1 + b_pet * pet_1 + b_rev * rev_1 + b_q1 * q1 + b_q2 * q2
V[['option_2']] = b_price * price_2 + b_free * free_2 + b_star * star_2 + b_safe * safe_2 + b_wellknown * wellknown_2 + b_loc * loc_2 + b_clean * clean_2 + b_noise * noise_2 + b_pet * pet_2 + b_rev * rev_2 + b_q1 * q1 + b_q2 * q2
V[['option_3']] = b_price * price_3 + b_free * free_3 + b_star * star_3 + b_safe * safe_3 + b_wellknown * wellknown_3 + b_loc * loc_3 + b_clean * clean_3 + b_noise * noise_3 + b_pet * pet_3 + b_rev * rev_3 + b_q1 * q1 + b_q2 * q2
V[['non-purchase']] = asc_non_purchase

### Define settings for MNL model component
mnl_settings = list(
alternatives = c('option_1'=1, 'option_2'=2, 'option_3'=3, 'non-purchase'=4),
avail = 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)

### 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)

#ci
confint(model)


Thank you very much!!
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: How to cluster standard errors at individual level

Post by stephanehess »

Hi

this happens automatically. Please note this is explained on the website and in the manual. Here is the text from the website:

How can I capture the panel structure of my data in Apollo?
The treatment of panel data depends completely on the model being used. Whenever the data contains multiple choices per individual, the analyst needs to use the function apollo_panelProd to group them together in estimation, except if using the setting apollo_control$panelData=FALSE, in which case the data will be treated as if all observations came from separate individuals. In models without any random heterogeneity, such as MNL, there is no explicit modelling of the correlation across choices for the same individual. All that will happen by using apollo_panelProd is that the calculation of the robust standard errors recognises that the choices come from the same individual. In models with random heterogeneity, such as Mixed Logit, the analyst can more explicitly account for the panel structure, for example by specifying that the heterogeneity in any random taste coefficients is across individuals, not within individuals, and/or by including an explicity pseudo-panel effect error component. These issues are discussed in detail in the manual.

Best wishes
--------------------------------
Stephane Hess
www.stephanehess.me.uk
zx9203
Posts: 24
Joined: 13 Jun 2020, 09:52

Re: How to cluster standard errors at individual level

Post by zx9203 »

Hi Stephane,

Thank you for your answer! This is very clear. Sorry I missed this part in your manual.

Really appreciate it!
Post Reply