bug in MNL with covariates
Posted: 11 May 2022, 11:29
Hi,
I am new to Apollo and are trying to make an MNL model with socio-demographics. I always end up with an error. I have a simple DCE data with 8 questions per respondent and are trying to implement "female" dummy variable to the model.
Here is my R code:
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MNL_SP_covariates",
modelDescr = "MNL model with socio-demographics on mode choice SP data",
indivID = "ID",
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 = ne1
#### ANALYSIS OF CHOICES
### Define settings for analysis of choice data to be conducted prior to model estimation
choiceAnalysis_settings <- list(
alternatives = c(airplane=1, train=2, home=3),
avail = 1,
choiceVar = database$choice,
explanators = database[,c("female")])
### Run function to analyse choice data
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
#### DEFINE MODEL PARAMETERS
### Vector of parameters, including any that are kept fixed in estimation
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_plane = 0,
asc_train = 0,
b_female = 0,
b_tt = 0,
b_cost = 0)
### 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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["airplane"]] = asc_plane + b_tt * t1 + b_cost * c1 + b_female * female
V[["train"]] = asc_train + b_tt * t2 + b_cost * c2 + b_female * female
V[["home"]] = 0
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(airplane=1, train=2, home=3),
avail = 1,
choiceVar = choice,
utilities = 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 function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
#### MODEL ESTIMATION
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
And I end up with this error message:
Error in apollo_validate(mnl_settings, modelType, functionality, apollo_inputs) :
Some utilities for model component "MNL" contain values that are not finite numbers!
I am new to Apollo and are trying to make an MNL model with socio-demographics. I always end up with an error. I have a simple DCE data with 8 questions per respondent and are trying to implement "female" dummy variable to the model.
Here is my R code:
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MNL_SP_covariates",
modelDescr = "MNL model with socio-demographics on mode choice SP data",
indivID = "ID",
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 = ne1
#### ANALYSIS OF CHOICES
### Define settings for analysis of choice data to be conducted prior to model estimation
choiceAnalysis_settings <- list(
alternatives = c(airplane=1, train=2, home=3),
avail = 1,
choiceVar = database$choice,
explanators = database[,c("female")])
### Run function to analyse choice data
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
#### DEFINE MODEL PARAMETERS
### Vector of parameters, including any that are kept fixed in estimation
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_plane = 0,
asc_train = 0,
b_female = 0,
b_tt = 0,
b_cost = 0)
### 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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["airplane"]] = asc_plane + b_tt * t1 + b_cost * c1 + b_female * female
V[["train"]] = asc_train + b_tt * t2 + b_cost * c2 + b_female * female
V[["home"]] = 0
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(airplane=1, train=2, home=3),
avail = 1,
choiceVar = choice,
utilities = 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 function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
#### MODEL ESTIMATION
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
And I end up with this error message:
Error in apollo_validate(mnl_settings, modelType, functionality, apollo_inputs) :
Some utilities for model component "MNL" contain values that are not finite numbers!