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.

ChoiceVar

Ask questions about data format and processing of data, including the use of pre-estimation functions in Apollo. If your question relates to a specific error you are getting, please provide some of the output.
Post Reply
chtoune_63
Posts: 1
Joined: 28 Jun 2021, 16:45

ChoiceVar

Post by chtoune_63 »

Hey,
I'm working on a very simple dataset to test Apollo. I get the following error message:

Error in apollo_preprocess(inputs = mnl_settings, modelType, functionality, :
The "choiceVar" argument for model component "model" needs to be a scalar or a vector with one entry per observation in the "database"

I checked the following:
- I have no missing variable for my variable "choice";
- this variable takes three values (1/2/3) as specified in the "alternatives" parameter
- I have one row for each choice question(I used the same data entry as in Biogeme)
- I have no error in the apollo_validateInputs() step

Any idea why this error occurs?
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: ChoiceVar

Post by stephanehess »

Hi

to allow us to help you, please show us your whole model code, along with the output

Thanks

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Finley
Posts: 4
Joined: 26 Jul 2021, 07:21

Re: ChoiceVar

Post by Finley »

The same error also occurs to me.
The code and the model are as follows.

Code: Select all

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

### Load libraries
library(apollo)
#> Apollo 0.2.4
#> www.ApolloChoiceModelling.com
#> See url for a detailed manual, examples and a help forum.
#> Sign up to our mailing list for updates on new releases.

### Initialise code
apollo_initialise()

### Set core controls
apollo_control <- list(
  modelName = "MNL",
  modelDescr = "Adults' preferences for a vaccine product",
  indivID = "ID"
)
# ####################################################### #
#### 2. Data loading                                   ####
# ####################################################### #

database <- subset(model, select = c("ID","price.1","risk2.1","risk3.1","duration2.1",
                                     "duration3.1","efficacy2.1","efficacy3.1",
                                     "admin.1","doses2.1","doses3.1","origin.1",
                                     "price.2","risk2.2","risk3.2",
                                     "duration2.2","duration3.2","efficacy2.2",
                                     "efficacy3.2","admin.2","doses2.2",
                                     "doses3.2","origin.2","choice"))

database <- as.data.frame(database)

# ####################################################### #
#### 3. Parameter definition                           ####
# ####################################################### #

### Vector of parameters, including any that are kept fixed 
### during estimation

apollo_beta <- c(
  asc     = 0,
  b_price  = 0,
  b_risk2  = 0,
  b_risk3  = 0,
  b_duration2  = 0,
  b_duration3 = 0,
  b_efficacy2 = 0,
  b_efficacy3 = 0,
  b_oral = 0,
  b_dose2 = 0,
  b_dose3 = 0,
  b_imported = 0
)

### Vector with names (in quotes) of parameters to be
###  kept fixed at their starting value in apollo_beta.
### Use apollo_beta_fixed = c() for no fixed parameters.

apollo_fixed <- c() # no fixed parameters in our model

# ####################################################### #
#### 4. Input validation                               ####
# ####################################################### #

apollo_inputs = apollo_validateInputs()

# ####################################################### #
#### 5. Likelihood definition                          ####
# ####################################################### #

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()
  
  V <- list()
  
  ### List of utilities: these must use the same names as
  ### in mnl_settings, order is irrelevant.
  V[['alt1']] = asc + b_price*price.1 + b_risk2*risk2.1 + b_risk3*risk3.1 + 
    b_duration2*duration2.1 + b_duration3*duration3.1 + b_efficacy2*efficacy2.1 + 
    b_efficacy3*efficacy3.1 + b_oral*admin.1 + b_dose2*doses2.1 + 
    b_dose3*doses3.1 + b_imported*origin.1
  V[['alt2']] = asc + b_price*price.2 + b_risk2*risk2.2 + b_risk3*risk3.2 + 
    b_duration2*duration2.2 + b_duration3*duration3.2 + b_efficacy2*efficacy2.2 + 
    b_efficacy3*efficacy3.2 + b_oral*admin.2 + b_dose2*doses2.2 + 
    b_dose3*doses3.2 + b_imported*origin.2
  V[['alt3']] = 0
  
  ### Define settings for MNL model component
  mnl_settings <- list(
    alternatives = c(alt1 = 1, alt2 = 2, alt3 = 3),
    avail        = list(alt1 = 1, alt2 = 1, alt3 = 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 the function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  
  return(P)
}

# ####################################################### #
#### 6. Model estimation and reporting                 ####
# ####################################################### #
fit <- apollo_estimate(apollo_beta, apollo_fixed, 
                       apollo_probabilities, apollo_inputs)

apollo_modelOutput(fit, list(printPVal = TRUE))
Actually, the code ran successfully in May this year, but it doesn't work with the same data now. So I am sure that the simple data is qualified for the discrete choice model. I have referred to the source code of `apollo_preprocess.R` but didn't find the same error message like "The "choiceVar" argument for model component "model" needs to be a scalar or a vector with one entry per observation in the 'database'".
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: ChoiceVar

Post by stephanehess »

can you send us the output of

table(database$choice)
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply