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.

Error: some utilities for component "MNL" contain values that are not finite numbers

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Shan
Posts: 12
Joined: 14 May 2020, 17:38

Error: some utilities for component "MNL" contain values that are not finite numbers

Post by Shan »

Dear Sir,

I hope that you are doing well. I tried to estimate a Mixed logit model, and the results were good when using only 100 Halton draws. However, an error was reported when the draws were raised to 1000, which is in apollo_mnl (mnl_setting, functionality): some utilities for component "MNL" contain values that are not finite numbers. My codes are as follows:
------------------------------------------------------------------------------------------------------------------------------------------------
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

rm(list = ls())
library(apollo)
apollo_initialise()

apollo_control = list(
modelName = "Apollo_modechoicedata_MixedLogit-SD",
modelDescr = "MMNL model with Normal distributions for significant parameters from a simple MNL model",
indivID = "ID",
mixing = TRUE,
nCores = 3
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #

database = read.csv("travelmodechoicedata.csv",header=TRUE)

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

### Vector of parameters, including any that are kept fixed in the estimation
apollo_beta = c(asc_car = 0,
asc_bus_base = 0,
asc_bus_shift_female = 0,
asc_metro_base = 0,
asc_metro_shift_female = 0,
b_fuel_CNG = 0,
b_fuel_electricity = 0,
b_access_bus_mu = 0,
b_access_bus_sig = 0,
b_access_metro = 0,
b_freq_bus = 0,
b_freq_metro_fast = 0,
b_freq_metro_moderate = 0,
b_freq_metro_slow = 0,
b_egress_bus = 0,
b_egress_metro_mu = 0,
b_egress_metro_sig = 0,
b_seat_all_bus_mu = 0,
b_seat_all_bus_sig = 0,
b_seat_all_metro_mu = 0,
b_seat_all_metro_sig = 0,
b_seat_half_bus = 0,
b_seat_half_metro = 0,
b_seat_no = 0,
b_tt_car_mu = 0,
b_tt_car_sig = 0,
b_tt_bus_mu = 0,
b_tt_bus_sig = 0,
b_tt_metro_mu = 0,
b_tt_metro_sig = 0,
b_fare_bus_mu = 0,
b_fare_bus_sig = 0,
b_fare_metro_mu = 0,
b_fare_metro_sig = 0,
b_fuelcost = 0,
b_parking_mu = 0,
b_parking_sig = 0)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta
apollo_fixed = c("asc_car", "b_fuel_CNG", "b_freq_metro_slow","b_seat_no")

# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 1000,
interNormDraws= c("draws_access_bus","draws_egress_metro","draws_seat_all_bus","draws_seat_all_metro","draws_tt_car","draws_tt_bus","draws_tt_metro","draws_fare_bus","draws_fare_metro","draws_parking")
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["b_access_bus"]] = b_access_bus_mu + b_access_bus_sig * draws_access_bus
randcoeff[["b_egress_metro"]] = b_egress_metro_mu + b_egress_metro_sig * draws_egress_metro
randcoeff[["b_seat_all_bus"]] = b_seat_all_bus_mu + b_seat_all_bus_sig * draws_seat_all_bus
randcoeff[["b_seat_all_metro"]] = b_seat_all_metro_mu + b_seat_all_metro_sig * draws_seat_all_metro
randcoeff[["b_tt_car"]] = b_tt_car_mu + b_tt_car_sig * draws_tt_car
randcoeff[["b_tt_bus"]] = b_tt_bus_mu + b_tt_bus_sig * draws_tt_bus
randcoeff[["b_tt_metro"]] = b_tt_metro_mu + b_tt_metro_sig * draws_tt_metro
randcoeff[["b_fare_bus"]] = b_fare_bus_mu + b_fare_bus_sig * draws_fare_bus
randcoeff[["b_fare_metro"]] = b_fare_metro_mu + b_fare_metro_sig * draws_fare_metro
randcoeff[["b_parking"]] = b_parking_mu + b_parking_sig * draws_parking

