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.

positive eigenvalues causing NaNs

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
piatulo
Posts: 3
Joined: 12 Nov 2022, 12:41

positive eigenvalues causing NaNs

Post by piatulo »

Hey,

I'm trying to calculate a model with categorical variables for precipitation.
I fixed the asc for walking and the first class of precip for each mode.
As soon as I get all my betas in, the s.e. and t.ratio produces NaNs.

This is my code:


### Initialise code
apollo_initialise()

### set core controls
apollo_control = list(
modelName ="Modell_Precip_Temp_AGRGREGIERT_TAG",
modelDescr ="Simple MNL model on mode choice RP data",
indivID ="FID",
outputDirectory = "output"
)


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

### Vector of parameters, including any that are kept fixed in estimation

apollo_beta=c(asc_fuss = 0,
asc_rad = 0,
asc_auto = 0,
asc_mitfahr = 0,
asc_oev = 0,

b_precip_fuss_0 = 0,
b_precip_fuss_1 = 0,
b_precip_fuss_2 = 0,

b_precip_rad_0 = 0,
b_precip_rad_1 = 0,
b_precip_rad_2 = 0,

b_precip_auto_0 = 0,
b_precip_auto_1 = 0,
b_precip_auto_2 = 0,

b_precip_mitfahr_0 = 0,
b_precip_mitfahr_1 = 0,
b_precip_mitfahr_2 = 0,

b_precip_oev_0 = 0,
b_precip_oev_1 = 0,
b_precip_oev_2 = 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("asc_fuss",
"b_precip_fuss_0",
"b_precip_rad_0",
"b_precip_auto_0",
"b_precip_mitfahr_0",
"b_precip_oev_0")


# ################################################################# #
#### 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[['fuss']] = asc_fuss +
b_precip_fuss_0 * (PRECIP_TAG_KAT == 0)+
b_precip_fuss_1 * (PRECIP_TAG_KAT == 1)+
b_precip_fuss_2 * (PRECIP_TAG_KAT == 2)

V[['rad']] = asc_rad +
b_precip_rad_0 * (PRECIP_TAG_KAT == 0)+
b_precip_rad_1 * (PRECIP_TAG_KAT == 1)+
b_precip_rad_2 * (PRECIP_TAG_KAT == 2)

V[['auto']] = asc_auto +
b_precip_auto_0 * (PRECIP_TAG_KAT == 0) +
b_precip_auto_1 * (PRECIP_TAG_KAT == 1) +
b_precip_auto_2 * (PRECIP_TAG_KAT == 2)


V[['mitfahr']] = asc_mitfahr +
b_precip_mitfahr_0 * (PRECIP_TAG_KAT == 0) +
b_precip_mitfahr_1 * (PRECIP_TAG_KAT == 1) +
b_precip_mitfahr_2 * (PRECIP_TAG_KAT == 2)

V[['oev']] = asc_oev +
b_precip_oev_0 * (PRECIP_TAG_KAT == 0)+
b_precip_oev_1 * (PRECIP_TAG_KAT == 1)+
b_precip_oev_2 * (PRECIP_TAG_KAT == 2)



# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #


choiceAnalysis_settings <- list(
alternatives = c(fuss=1, rad =2, auto=4, mitfahr=5, oev=6),
avail =list(fuss =av_fuss, rad= av_rad, auto=av_auto, mitfahr = av_mitfahr, oev = av_oev),
choiceVar = choice,
V = V
)


### Compute probabilities using MNL model
P[['model']] = apollo_mnl(choiceAnalysis_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 these are my results:
Model name : Modell_Precip_Temp_AGRGREGIERT_TAG
Model description : Simple MNL model on mode choice RP data
Model run at : 2022-11-19 18:11:38
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 1984
Number of rows in database : 100000
Number of modelled outcomes : 1e+05

Number of cores used : 1
Model without mixing

LL(start) : -151142.4
LL at equal shares, LL(0) : -151142.4
LL at observed shares, LL(C) : -122710.1
LL(final) : -122675.2
Rho-squared vs equal shares : 0.1883
Adj.Rho-squared vs equal shares : 0.1883
Rho-squared vs observed shares : 3e-04
Adj.Rho-squared vs observed shares : 2e-04
AIC : 245378.4
BIC : 245511.5

Estimated parameters : 14
Time taken (hh:mm:ss) : 00:01:27.95
pre-estimation : 00:00:18.59
estimation : 00:00:14.57
post-estimation : 00:00:54.79
Iterations : 25
Min abs eigenvalue of Hessian : 1e-06
Some eigenvalues of Hessian are positive, indicating potential problems!

Unconstrained optimisation.

These outputs have had the scaling used in estimation applied to them.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_fuss 0.00000 NA NA NA NA
asc_rad -0.50569 0.02406 -21.02 0.06669 -7.58274
asc_auto 1.18131 0.01752 67.41 0.04961 23.81087
asc_mitfahr -0.47686 0.02103 -22.67 0.05287 -9.01947
asc_oev -0.97220 0.02485 -39.12 0.06137 -15.84059
b_precip_fuss_0 0.00000 NA NA NA NA
b_precip_fuss_1 0.05050 492.93528 1.0244e-04 NaN NaN
b_precip_fuss_2 0.06223 NaN NaN 0.15710 0.39613
b_precip_rad_0 0.00000 NA NA NA NA
b_precip_rad_1 -0.12342 492.93528 -2.5038e-04 NaN NaN
b_precip_rad_2 -0.12571 NaN NaN 0.25779 -0.48766
b_precip_auto_0 0.00000 NA NA NA NA
b_precip_auto_1 0.03870 492.93528 7.851e-05 NaN NaN
b_precip_auto_2 0.07110 NaN NaN 0.33047 0.21515
b_precip_mitfahr_0 0.00000 NA NA NA NA
b_precip_mitfahr_1 0.05703 492.93527 1.1569e-04 NaN NaN
b_precip_mitfahr_2 -0.03169 NaN NaN NaN NaN
b_precip_oev_0 0.00000 NA NA NA NA
b_precip_oev_1 -0.02281 492.93527 -4.627e-05 NaN NaN
b_precip_oev_2 0.02407 NaN NaN 0.33292 0.07231



Thanks for your help!
Pia
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: positive eigenvalues causing NaNs

Post by stephanehess »

Hi

your model is overspecified. You cannot interact the covariate with all of the alternatives, i.e. you need keep the terms b_precip_fuss_1 and b_precip_fuss_2 fixed to 0 too

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply