Page 1 of 1

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

Posted: 24 Jan 2023, 10:55
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

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

Posted: 02 Feb 2023, 10:53
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

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

Posted: 02 Feb 2023, 14:07
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

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

Posted: 02 Feb 2023, 14:14
by stephanehess
can you share the model and data with me and I'll find the problem?

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

Posted: 03 Feb 2023, 14:01
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.

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

Posted: 28 Feb 2023, 21:53
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)
}

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

Posted: 01 Mar 2023, 13:20
by Shan
Hi David,

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

Best,
Shan