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.

NaNs

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
TAJKUE
Posts: 7
Joined: 17 Sep 2020, 13:24

Re: NaNs

Post by TAJKUE »

I have now fixed lambda_Brand and lambda_NoBrand at 1, which makes the NaNs disappear (most of them). However, having both nests to 1, would mean that the likelihood of choosing one of the nests is the same, which does not make too much sense.

Here the output:
Model run using Apollo for R, version 0.1.0
www.ApolloChoiceModelling.com

Model name : NL Model
Model description : NL Model with data
Model run at : 2020-09-18 15:42:14
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 500
Number of observations : 2500

Number of cores used : 1
Model without mixing

LL(start) : -6602.684
LL(0) : -6309.866
LL(final) : -4453.675
Rho-square (0) : 0.2942
Adj.Rho-square (0) : 0.2931
AIC : 8921.35
BIC : 8962.12
Estimated parameters : 7
Time taken (hh:mm:ss) : 00:00:18.73
Iterations : 33
Min abs eigenvalue of hessian : 0.160004

Estimates:
Estimate Std.err. t.ratio(0) Rob.std.err. Rob.t.ratio(0)
b_tt_Price1 0.0000 NA NA NA NA
b_tt_Price2 -0.6301 0.0664 -9.49 0.1173 -5.37
b_tt_Price3 0.4845 0.0545 8.89 0.0929 5.21
b_tt_Price4 -1.5275 0.0879 -17.39 0.1154 -13.24
b_tt_Brand 0.0000 NA NA NA NA
b_tt_No_Name_Brand_1 -0.0416 0.0483 -0.86 0.0664 -0.63
b_tt_No_Name_Brand_2 0.0000 NA NA NA NA
b_tt_No_Bio -5.3755 0.3549 -15.15 0.3415 -15.74
b_tt_Yes_Bio 0.0000 NA NA NA NA
b_tt_Outside_Option1 1.0000 NA NA NA NA
b_tt_Outside_Option2 1.8070 2.0261 0.89 2.3044 0.78
lambda_Brand 1.0000 NA NA NA NA
lambda_NoBrand 1.0000 NA NA NA NA
lambda_outside -11.3439 1.5380 -7.38 1.8524 -6.12

Overview of choices for model component "NL"
B11 B12 B13 B14 B21 B22 B23 B24 NB111 NB112 NB113 NB114 NB121 NB122 NB123 NB124 NB211 NB212
Times available 1319.00 1261.00 1279.00 1282.00 1283.00 1263.00 1275.00 1268.00 1343.00 1292.00 1296.00 1304.00 1328.00 1301 1309.00 1312.00 1329.00 1289
Times chosen 604.00 354.00 265.00 35.00 110.00 69.00 41.00 11.00 14.00 1.00 535.00 103.00 1.00 0 112.00 16.00 2.00 0
Percentage chosen overall 24.16 14.16 10.60 1.40 4.40 2.76 1.64 0.44 0.56 0.04 21.40 4.12 0.04 0 4.48 0.64 0.08 0
Percentage chosen when available 45.79 28.07 20.72 2.73 8.57 5.46 3.22 0.87 1.04 0.08 41.28 7.90 0.08 0 8.56 1.22 0.15 0
NB213 NB214 NB221 NB222 NB223 NB224 OO1 OO2
Times available 1269.00 1288.00 1299 1278 1264.00 1285.00 2500.00 2500.00
Times chosen 62.00 16.00 0 0 8.00 2.00 72.00 67.00
Percentage chosen overall 2.48 0.64 0 0 0.32 0.08 2.88 2.68
Percentage chosen when available 4.89 1.24 0 0 0.63 0.16 2.88 2.68

Warning: some alternatives are never chosen in your data!
Nesting structure:
Nest: root (1)
|-Nest: Brand (1)
| |-Alternative: B11
| |-Alternative: B12
| |-Alternative: B13
| |-Alternative: B14
| |-Alternative: B21
| |-Alternative: B22
| |-Alternative: B23
| '-Alternative: B24
|-Nest: NoBrand (1)
| |-Alternative: NB111
| |-Alternative: NB112
| |-Alternative: NB113
| |-Alternative: NB114
| |-Alternative: NB211
| |-Alternative: NB212
| |-Alternative: NB213
| |-Alternative: NB214
| |-Alternative: NB121
| |-Alternative: NB122
| |-Alternative: NB123
| |-Alternative: NB124
| |-Alternative: NB221
| |-Alternative: NB222
| |-Alternative: NB223
| '-Alternative: NB224
'-Nest: Outside (-11.3439)
|-Alternative: OO1
'-Alternative: OO2