return(randcoeff)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){

### Function initialisation: do not change the following three commands
### 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()

### Create coefficients with interactions with socio-demographic
asc_bus = asc_bus_base + asc_bus_shift_female * female
asc_metro = asc_metro_base + asc_metro_shift_female * female

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[[ 'car' ]] = ( asc_car + b_tt_car*ttime_car + b_fuelcost*fuelcost_car + b_parking*parking_car)
V[[ 'bus' ]] = ( asc_bus
+ b_fuel_CNG*(fuel_bus=="CNG") + b_fuel_electricity*(fuel_bus=="Electricity")
+ b_access_bus*access_bus
+ b_freq_bus*freq_bus
+ b_egress_bus*egress_bus
+ b_seat_no*(seat_bus=="You have no seat all your trip") + b_seat_half_bus*(seat_bus=="You have a
seat half of your trip") + b_seat_all_bus*(seat_bus=="You have a seat all your trip")
+ b_tt_bus*ttime_bus
+ b_fare_bus*fare_bus)
V[[ 'metro' ]] = ( asc_metro
+ b_access_metro*access_metro
+ b_freq_metro_slow*(freq_metro=="Every 4 min") + b_freq_metro_moderate*(freq_metro=="Every 3
min") + b_freq_metro_fast*(freq_metro=="Every 2 min")
+ b_egress_metro*egress_metro
+ b_seat_no*(seat_metro=="You have no seat all your trip") + b_seat_half_metro*(seat_metro=="You
have a seat half of your trip") + b_seat_all_metro*(seat_metro=="You have a seat all your trip")
+ b_tt_metro*ttime_metro
+ b_fare_metro*fare_metro)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(car=1, bus=2, metro=3),
avail = 1,
choiceVar = selectedchoice,
V = 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)

### Average across inter-individual draws
P = apollo_avgInterDraws(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, estimate_settings=list(hessianRoutine="maxLik"))

# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

apollo_modelOutput(model)
-------------------------------------------------------------------------------------------------------------------------------------------
Thank you for your help!

Kind regards,
Shan
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by stephanehess »

Shan

first, you really should not use Halton draws when you have more than 5 random coefficients. Please use MLHS or some other type of draws.

Regarding your error, does this happen immediately at the starting values?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Shan
Posts: 12
Joined: 14 May 2020, 17:38

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by Shan »

Hi Stephane,

Thank you for your reply. I have changed the random draws from "Halton" to "mlhs". However, I still received this error report. I believe the error was reported at the beginning: right after "the Preparing user-defined functions. Testing likelihood function...".

Regards,
Shan
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by stephanehess »

can you share the model and data with me and I'll find the problem?
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Shan
Posts: 12
Joined: 14 May 2020, 17:38

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by Shan »

Thank you for your help. Sorry, I cannot provide the data without permission since the copyright belongs to my university. I deleted two alternative-specific constants, namely, asc_bus_shift_female and asc_metro_shift_female. Then, my model could run successfully, which is weird.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by dpalma »

Hi Shan,

I can't be sure, as I can't run your model, but I have the impression that the definion of dummy variables for some seat levels was not working, as you had line feeds inside the definition of a string (a character variable). Please try using the following apollo_probabilities and see what happens.

Also, in general it is preferable to define dummy variables such as (fuel_bus=="Electricity") before estimation and store them in the database, rather than do it inside apollo_probabilities.

Cheers
David

Code: Select all

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Function initialisation: do not change the following three commands
  ### 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()
  
  ### Create coefficients with interactions with socio-demographic
  asc_bus = asc_bus_base + asc_bus_shift_female * female
  asc_metro = asc_metro_base + asc_metro_shift_female * female
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[[ 'car' ]] = ( asc_car + b_tt_car*ttime_car + b_fuelcost*fuelcost_car + b_parking*parking_car)
  V[[ 'bus' ]] = ( asc_bus
                   + b_fuel_CNG*(fuel_bus=="CNG") + b_fuel_electricity*(fuel_bus=="Electricity")
                   + b_access_bus*access_bus
                   + b_freq_bus*freq_bus
                   + b_egress_bus*egress_bus
                   + b_seat_no*(seat_bus=="You have no seat all your trip") 
                   + b_seat_half_bus*(seat_bus=="You have a seat half of your trip") 
                   + b_seat_all_bus*(seat_bus=="You have a seat all your trip")
                   + b_tt_bus*ttime_bus
                   + b_fare_bus*fare_bus)
  V[[ 'metro' ]] = ( asc_metro
                     + b_access_metro*access_metro
                     + b_freq_metro_slow*(freq_metro=="Every 4 min") 
                     + b_freq_metro_moderate*(freq_metro=="Every 3 min") 
                     + b_freq_metro_fast*(freq_metro=="Every 2 min")
                     + b_egress_metro*egress_metro
                     + b_seat_no*(seat_metro=="You have no seat all your trip") 
                     + b_seat_half_metro*(seat_metro=="You have a seat half of your trip")
                     + b_seat_all_metro*(seat_metro=="You have a seat all your trip")
                     + b_tt_metro*ttime_metro
                     + b_fare_metro*fare_metro)
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives = c(car=1, bus=2, metro=3),
    avail = 1,
    choiceVar = selectedchoice,
    V = 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)
  
  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}
Shan
Posts: 12
Joined: 14 May 2020, 17:38

Re: Error: some utilities for component "MNL" contain values that are not finite numbers

Post by Shan »

Hi David,

Many thanks for your reply. I will try it soon.

Best,
Shan
Post Reply