Thanks, that makes sense. I'm quite sure I did use all the possible best-worst combination as alternatives.
I ran the following codes for my main table of BWS results:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Apollo BW simultaneous",
modelDescr = "Best-worst model simultaneous",
indivID = "id",
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
library(readxl)
database = read_excel("...")
#View(database)
#attach(database)
### for data dictionary, use ?apollo_drugChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(bafford = 0,
bpayback = 0,
bmargins = 0,
brentalmarket = 0,
bsavings = 0,
brent = 0,
bresale = 0,
bnoinstall = 0,
btime = 0,
bnoconsider = 0,
bgov = 0,
asc_alt1 = 0,
asc_alt2 = 0,
asc_alt3 = 0,
asc_alt4 = 0,
asc_alt5 = 0,
mu_worst = 1)
### 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("asc_alt5","bnoconsider")
# ################################################################# #
#### 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"]] = ( asc_alt1 + bafford*afford1 + bpayback*payback1 + bmargins*margins1 + brentalmarket*rentalmarket1 + bsavings*savings1 + brent*rent1 + bresale*resale1 + bnoinstall*noinstall1 + btime*time1 + bnoconsider*noconsider1 + bgov*gov1)
V[["alt2"]] = ( asc_alt2 + bafford*afford2 + bpayback*payback2 + bmargins*margins2 + brentalmarket*rentalmarket2 + bsavings*savings2 + brent*rent2 + bresale*resale2 + bnoinstall*noinstall2 + btime*time2 + bnoconsider*noconsider2 + bgov*gov2)
V[["alt3"]] = ( asc_alt3 + bafford*afford3 + bpayback*payback3 + bmargins*margins3 + brentalmarket*rentalmarket3 + bsavings*savings3 + brent*rent3 + bresale*resale3 + bnoinstall*noinstall3 + btime*time3 + bnoconsider*noconsider3 + bgov*gov3)
V[["alt4"]] = ( asc_alt4 + bafford*afford4 + bpayback*payback4 + bmargins*margins4 + brentalmarket*rentalmarket4 + bsavings*savings4 + brent*rent4 + bresale*resale4 + bnoinstall*noinstall4 + btime*time4 + bnoconsider*noconsider4 + bgov*gov4)
V[["alt5"]] = ( asc_alt5 + bafford*afford5 + bpayback*payback5 + bmargins*margins5 + brentalmarket*rentalmarket5 + bsavings*savings5 + brent*rent5 + bresale*resale5 + bnoinstall*noinstall5 + btime*time5 + bnoconsider*noconsider5 + bgov*gov5)
### Compute probabilities for combined "best" and "worst" choice using MNL model
mnl_settings = list(
alternatives = c(alt_b1_w2=12,
alt_b1_w3=13,
alt_b1_w4=14,
alt_b1_w5=15,
alt_b2_w1=21,
alt_b2_w3=23,
alt_b2_w4=24,
alt_b2_w5=25,
alt_b3_w1=31,
alt_b3_w2=32,
alt_b3_w4=34,
alt_b3_w5=35,
alt_b4_w1=41,
alt_b4_w2=42,
alt_b4_w3=43,
alt_b4_w5=45,
alt_b5_w1=51,
alt_b5_w2=52,
alt_b5_w3=53,
alt_b5_w4=54),
choiceVar = 10*best1+worst1,
utilities = list(alt_b1_w2=V[["alt1"]]-mu_worst*V[["alt2"]],
alt_b1_w3=V[["alt1"]]-mu_worst*V[["alt3"]],
alt_b1_w4=V[["alt1"]]-mu_worst*V[["alt4"]],
alt_b1_w5=V[["alt1"]]-mu_worst*V[["alt5"]],
alt_b2_w1=V[["alt2"]]-mu_worst*V[["alt1"]],
alt_b2_w3=V[["alt2"]]-mu_worst*V[["alt3"]],
alt_b2_w4=V[["alt2"]]-mu_worst*V[["alt4"]],
alt_b2_w5=V[["alt2"]]-mu_worst*V[["alt5"]],
alt_b3_w1=V[["alt3"]]-mu_worst*V[["alt1"]],
alt_b3_w2=V[["alt3"]]-mu_worst*V[["alt2"]],
alt_b3_w4=V[["alt3"]]-mu_worst*V[["alt4"]],
alt_b3_w5=V[["alt3"]]-mu_worst*V[["alt5"]],
alt_b4_w1=V[["alt4"]]-mu_worst*V[["alt1"]],
alt_b4_w2=V[["alt4"]]-mu_worst*V[["alt2"]],
alt_b4_w3=V[["alt4"]]-mu_worst*V[["alt3"]],
alt_b4_w5=V[["alt4"]]-mu_worst*V[["alt5"]],
alt_b5_w1=V[["alt5"]]-mu_worst*V[["alt1"]],
alt_b5_w2=V[["alt5"]]-mu_worst*V[["alt2"]],
alt_b5_w3=V[["alt5"]]-mu_worst*V[["alt3"]],
alt_b5_w4=V[["alt5"]]-mu_worst*V[["alt4"]])
)
P[["model"]] = apollo_mnl(mnl_settings, functionality)
P=apollo_panelProd(P,apollo_inputs,functionality)
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) ----
# ----------------------------------------------------------------- #
modelOutput_settings = list(printPVal=2)
apollo_modelOutput(model,modelOutput_settings)
deltaMethod_settings=list(expression=c(isdiff = "brent-bafford"))
apollo_deltaMethod(model, deltaMethod_settings)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
saveOutput_settings = list(printPVal=2)
apollo_saveOutput(model, saveOutput_settings)
And:
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Apollo BW separate fixed",
modelDescr = "Best-worst model separate",
indivID = "id",
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)
library(readxl)
database = read_excel("...")
#View(database)
#attach(database)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(bafford = 0,
bpayback = 0,
bmargins = 0,
brentalmarket = 0,
bsavings = 0,
brent = 0,
bresale = 0,
bnoinstall = 0,
btime = 0,
bnoconsider = 0,
bgov = 0,
asc_alt1 = 0,
asc_alt2 = 0,
asc_alt3 = 0,
asc_alt4 = 0,
asc_alt5 = 0,
mu_worst = 1)
### 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("asc_alt5","bnoconsider")
# ################################################################# #
#### 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"]] = asc_alt1 + bafford*afford1 + bpayback*payback1 + bmargins*margins1 + brentalmarket*rentalmarket1 + bsavings*savings1 + brent*rent1 + bresale*resale1 + bnoinstall*noinstall1 + btime*time1 + bnoconsider*noconsider1 + bgov*gov1
V[["alt2"]] = asc_alt2 + bafford*afford2 + bpayback*payback2 + bmargins*margins2 + brentalmarket*rentalmarket2 + bsavings*savings2 + brent*rent2 + bresale*resale2 + bnoinstall*noinstall2 + btime*time2 + bnoconsider*noconsider2 + bgov*gov2
V[["alt3"]] = asc_alt3 + bafford*afford3 + bpayback*payback3 + bmargins*margins3 + brentalmarket*rentalmarket3 + bsavings*savings3 + brent*rent3 + bresale*resale3 + bnoinstall*noinstall3 + btime*time3 + bnoconsider*noconsider3 + bgov*gov3
V[["alt4"]] = asc_alt4 + bafford*afford4 + bpayback*payback4 + bmargins*margins4 + brentalmarket*rentalmarket4 + bsavings*savings4 + brent*rent4 + bresale*resale4 + bnoinstall*noinstall4 + btime*time4 + bnoconsider*noconsider4 + bgov*gov4
V[["alt5"]] = asc_alt5 + bafford*afford5 + bpayback*payback5 + bmargins*margins5 + brentalmarket*rentalmarket5 + bsavings*savings5 + brent*rent5 + bresale*resale5 + bnoinstall*noinstall5 + btime*time5 + bnoconsider*noconsider5 + bgov*gov5
### Compute probabilities for "best" choice using MNL model
mnl_settings_best = list(
alternatives = c(alt1=1, alt2=2, alt3=3, alt4=4, alt5=5),
avail = list(alt1=1, alt2=1, alt3=1, alt4=1, alt5=1),
choiceVar = best1,
utilities = V,
componentName = "best"
)
P[["choice_best"]] = apollo_mnl(mnl_settings_best, functionality)
### Compute probabilities for "worst" choice using MNL model
mnl_settings_best = list(
alternatives = c(alt1=1, alt2=2, alt3=3, alt4=4, alt5=5),
avail = list(alt1=(best1!=1), alt2=(best1!=2), alt3=(best1!=3), alt4=(best1!=4), alt5=(best1!=5)),
choiceVar = worst1,
utilities = list(alt1=-mu_worst*V[["alt1"]],
alt2=-mu_worst*V[["alt2"]],
alt3=-mu_worst*V[["alt3"]],
alt4=-mu_worst*V[["alt4"]],
alt5=-mu_worst*V[["alt5"]]),
componentName = "worst"
)
P[["choice_worst"]] = apollo_mnl(mnl_settings_best, functionality)
P=apollo_combineModels(P,apollo_inputs,functionality)
P=apollo_panelProd(P,apollo_inputs,functionality)
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) ----
# ----------------------------------------------------------------- #
modelOutput_settings = list(printPVal=2)
apollo_modelOutput(model,modelOutput_settings)
deltaMethod_settings=list(expression=c(isdiff = "brent-bafford"))
apollo_deltaMethod(model, deltaMethod_settings)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
saveOutput_settings = list(printPVal=2)
apollo_saveOutput(model, saveOutput_settings)
Thank you!