And the code:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="NL Model",
modelDescr ="NL Model with data",
indivID ="ID"
)

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

database = read.csv("Data_Model.csv",
fileEncoding="UTF-8-BOM",
sep = ";",
header = TRUE)


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(b_tt_Price1 = 0,
b_tt_Price2 = 0,
b_tt_Price3 = 0,
b_tt_Price4 = 0,
b_tt_Brand = 0,
b_tt_No_Name_Brand_1 =0,
b_tt_No_Name_Brand_2 =0,
b_tt_No_Bio = 0,
b_tt_Yes_Bio =0,
b_tt_Outside_Option1 =1,
b_tt_Outside_Option2 =0,
lambda_Brand=1,
lambda_NoBrand=1,
lambda_outside=0.95
)


### 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("b_tt_Price1","b_tt_Yes_Bio","b_tt_No_Name_Brand_2","b_tt_Outside_Option1","b_tt_Brand","lambda_Brand","lambda_NoBrand")


# ################################################################# #
#### 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 nl_settings, order is irrelevant
V = list()
V[['B11']] = b_tt_Price1*AV_Barilla11 + b_tt_Yes_Bio*AV_Barilla11 + b_tt_Brand*AV_Barilla11
V[['B12']] = b_tt_Price2 *AV_Barilla12+ b_tt_Yes_Bio *AV_Barilla12+ b_tt_Brand*AV_Barilla12
V[['B13']] = b_tt_Price3 *AV_Barilla13 + b_tt_Yes_Bio*AV_Barilla13 + b_tt_Brand*AV_Barilla13
V[['B14']] = b_tt_Price4 *AV_Barilla14+ b_tt_Yes_Bio*AV_Barilla14 + b_tt_Brand*AV_Barilla14
V[['B21']] = b_tt_Price1*AV_Barilla21 + b_tt_No_Bio*AV_Barilla21 + b_tt_Brand*AV_Barilla21
V[['B22']] = b_tt_Price2*AV_Barilla22 + b_tt_No_Bio*AV_Barilla22 + b_tt_Brand*AV_Barilla22
V[['B23']] = b_tt_Price3 *AV_Barilla23+ b_tt_No_Bio*AV_Barilla23 + b_tt_Brand*AV_Barilla23
V[['B24']] = b_tt_Price4*AV_Barilla24 + b_tt_No_Bio*AV_Barilla24 + b_tt_Brand*AV_Barilla24
V[['NB111']] = b_tt_Price1*AV_NoNameBrand111 + b_tt_Yes_Bio*AV_NoNameBrand111 + b_tt_No_Name_Brand_1*AV_NoNameBrand111
V[['NB112']] = b_tt_Price2*AV_NoNameBrand112 + b_tt_Yes_Bio*AV_NoNameBrand112 + b_tt_No_Name_Brand_1*AV_NoNameBrand112
V[['NB113']] = b_tt_Price3*AV_NoNameBrand113 + b_tt_Yes_Bio*AV_NoNameBrand113 + b_tt_No_Name_Brand_1*AV_NoNameBrand113
V[['NB114']] = b_tt_Price4*AV_NoNameBrand114 + b_tt_Yes_Bio*AV_NoNameBrand114 + b_tt_No_Name_Brand_1*AV_NoNameBrand114
V[['NB121']] = b_tt_Price1 *AV_NoNameBrand121+ b_tt_No_Bio*AV_NoNameBrand121 + b_tt_No_Name_Brand_1*AV_NoNameBrand121
V[['NB122']] = b_tt_Price2 *AV_NoNameBrand122+ b_tt_No_Bio*AV_NoNameBrand122 + b_tt_No_Name_Brand_1*AV_NoNameBrand122
V[['NB123']] = b_tt_Price3*AV_NoNameBrand123 + b_tt_No_Bio*AV_NoNameBrand123 + b_tt_No_Name_Brand_1*AV_NoNameBrand123
V[['NB124']] = b_tt_Price4*AV_NoNameBrand124 + b_tt_No_Bio*AV_NoNameBrand124 + b_tt_No_Name_Brand_1*AV_NoNameBrand124
V[['NB211']] = b_tt_Price1 *AV_NoNameBrand211+ b_tt_Yes_Bio*AV_NoNameBrand211 + b_tt_No_Name_Brand_2*AV_NoNameBrand211
V[['NB212']] = b_tt_Price2 *AV_NoNameBrand212+ b_tt_Yes_Bio*AV_NoNameBrand212 + b_tt_No_Name_Brand_2*AV_NoNameBrand212
V[['NB213']] = b_tt_Price3 *AV_NoNameBrand213 + b_tt_Yes_Bio*AV_NoNameBrand213 + b_tt_No_Name_Brand_2*AV_NoNameBrand213
V[['NB214']] = b_tt_Price4*AV_NoNameBrand214 + b_tt_Yes_Bio*AV_NoNameBrand214 + b_tt_No_Name_Brand_2*AV_NoNameBrand214
V[['NB221']] = b_tt_Price1*AV_NoNameBrand221 + b_tt_No_Bio *AV_NoNameBrand221+ b_tt_No_Name_Brand_2*AV_NoNameBrand221
V[['NB222']] = b_tt_Price2*AV_NoNameBrand222 + b_tt_No_Bio *AV_NoNameBrand222+ b_tt_No_Name_Brand_2*AV_NoNameBrand222
V[['NB223']] = b_tt_Price3*AV_NoNameBrand223 + b_tt_No_Bio*AV_NoNameBrand223 + b_tt_No_Name_Brand_2*AV_NoNameBrand223
V[['NB224']] = b_tt_Price4*AV_NoNameBrand224 + b_tt_No_Bio*AV_NoNameBrand224 + b_tt_No_Name_Brand_2*AV_NoNameBrand224
V[['OO1']] = b_tt_Outside_Option1*AV_Outside1
V[['OO2']] = b_tt_Outside_Option2*AV_Outside2

