Page 1 of 1

Need to multiply observations for same individual?

Posted: 09 Apr 2021, 00:42
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)

Re: Need to multiply observations for same individual?

Posted: 12 Apr 2021, 16:35
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