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.

log-likelihood calculation failed

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
duxdfx
Posts: 8
Joined: 13 Dec 2021, 18:10

log-likelihood calculation failed

Post by duxdfx »

Hi,

I am estimating a mixed logit model with 3 random coefficients and got the following error message:
"Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Log-likelihood calculation fails at values close to the starting values!"
I tried to use the logit estimates for the starting values, but still didn't work. How should I proceed? I pasted the code below.

Thanks,
Sheldon

##----------------------------------
## estimation using Apollo
##----------------------------------
##following the example here: http://apollochoicemodelling.com/files/ ... ce_space.r
rm(list=ls())
setwd("Y:/Corn BE Yield Impact")

#install.packages("apollo")
library(apollo)

load("sample_logit_10000_forced_final.RData")
#head(data_sample)
#here "id" is the choice situation ("unit_id" + "crop_year"); "ids" is unit_id
colnames(data_sample)[which(colnames(data_sample)=="ids")] <- c("unit_id")

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "MMNL_preference_space",
modelDescr = "Mixed logit model with demographics",
indivID = "unit_id",
mixing = TRUE,
nCores = 4,
outputDirectory = "output"
)

database = data_sample
head(database)

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

### Vector of parameters, including any that are kept fixed in estimation; asc-alternative specific constant.
apollo_beta=c(asc_pln1 = 0, asc_pln2 = -0.70, asc_pln3 = 0.76, asc_pln4 = 2.08, asc_pln5 = 0.78,
asc_pln6 = -0.86, asc_pln7 = -4.11, asc_pln8 = -4.31, asc_pln9 = 1.38, asc_pln10 = 2.22,
asc_pln11 = 1.09, asc_pln12 = -0.95, asc_pln13 = -2.10, asc_pln14 = -1.21, asc_pln15 = -0.39,
asc_pln16 = 0.42, asc_pln17 = 1.40, asc_pln18 = 1.55, asc_pln19 = 0.16, asc_pln20 = -2.96, asc_pln21 = -5.925,
# b_prm = 0,
# b_sub = 0,
# b_lia = 0,
# asc_pln2_shift_acres = -0.00007, asc_pln3_shift_acres = 0.0003, asc_pln4_shift_acres = -0.0015,asc_pln5_shift_acres = -0.00078,asc_pln6_shift_acres = -0.0018,
# asc_pln7_shift_acres = -0.00077, asc_pln8_shift_acres = -0.0016,asc_pln9_shift_acres = 0.0016, asc_pln10_shift_acres = 0.0018, asc_pln11_shift_acres = 0.0014,
# asc_pln12_shift_acres = 0.0015, asc_pln13_shift_acres = 0.006, asc_pln14_shift_acres = 0.0019,asc_pln15_shift_acres = -0.001,asc_pln16_shift_acres = 0.0016,
# asc_pln17_shift_acres = 0.005, asc_pln18_shift_acres = 0.0009,asc_pln19_shift_acres = 0.000875,asc_pln20_shift_acres = 0.0014,asc_pln21_shift_acres = 0.0012,
# asc_pln2_shift_myield = -0.01, asc_pln3_shift_myield = -0.015,asc_pln4_shift_myield = -0.002,asc_pln5_shift_myield = 0.002,asc_pln6_shift_myield = 0.015,
# asc_pln7_shift_myield = 0.023, asc_pln8_shift_myield = 0.023,asc_pln9_shift_myield = -0.001,asc_pln10_shift_myield = -0.0006,asc_pln11_shift_myield = 0.007,
# asc_pln12_shift_myield = 0.013, asc_pln13_shift_myield = 0.016,asc_pln14_shift_myield = -0.0019,asc_pln15_shift_myield = -0.011,asc_pln16_shift_myield = -0.012,
# asc_pln17_shift_myield = -0.0012, asc_pln18_shift_myield = 0.00001,asc_pln19_shift_myield = 0.013,asc_pln20_shift_myield = 0.032,asc_pln21_shift_myield = 0.045,
# asc_pln2_shift_vyield = -0.028, asc_pln3_shift_vyield = 0.01,asc_pln4_shift_vyield = 0.0055,asc_pln5_shift_vyield = 0.016,asc_pln6_shift_vyield = 0.007,
# asc_pln7_shift_vyield = 0.037, asc_pln8_shift_vyield = 0.021,asc_pln9_shift_vyield = 0.012,asc_pln10_shift_vyield = 0.013,asc_pln11_shift_vyield = 0.019,
# asc_pln12_shift_vyield = 0.019, asc_pln13_shift_vyield = 0.03,asc_pln14_shift_vyield = 0.009,asc_pln15_shift_vyield = 0.026,asc_pln16_shift_vyield = 0.018,
# asc_pln17_shift_vyield = 0.013, asc_pln18_shift_vyield = 0.017,asc_pln19_shift_vyield = 0.018,asc_pln20_shift_vyield = 0.024,asc_pln21_shift_vyield = 0.03,
mu_log_b_prm = -0.004, sigma_log_b_prm = 0,
mu_log_b_sub = 0.004, sigma_log_b_sub = 0,
mu_log_b_lia = 0.0001, sigma_log_b_lia = 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_pln1")

# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("draws_prm","draws_sub","draws_lia"),
intraDrawsType = "halton",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["b_prm"]] = -exp( mu_log_b_prm + sigma_log_b_prm * draws_prm )
randcoeff[["b_sub"]] = -exp( mu_log_b_sub + sigma_log_b_sub * draws_sub )
randcoeff[["b_lia"]] = -exp( mu_log_b_lia + sigma_log_b_lia * draws_lia )