### Specify nests for NL model
nlNests = list(root=1, Brand=lambda_Brand, NoBrand = lambda_NoBrand, Outside = lambda_outside)

### Specify tree structure for NL model
nlStructure= list()
nlStructure[["root"]] = c("Brand", "NoBrand","Outside")
nlStructure[["Brand"]] = c("B11","B12","B13","B14",
"B21","B22","B23","B24")
nlStructure[["NoBrand"]] = c("NB111","NB112","NB113","NB114",
"NB211","NB212","NB213","NB214",
"NB121","NB122","NB123","NB124",
"NB221","NB222","NB223","NB224")
nlStructure[["Outside"]] = c("OO1","OO2")

### Define settings for NL model
nl_settings <- list(
alternatives = c(B11=1, B12=2, B13=3, B14=4, B21=5, B22=6,B23=7, B24=8,
NB111=9, NB112=10, NB113=11,NB114=12, NB121=13, NB122=14,NB123=15,NB124=16,
NB211=17, NB212=18, NB213=19,NB214=20, NB221=21, NB222=22,NB223=23,NB224=24,
OO1=25, OO2=26),
avail = list(B11=AV_Barilla11, B12=AV_Barilla12, B13=AV_Barilla13, B14=AV_Barilla14,
B21=AV_Barilla21, B22=AV_Barilla22, B23=AV_Barilla23, B24=AV_Barilla24,
NB111=AV_NoNameBrand111, NB112=AV_NoNameBrand112, NB113=AV_NoNameBrand113, NB114=AV_NoNameBrand114,
NB121=AV_NoNameBrand121, NB122=AV_NoNameBrand122, NB123=AV_NoNameBrand123, NB124=AV_NoNameBrand124,
NB211=AV_NoNameBrand211, NB212=AV_NoNameBrand212, NB213=AV_NoNameBrand213, NB214=AV_NoNameBrand214,
NB221=AV_NoNameBrand221, NB222=AV_NoNameBrand222, NB223=AV_NoNameBrand223, NB224=AV_NoNameBrand224,
OO1=AV_Outside1, OO2=AV_Outside2),
choiceVar = choice,
V = V,
nlNests = nlNests,
nlStructure = nlStructure
)

### Compute probabilities using NL model
P[["model"]] = apollo_nl(nl_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)

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

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)
stephanehess
Site Admin
Posts: 1042
Joined: 24 Apr 2020, 16:29

Re: NaNs

Post by stephanehess »

While the value of lambda has an influence on the nest probability, it is not true that the same value of lambda means that the probability of choosing the nest is the same. This depends on the utilities within the nest too.

But overall, I think you need to recognise the lack of richness in your data.
--------------------------------
Stephane Hess
www.stephanehess.me.uk
TAJKUE
Posts: 7
Joined: 17 Sep 2020, 13:24

Re: NaNs

Post by TAJKUE »

Thank you! Yes I am aware that the richness of the data is quite limited.

So would this mean if there were more parameters in the model, there would be less issues?
stephanehess
Site Admin
Posts: 1042
Joined: 24 Apr 2020, 16:29

Re: NaNs

Post by stephanehess »

Not more parameters in the model, but more variability in the data, so not just a set of binary attributes
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply