Page 1 of 1

positive eigenvalues causing NaNs

Posted: 19 Nov 2022, 17:24
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

Re: positive eigenvalues causing NaNs

Posted: 25 Nov 2022, 13:16
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