return(randcoeff)
}

# ################################################################# #
#### 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()

### Create alternative specific constants and coefficients using interactions with socio-demographics

# asc_pln2_value = asc_pln2 + asc_pln2_shift_acres * acres + asc_pln2_shift_myield * yield_mean + asc_pln2_shift_vyield * yield_var
# asc_pln3_value = asc_pln3 + asc_pln3_shift_acres * acres + asc_pln3_shift_myield * yield_mean + asc_pln3_shift_vyield * yield_var
# asc_pln4_value = asc_pln4 + asc_pln4_shift_acres * acres + asc_pln4_shift_myield * yield_mean + asc_pln4_shift_vyield * yield_var
# asc_pln5_value = asc_pln5 + asc_pln5_shift_acres * acres + asc_pln5_shift_myield * yield_mean + asc_pln5_shift_vyield * yield_var
# asc_pln6_value = asc_pln6 + asc_pln6_shift_acres * acres + asc_pln6_shift_myield * yield_mean + asc_pln6_shift_vyield * yield_var
# asc_pln7_value = asc_pln7 + asc_pln7_shift_acres * acres + asc_pln7_shift_myield * yield_mean + asc_pln7_shift_vyield * yield_var
# asc_pln8_value = asc_pln8 + asc_pln8_shift_acres * acres + asc_pln8_shift_myield * yield_mean + asc_pln8_shift_vyield * yield_var
# asc_pln9_value = asc_pln9 + asc_pln9_shift_acres * acres + asc_pln9_shift_myield * yield_mean + asc_pln9_shift_vyield * yield_var
# asc_pln10_value = asc_pln10 + asc_pln10_shift_acres * acres + asc_pln10_shift_myield * yield_mean + asc_pln10_shift_vyield * yield_var
# asc_pln11_value = asc_pln11 + asc_pln11_shift_acres * acres + asc_pln11_shift_myield * yield_mean + asc_pln11_shift_vyield * yield_var
# asc_pln12_value = asc_pln12 + asc_pln12_shift_acres * acres + asc_pln12_shift_myield * yield_mean + asc_pln12_shift_vyield * yield_var
# asc_pln13_value = asc_pln13 + asc_pln13_shift_acres * acres + asc_pln13_shift_myield * yield_mean + asc_pln13_shift_vyield * yield_var
# asc_pln14_value = asc_pln14 + asc_pln14_shift_acres * acres + asc_pln14_shift_myield * yield_mean + asc_pln14_shift_vyield * yield_var
# asc_pln15_value = asc_pln15 + asc_pln15_shift_acres * acres + asc_pln15_shift_myield * yield_mean + asc_pln15_shift_vyield * yield_var
# asc_pln16_value = asc_pln16 + asc_pln16_shift_acres * acres + asc_pln16_shift_myield * yield_mean + asc_pln16_shift_vyield * yield_var
# asc_pln17_value = asc_pln17 + asc_pln17_shift_acres * acres + asc_pln17_shift_myield * yield_mean + asc_pln17_shift_vyield * yield_var
# asc_pln18_value = asc_pln18 + asc_pln18_shift_acres * acres + asc_pln18_shift_myield * yield_mean + asc_pln18_shift_vyield * yield_var
# asc_pln19_value = asc_pln19 + asc_pln19_shift_acres * acres + asc_pln19_shift_myield * yield_mean + asc_pln19_shift_vyield * yield_var
# asc_pln20_value = asc_pln20 + asc_pln20_shift_acres * acres + asc_pln20_shift_myield * yield_mean + asc_pln20_shift_vyield * yield_var
# asc_pln21_value = asc_pln21 + asc_pln21_shift_acres * acres + asc_pln21_shift_myield * yield_mean + asc_pln21_shift_vyield * yield_var


### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["pln1"]] = asc_pln1 + b_prm * prm1 + b_sub * sub1 + b_lia * lia1
V[["pln2"]] = asc_pln2 + b_prm * prm2 + b_sub * sub2 + b_lia * lia2
V[["pln3"]] = asc_pln3 + b_prm * prm3 + b_sub * sub3 + b_lia * lia3
V[["pln4"]] = asc_pln4 + b_prm * prm4 + b_sub * sub4 + b_lia * lia4
V[["pln5"]] = asc_pln5 + b_prm * prm5 + b_sub * sub5 + b_lia * lia5
V[["pln6"]] = asc_pln6 + b_prm * prm6 + b_sub * sub6 + b_lia * lia6
V[["pln7"]] = asc_pln7 + b_prm * prm7 + b_sub * sub7 + b_lia * lia7
V[["pln8"]] = asc_pln8 + b_prm * prm8 + b_sub * sub8 + b_lia * lia8
V[["pln9"]] = asc_pln9 + b_prm * prm9 + b_sub * sub9 + b_lia * lia9
V[["pln10"]] = asc_pln10 + b_prm * prm10 + b_sub * sub10 + b_lia * lia10
V[["pln11"]] = asc_pln11 + b_prm * prm11 + b_sub * sub11 + b_lia * lia11
V[["pln12"]] = asc_pln12 + b_prm * prm12 + b_sub * sub12 + b_lia * lia12
V[["pln13"]] = asc_pln13 + b_prm * prm13 + b_sub * sub13 + b_lia * lia13
V[["pln14"]] = asc_pln14 + b_prm * prm14 + b_sub * sub14 + b_lia * lia14
V[["pln15"]] = asc_pln15 + b_prm * prm15 + b_sub * sub15 + b_lia * lia15
V[["pln16"]] = asc_pln16 + b_prm * prm16 + b_sub * sub16 + b_lia * lia16
V[["pln17"]] = asc_pln17 + b_prm * prm17 + b_sub * sub17 + b_lia * lia17
V[["pln18"]] = asc_pln18 + b_prm * prm18 + b_sub * sub18 + b_lia * lia18
V[["pln19"]] = asc_pln19 + b_prm * prm19 + b_sub * sub19 + b_lia * lia19
V[["pln20"]] = asc_pln20 + b_prm * prm20 + b_sub * sub20 + b_lia * lia20
V[["pln21"]] = asc_pln21 + b_prm * prm21 + b_sub * sub21 + b_lia * lia21

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(pln1=1, pln2=2, pln3=3, pln4=4, pln5=5, pln6=6, pln7=7, pln8=8, pln9=9, pln10=10, pln11=11, pln12=12,pln13=13, pln14=14, pln15=15,pln16=16, pln17=17, pln18=18,pln19=19, pln20=20, pln21=21),
avail = list(pln1=av1, pln2=av2, pln3=av3, pln4=av4, pln5=av5, pln6=av6, pln7=av7, pln8=av8,pln9=av9, pln10=av10, pln11=av11, pln12=av12,pln13=av13,pln14=av14,pln15=av15,pln16=av16,pln17=av17,pln18=av18,pln19=av19,pln20=av20,pln21=av21),
choiceVar = choice,
utilities = 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)

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

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

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: log-likelihood calculation failed

Post by stephanehess »

Hi

this is quite a common starting values issue with Lognormal distributions - see the discussion at http://www.apollochoicemodelling.com/faq.html

Remember that the lognormal uses an exponential of a Normal, so if you are using MNL point values as the starting values for the means, you need to take the logs of those first

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