How to cluster standard errors at individual level
Posted: 13 Jun 2020, 10:08
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!!
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!!