Page 1 of 1

Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) : object 'task' not found

Posted: 19 Apr 2022, 15:57
by victornielsen
Hello!

I am trying to estimate an ordinal logistic model

I copied everything from this example: http://apollochoicemodelling.com/files/ ... ating/OL.r except for the data.

I made my changes to make it match my data. I made dummies for all variables and excluded one variable.
For the explained variable I keep all dummies (the tau ones). Code is found below.

I get the following error:

Code: Select all

> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) : 
  object 'task' not found
Not sure what causes that error

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       = "OL",
  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                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(beta_geslacht_vrouw               = 0, 
                beta_huishoudensinkomen_980_1870  = 0, 
                beta_huishoudensinkomen_1870_2680 = 0, 
                beta_huishoudensinkomen_2680_3800 = 0, 
                beta_huishoudensinkomen_3800_5460 = 0, 
                beta_huishoudensinkomen_5460_     = 0, 
                beta_opleiding_basis              = 0, 
                beta_opleiding_middelbare         = 0, 
                beta_opleiding_MBO                = 0, 
                beta_opleiding_HBO                = 0, 
                beta_opleiding_WO                 = 0, 
                tau_nautos_change_1               = -1,
                tau_nautos_change_2               = 0,
                tau_nautos_change_3               = 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_geslacht_vrouw*geslacht_vrouw + 
                       beta_huishoudensinkomen_980_1870*huishoudensinkomen_980_1870 + 
                       beta_huishoudensinkomen_1870_2680*huishoudensinkomen_1870_2680 +
                       beta_huishoudensinkomen_2680_3800*huishoudensinkomen_2680_3800 + 
                       beta_huishoudensinkomen_3800_5460*huishoudensinkomen_3800_5460 + 
                       beta_huishoudensinkomen_5460_*huishoudensinkomen_5460_ +
                       beta_opleiding_basis*opleiding_basis + beta_opleiding_middelbare*opleiding_middelbare +
                       beta_opleiding_MBO*opleiding_MBO + beta_opleiding_HBO*opleiding_HBO +
                       beta_opleiding_WO*opleiding_WO + beta_opleiding_WO*opleiding_WO, 
                     tau            = list(tau_nautos_change_1, tau_nautos_change_2, tau_nautos_change_3),
                     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_probabilities(apollo_beta_shifted, apollo_inputs) : object 'task' not found

Posted: 21 Apr 2022, 10:22
by stephanehess
Hi

the error message tells you that the variable task does not exist in your data. This is used in our data to make sure the ordered model is only used for the first task for each person. If it doesn't apply in your case, you'll need to remove that line of code. There are details in the manual too

Stephane

Re: Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) : object 'task' not found

Posted: 26 Apr 2022, 08:44
by victornielsen
Thank you for your reply. I am not quite following though. What is the variable task?

Re: Error in apollo_probabilities(apollo_beta_shifted, apollo_inputs) : object 'task' not found

Posted: 26 Apr 2022, 23:19
by stephanehess
Hi

like I said, the details are in the manual, as well as in the data dictionary. See http://apollochoicemodelling.com/files/ ... ionary.pdf

Stephane