Page 1 of 1

Simultaneous BW

Posted: 03 Feb 2022, 22:18
by marah
Hi Stephane,

You've been helping me code the simultaneous BW.

After making the changes you suggested, it still doesn't work.

The error code is now:
Error in list(alternatives = c(alt_b1_w2 = 12, alt_b1_w3 = 13, alt_b1_w4 = 14, :
argument 4 is empty

I'm not sure what that means?

Thanks and regards,

Mara

The code is:

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 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("C:/Users/marah/Australian National University/ECA application - Documents/Estimation/Apollo BW/apollobwexcel.xlsx")
#database = read_excel("apollobwexcel.xlsx")
#database = read_excel("apollobwexcel.xlsx")
#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)                               ----
  # ----------------------------------------------------------------- #
  
  apollo_modelOutput(model)
  
  # ----------------------------------------------------------------- #
  #---- FORMATTED OUTPUT (TO FILE, using model name)               ----
  # ----------------------------------------------------------------- #
  
  apollo_saveOutput(model)
  

Re: Simultaneous BW

Posted: 03 Feb 2022, 22:31
by stephanehess
Hi Mara

when R says "argument 4 is empty", this relates not to the 4th alternative, but to the 4th element in your list.

You have alternatives, then choiceVar, then utilities, but then you had a stray comma after the utilities, so R is waiting for the 4th argument. If you remove that comma, it all runs fine

Stephane