Page 1 of 1

Error in apollo_preprocess(inputs = ol_settings, modelType, functionality, : The "outcomeOrdered" argument for model co

Posted: 18 Jul 2022, 18:16
by victornielsen
Hello I use this ordered data for an ordered logit model

Code: Select all

 table(database$nautos_change, useNA = "always")

   0    1    2 <NA> 
  50 1281   68    0 
As you can see it has no NAs

Below I estimate the model, but note that after

Code: Select all

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
I get the error:

Code: Select all

Error in apollo_preprocess(inputs = ol_settings, modelType, functionality, : 
The "outcomeOrdered" argument for model component "OL" needs to be a scalar or a vector with one entry per observation in the "database"
I am running out of solutions to this. There are no NAs in the outcomeOrdered argument. They are vectors with one entry per observation.
Please help!


Code: Select all

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
# rm(list = ls())
# s12 <- read_sav("Cleaned data/discretechoice.sav", user_na = FALSE)

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "OLnautos",
  modelDescr      = "Ordered logit model fitted to MOCOLODO questionnaire",
  indivID         = "uuid2",
  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)
database = s12
### for data dictionary, use ?apollo_drugChoiceData

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

#ref cat strategy: don't use one that is too small and think of what you want to explore. Otherwise choose the middle or extremes.

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
                beta_weekdist_woonwerk_changecat_decreasedcommute    = 0,
                beta_weekdist_woonwerk_changecat_increasedcommute    = 0,
                beta_weekdist_woonwerk_changecat_samecommute         = 0,
                #beta_weekdist_woonwerk_changecat_nojob              = 0,
                beta_weekdist_woonwerk_changecat_lostjob             = 0,
                beta_weekdist_woonwerk_changecat_gotjob              = 0,
                beta_weekdist_woonwerk_changecat_alwaysworkfromhome  = 0,
                beta_weekdist_woonwerk_changecat_workfromhometocommuting = 0,
                beta_weekdist_woonwerk_changecat_commutingtoworkfromhome = 0,
                beta_weekdist_woonwerk_changecat_differingaddresses  = 0,
                tau_nautos_change_1                = 0,
                tau_nautos_change_2                = 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()

# ################################################################# #
#### 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()

  ### Calculate probabilities using Ordered Logit model
  ol_settings = list(outcomeOrdered = nautos_change,
                     utility        =
                       beta_weekdist_woonwerk_changecat_decreasedcommute*weekdist_woonwerk_changecat_decreasedcommute +
                       beta_weekdist_woonwerk_changecat_increasedcommute*weekdist_woonwerk_changecat_increasedcommute +
                       beta_weekdist_woonwerk_changecat_samecommute*weekdist_woonwerk_changecat_samecommute +
                       #beta_weekdist_woonwerk_changecat_nojob*weekdist_woonwerk_changecat_nojob +
                       beta_weekdist_woonwerk_changecat_lostjob*weekdist_woonwerk_changecat_lostjob +
                       beta_weekdist_woonwerk_changecat_gotjob*weekdist_woonwerk_changecat_gotjob +
                       beta_weekdist_woonwerk_changecat_alwaysworkfromhome*weekdist_woonwerk_changecat_alwaysworkfromhome +
                       beta_weekdist_woonwerk_changecat_workfromhometocommuting*weekdist_woonwerk_changecat_workfromhometocommuting +
                       beta_weekdist_woonwerk_changecat_commutingtoworkfromhome*weekdist_woonwerk_changecat_commutingtoworkfromhome +
                       beta_weekdist_woonwerk_changecat_differingaddresses*weekdist_woonwerk_changecat_differingaddresses,
                       tau             = list(tau_nautos_change_1, tau_nautos_change_2))
                       #rows           = (task==1))

  P[["model"]] = apollo_ol(ol_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)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

#### ####

Re: Error in apollo_preprocess(inputs = ol_settings, modelType, functionality, : The "outcomeOrdered" argument for mode

Posted: 19 Jul 2022, 09:48
by dpalma
Hi,

Is the variable nautos_change a factor? Apollo does not support factors, and needs all variables to be coded as numeric. To check if your variable is a factor, you can do:

Code: Select all

is.factor(database$nautos_change)
If so, please turn it into a numeric.

Best wishes
David

Re: Error in apollo_preprocess(inputs = ol_settings, modelType, functionality, : The "outcomeOrdered" argument for mode

Posted: 21 Jul 2022, 12:24
by victornielsen
It worked to save (and later read) the file as csv instead of sav.