I have stated choice experiement data. It is unlabelled experiement, including:
- 370 individuals
- 5 attributes: 4 dummy variables (barn, range, organic, vitamin), 1 continuous variable (price)
- 8 choice sets, each including 3 alternatives (alternative 1, alternative 2, opt-out)
I have tried to apply the functions in Apollo package, I struggle to set up the apollo_probabilities in the R code.
Here is my R code:
Code: Select all
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MNL_SP",
modelDescr = "Simple MNL model on mode choice SP data",
indivID = "ID",
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
dtaApollo <- read.xlsx("dtaApollo.xlsx", sheet = "data", colNames = T)
database <- dtaApollo
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_alt3 = 0,
b_no_barn = 0,
b_no_range = 0,
b_no_organic = 0,
b_no_vitamin = 0,
b_barn = 0,
b_range = 0,
b_organic = 0,
b_vitamin = 0,
b_price = 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("b_no_barn","b_no_range","b_no_organic","b_no_vitamin")
# ################################################################# #
#### 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[["alt1"]] = b_no_barn * (barn_alt1 == 0) + b_no_range * (range_alt1 == 0) + b_no_organic * (organic_alt1 == 0) + b_no_vitamin * (vitamin_alt1 == 0) + b_range * (range_alt1 == 1) + b_organic * (organic_alt1 == 1) + b_vitamin * (vitamin_alt1 == 1) + b_price * price_alt1
V[["alt2"]] = b_no_barn * (barn_alt2 == 0) + b_no_range * (range_alt2 == 0) + b_no_organic * (organic_alt2 == 0) + b_no_vitamin * (vitamin_alt2 == 0) + b_range * (range_alt2 == 1) + b_organic * (organic_alt2 == 1) + b_vitamin * (vitamin_alt2 == 1) + b_price * price_alt1
V[["alt3"]] = asc_alt3 + b_no_barn * (barn_alt3 == 0) + b_no_range * (range_alt3 == 0) + b_no_organic * (organic_alt3 == 0) + b_no_vitamin * (vitamin_alt3 == 0) + b_range * (range_alt3 == 1) + b_organic * (organic_alt3 == 1) + b_vitamin * (vitamin_alt3 == 1) + b_price * price_alt1
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3),
choiceVar = choice,
utilities = 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)
Error in apollo_mnl(c(mnl_settings, componentName2 = "model"), functionality) :
SPECIFICATION ISSUE - Only one alternative is available for each observation for model component "model!
Here are first lines of my data:
Could you please give me some instruction?
Thanks very much.
Cuong