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.

In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Ask questions about the results reported after estimation. If the output includes errors, please include your model code if possible.
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Hello,

I wanted to do a multinomial logit model for travel mode choice on my dataset.

1- When I want to estimate alternate specific constants, the results are strange and the errors are too big.

2-Also, the sign of the beta parameter for the "bus_cost" variable is positive. But a positive sign does not make sense, since as the price of the bus increases, the utility of it should decrease. The standard error of this variable is also too big.

I would be so thankful if you could help me with these issues.

rm(list = ls())
library(tidyverse)
library(apollo)
library(haven)
apollo_initialise()
apollo_control = list(
modelName ="Model_MNL",
modelDescr ="Mode",
indivID ="id",
)
tbl <- read_csv("Travel_Mode.csv")

database <- as.data.frame(tbl)

apollo_beta = c(asc_DR = 0, asc_RI = 0, asc_WA= 0, asc_BUS = 0,A_drive_time=0, B_ride_time=0,A_Car=0, A_driving_Freq=0, C_walk_time=0, C_disability=0, C_accessibility=0, D_bus_cost=0 ,D_bus_time=0)
apollo_fixed = c()
apollo_inputs = apollo_validateInputs()

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
V = list()
V[["DR"]] = asc_DR+A_drive_time * drive_time + A_driving_Freq * driving_Freq+A_Car*Car
V[["RI"]] = asc_RI+B_ride_time * ride_time
V[["WA"]] = asc_WA+ C_walk_time*walk_time+ C_disability*disability+ C_accessibility*accessibility
V[["BUS"]] =asc_BUS+D_bus_cost* bus_cost+ C_disability*disability+ D_bus_time*bus_time
mnl_settings = list(
alternatives = c(DR = 1, RI = 2, WA = 3, BUS=4),
avail = list(DR=av_drive, RI=av_ride, WA=av_walk, BUS=av_bus),
choiceVar = CHOICE,
V = V)
P = list()
P[['model']] = apollo_mnl(mnl_settings, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
model = apollo_estimate(apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs)
apollo_modelOutput(model) Also, the following is the result of the code: Testing likelihood function...

Overview of choices for MNL model component :
DR RI WA BUS
Times available 112.00 153.00 153.0 132.00
Times chosen 81.00 39.00 13.0 20.00
Percentage chosen overall 52.94 25.49 8.5 13.07
Percentage chosen when available 72.32 25.49 8.5 15.15

Pre-processing likelihood function...

Testing influence of parameters.............
Starting main estimation
Initial function value: -193.9134
Initial gradient value:
asc_DR asc_RI asc_WA asc_BUS A_drive_time B_ride_time A_Car A_driving_Freq C_walk_time C_disability
51.500000 -4.666667 -30.666667 -16.166667 492.833333 8.166667 54.500000 106.500000 -2953.583333 -20.166667
C_accessibility D_bus_cost D_bus_time
-10.916667 -19.362500 -985.250000
initial value 193.913400
iter 2 value 160.614390
iter 3 value 155.379730
iter 4 value 152.266863
iter 5 value 135.999335
iter 6 value 130.210627
iter 7 value 99.115257
iter 8 value 97.028935
iter 9 value 89.862081
iter 10 value 88.633905
iter 11 value 85.650116
iter 12 value 83.836349
iter 13 value 79.105769
iter 14 value 78.484469
iter 15 value 77.885079
iter 16 value 77.594341
iter 17 value 77.393871
iter 18 value 77.375799
iter 19 value 77.372847
iter 20 value 77.372618
iter 21 value 77.371617
iter 22 value 77.370698
iter 23 value 77.370255
iter 24 value 77.370152
iter 25 value 77.370134
iter 26 value 77.370130
iter 26 value 77.370130
iter 26 value 77.370130
final value 77.370130
converged
Additional convergence test using scaled estimation. Parameters will be scaled by their current estimates and additional iterations will be
performed.
initial value 77.370130
iter 1 value 77.370130
final value 77.370130
converged
Estimated parameters:
Estimate
asc_DR -2.24667
asc_RI 1.27761
asc_WA 2.58634
asc_BUS -1.61726
A_drive_time -0.08235
B_ride_time -0.14189
A_Car 1.31058
A_driving_Freq 2.90470
C_walk_time -0.09854
C_disability -0.45844
C_accessibility 0.29397
D_bus_cost 3.43610
D_bus_time -0.08302

Computing covariance matrix using analytical gradient.
0%....25%....50%....75%..100%
WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle point!
Computing score matrix...
Calculating LL(0) for applicable models...
Calculating LL(c) for applicable models...
Calculating LL of each model component...
Warning messages:
1: In sqrt(diag(varcov)) : NaNs produced
2: In sqrt(diag(robvarcov)) : NaNs produced
> apollo_modelOutput(model)
Model run using Apollo for R, version 0.2.6 on Windows by Aria
www.ApolloChoiceModelling.com

Model name : Model_MNL
Model description: mode
Model run at : 2021-11-27 20:16:00
Estimation method: bfgs
Model diagnosis : successful convergence
Number of individuals : 153
Number of rows in database: 153
Number of modelled outcomes: 153

Number of cores used: 1
Model without mixing

LL(start) : -193.9134
LL(0): -193.9134
LL(C) : -135.2653
LL(final) : -77.37013
Rho-square (0): 0.601
Adj.Rho-square (0) : 0.534
Rho-square (C) : 0.428
Adj.Rho-square (C) : 0.3319
AIC: 180.74
BIC : 220.14

Estimated parameters : 13
Time taken (hh:mm:ss): 00:00:0.63
pre-estimation: 00:00:0.32
estimation : 00:00:0.13
post-estimation : 00:00:0.18
Iterations: 29
Min abs eigenvalue of Hessian: 0

Unconstrained optimisation.

Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_DR -2.24667 NaN NaN 1514.64745 -0.001483
asc_RI 1.27761 NaN NaN 857.82632 0.001489
asc_WA 2.58634 NaN NaN NaN NaN
asc_BUS -1.61726 NaN NaN 882.69570 -0.001832

A_drive_time -0.08235 0.13677 -0.6021 0.11548 -0.713120
B_ride_time -0.14189 0.11921 -1.1902 0.11128 -1.275021
A_Car 1.31058 1.01159 1.2956 0.99761 1.313721
A_driving_Freq 2.90470 0.64540 4.5006 0.57781 5.027062
C_walk_time -0.09854 0.02911 -3.3848 0.03414 -2.886562
C_disability -0.45844 0.54796 -0.8366 0.54898 -0.835077
C_accessibility 0.29397 0.75147 0.3912 0.75926 0.387174
D_bus_cost 3.43610 3.66049 0.9387 1.71946 1.998361
D_bus_time -0.08302 0.03483 -2.3839 0.03557 -2.333819
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by stephanehess »

Hi

the first one is obvious. Your model is overspecified. You have 4 alternatives, so you can only estimated 3 ASCs

For the second one, have a look at your data. Do some people choose bus when it's very expensive?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Hi Stephane,

Thank you so much for your helpful answer!

Regarding the second one, 20 out of 153 individuals chose bus as their mode choice. While bus_time varied among these individuals, the price for all the 20 individuals was the same. Is this the reason that I get this unexpected sign?

Best,
Ariana
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by stephanehess »

Ariana

is the cost always the same, for all 153 individuals, or just for those 20 who chose bus?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Hi Stephane,

Here is the pivot table of the bus_cost column:

Row Labels Count of bus_cost
0 21
1 23
1.1 103
1.5 3
2.2 1
3.35 1
4.5 1
(blank)
Grand Total 153

For 21 people, the price is zero, because the bus option is not available for them.

Thanks!
Ariana
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by stephanehess »

Ariana

thanks. How does this differ between the cases where bus is chosen and where it is not chosen?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Hi Stephane,

Thanks. I have attached the table as an image file:

Best,
Ariana
Attachments
Bus_cost.jpg
Bus_cost.jpg (197.92 KiB) Viewed 5863 times
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by stephanehess »

Did you also now try the model without the overspecfication of the constants?
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Thanks, yes, here is the results:

Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
A_drive_time -0.27983 0.11261 -2.4850 0.09830 -2.8468
B_ride_time -0.19440 0.10766 -1.8057 0.09960 -1.9518
A_Car 0.18109 0.80249 0.2257 0.77829 0.2327
A_driving_Freq 2.10043 0.46973 4.4716 0.46995 4.4695
C_walk_time -0.08344 0.02501 -3.3357 0.02637 -3.1646
C_disability -0.35708 0.53539 -0.6670 0.54299 -0.6576
C_accessibility 1.10168 0.69036 1.5958 0.88878 1.2395
D_bus_cost 0.68214 0.70012 0.9743 0.68034 1.0026
D_bus_time -0.09594 0.03425 -2.8013 0.03741 -2.5648
Ariana_1987
Posts: 10
Joined: 28 Nov 2021, 01:34

Re: In multinomial logit regression; NaNs produced, an unexpected sign of one of the coefficients

Post by Ariana_1987 »

Stephane,

Sorry for my late response. Yes. I also tried that and still the bus cost coefficient has a positive sign.
Post Reply