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.

Need to multiply observations for same individual?

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
gregorymacfarlane
Posts: 11
Joined: 29 Oct 2020, 23:11
Contact:

Need to multiply observations for same individual?

Post by gregorymacfarlane »

I'm in my first three hours of using apollo (outside of the examples). I'm trying to use it to estimate the following model from the mlogit package:

Code: Select all

library(mlogit)
data("TravelMode", package = "AER")
mlogit(choice ~ wait + vcost | size, TravelMode)
I believe that I have finally worked through everything to specify the model correctly and format the data, after going through the manual and trying to replicate the basic example. However, I get an error that I need to consider panel effects:

Code: Select all

Error in apollo_prepareProb(P, apollo_inputs, functionality) : 
  Need to multiply observations for the same individual! (see ?apollo_panelProd)
The data has one row per observation, with no repeated or panel information, nor did I specify any. I removed the apollo_panelProd call from my probability function as recommended, but now apollo_prepareProb doesn't know what to do with the vector. I note that in the MNL example, the apollo_validateInputs() function recognized that there was panel data without any input; it seems strange to assume something rather than require us to specify it. Regardless, how do I unspecify it? Is that even what the issue is?

When I try to debug the probabilities function, the P that gets put into apollo_prepareProb is in fact a column vector with the number of elements matching the total number of observations in the data.

My full code is below:

Code: Select all

library(apollo)
library(tidyverse)

# Munge data into "wide" format required by apollo =========
data("TravelMode", package = "AER")
database <- left_join(
  # get the choice and the generic variables
  TravelMode %>% tibble() %>%
    mutate(mode = as.character(mode),
           choice = ifelse(choice == "yes", mode, NA)) %>%
    group_by(individual) %>%
    summarise(choice = mode[!is.na(choice)], income = income[1], size = size[1]),
  # pivot into wider
  TravelMode %>% tibble() %>%
    pivot_wider(id_cols = individual, names_from = mode, values_from  = c(wait, vcost, travel, gcost)),
  by = "individual"
) %>%
  rename(ID = individual) %>%
  mutate(ID = as.character(ID))

## Configure apollo run =========
apollo_initialise()
apollo_control <- list(
  modelName = "MyModel",
  modelDescr = "Basic",
  indivID = "ID"
)

choiceAnalysis_settings <- list(
  alternatives = c(car = "car", bus = "bus", air = "air", train = "train"),
  choiceVar = database$choice,
  avail = 1,
  explanators = database[, c("income", "size")],
  rows = rep(TRUE, nrow(database))
)

# create parameters
apollo_beta <- c(
  asc_car = 0, asc_bus = 0, asc_train = 0, asc_air = 0,
  b_size_bus = 0, b_size_train = 0, b_size_air = 0,
  b_wait = 0,
  b_vcost= 0
)
apollo_fixed = c("asc_car")
apollo_inputs <- apollo_validateInputs(silent = TRUE)


# MNL 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[['car']]   = asc_car   + b_wait * wait_car   + b_vcost * vcost_car   
  V[['bus']]   = asc_bus   + b_wait * wait_bus   + b_vcost * vcost_bus   + b_size_bus * size
  V[['air']]   = asc_air   + b_wait * wait_air   + b_vcost * vcost_air   + b_size_air * size
  V[['train']] = asc_train + b_wait * wait_train + b_vcost * vcost_train + b_size_train * size
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives = c(car = "car", bus = "bus", air = "air", train = "train"),
    avail         = 1,
    choiceVar     = choice,
    V             = V
  )
  
  ### Compute probabilities using MNL model
  P[['model']] = apollo_mnl(mnl_settings, functionality)
  
  ## SHOULDN'T NEED TO DO PANEL MULTIPLICATION: ONE ROW PER OBSERVATION
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

## Error in apollo_prepareProb(P, apollo_inputs, functionality) : 
##  Need to multiply observations for the same individual! (see ?apollo_panelProd)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Need to multiply observations for same individual?

Post by stephanehess »

Hi

by default, Apollo works with the likelihood at the person rather than observation level. That's why you need to multiply together observations for the same individual. This does not affect your model estimates but means that the calculation of the robust standard errors recognises that multiple observations come from the same individual. Otherwise, you likely underestimate the standard errors.

I can't really see any good reason for not wanting to do this, but if you want to exclude it, you can include panelData = FALSE in apollo_control and then drop the multiplication

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply