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.

Error after convergence when estimating a latent class model

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

Error after convergence when estimating a latent class model

Post by duxdfx »

Hi, First, thank you for all your help along the process. You have been answering several questions quickly, very helpful. Much appreciated!

the model runs and converged after 59 iterations, but some error message appears after that saying: "Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS", :
NA in the initial gradient
In addition: There were 16 warnings (use warnings() to see them)"

Thank you for your comments and suggestions!

Sheldon

I paste the window outputs and code below:

---------outputs-----------------------------------------------------------------------------------------------

> > model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
>
> Testing likelihood function...
> Apollo found a model component of type classAlloc without a
> componentName. The name was set to "classAlloc" by default.
>
> Overview of choices for MNL model component Class_1:
> pln1 pln2 pln3 pln4 pln5
> Times available 38375.00 33340.00 37904.00 38375.00 38375.0
> Times chosen 725.00 72.00 291.00 3360.00 1880.0
> Percentage chosen overall 1.89 0.19 0.76 8.76 4.9
> Percentage chosen when available 1.89 0.22 0.77 8.76 4.9
> pln6 pln7 pln8 pln9 pln10
> Times available 38092.00 34101.00 27709.00 36517.00 36517.00
> Times chosen 1537.00 227.00 96.00 2820.00 6721.00
> Percentage chosen overall 4.01 0.59 0.25 7.35 17.51
> Percentage chosen when available 4.03 0.67 0.35 7.72 18.41
> pln11 pln12 pln13 pln14 pln15
> Times available 36517.00 36422.00 34686.00 36454.00 28989.00
> Times chosen 4995.00 1277.00 345.00 228.00 95.00
> Percentage chosen overall 13.02 3.33 0.90 0.59 0.25
> Percentage chosen when available 13.68 3.51 0.99 0.63 0.33
> pln16 pln17 pln18 pln19 pln20
> Times available 37383.00 38340.0 38375.00 38360.00 37971.00
> Times chosen 366.00 1380.0 3564.00 4691.00 2768.00
> Percentage chosen overall 0.95 3.6 9.29 12.22 7.21
> Percentage chosen when available 0.98 3.6 9.29 12.23 7.29
> pln21
> Times available 35933.00
> Times chosen 937.00
> Percentage chosen overall 2.44
> Percentage chosen when available 2.61
>
> Overview of choices for MNL model component Class_2:
> pln1 pln2 pln3 pln4 pln5
> Times available 38375.00 33340.00 37904.00 38375.00 38375.0
> Times chosen 725.00 72.00 291.00 3360.00 1880.0
> Percentage chosen overall 1.89 0.19 0.76 8.76 4.9
> Percentage chosen when available 1.89 0.22 0.77 8.76 4.9
> pln6 pln7 pln8 pln9 pln10
> Times available 38092.00 34101.00 27709.00 36517.00 36517.00
> Times chosen 1537.00 227.00 96.00 2820.00 6721.00
> Percentage chosen overall 4.01 0.59 0.25 7.35 17.51
> Percentage chosen when available 4.03 0.67 0.35 7.72 18.41
> pln11 pln12 pln13 pln14 pln15
> Times available 36517.00 36422.00 34686.00 36454.00 28989.00
> Times chosen 4995.00 1277.00 345.00 228.00 95.00
> Percentage chosen overall 13.02 3.33 0.90 0.59 0.25
> Percentage chosen when available 13.68 3.51 0.99 0.63 0.33
> pln16 pln17 pln18 pln19 pln20
> Times available 37383.00 38340.0 38375.00 38360.00 37971.00
> Times chosen 366.00 1380.0 3564.00 4691.00 2768.00
> Percentage chosen overall 0.95 3.6 9.29 12.22 7.21
> Percentage chosen when available 0.98 3.6 9.29 12.23 7.29
> pln21
> Times available 35933.00
> Times chosen 937.00
> Percentage chosen overall 2.44
> Percentage chosen when available 2.61
>
> Summary of class allocation for LC model component :
> Mean prob.
> Class_1 0.5000
> Class_2 0.5000
> Class probability for model component "model" averaged across
> observations of each individual.
>
> Pre-processing likelihood function...
> Preparing workers for multithreading...
> Analytical gradient is different to numerical one. Numerical gradients
> will be used.
>
> Testing influence of parameters..........................................................................................................
> Starting main estimation
> Initial function value: -134085.4
> Initial gradient value:
> asc_pln2 asc_pln3 asc_pln4
> 7.197121e+01 2.901757e+02 3.078990e+03
> asc_pln5 asc_pln6 asc_pln7
> 1.387307e+03 2.271744e+02 1.507090e+02
> asc_pln8 asc_pln9 asc_pln10
> 9.547226e+01 2.176493e+03 2.878016e+03
> asc_pln11 asc_pln12 asc_pln13
> 9.323144e+02 1.035081e+03 8.221329e+01
> asc_pln14 asc_pln15 asc_pln16
> 2.279871e+02 9.493623e+01 3.621384e+02
> asc_pln17 asc_pln18 asc_pln19
> 4.997754e+02 1.204641e+03 -5.356521e+03
> asc_pln20 asc_pln21 asc_pln2_shift_acres
> -9.870589e+03 -2.932440e+02 8.201400e+03
> asc_pln3_shift_acres asc_pln4_shift_acres asc_pln5_shift_acres
> 4.267518e+04 3.796766e+05 2.215978e+05
> asc_pln6_shift_acres asc_pln7_shift_acres asc_pln8_shift_acres
> 5.571117e+04 3.064926e+04 1.554940e+04
> asc_pln9_shift_acres asc_pln10_shift_acres asc_pln11_shift_acres
> 5.233957e+05 1.043346e+06 5.261104e+05
> asc_pln12_shift_acres asc_pln13_shift_acres asc_pln14_shift_acres
> 3.291979e+05 -3.294270e+05 4.661554e+04
> asc_pln15_shift_acres asc_pln16_shift_acres asc_pln17_shift_acres
> 1.257084e+04 7.656429e+04 -8.226655e+04
> asc_pln18_shift_acres asc_pln19_shift_acres asc_pln20_shift_acres
> 3.756615e+05 -7.173910e+05 -2.540068e+06
[1] -2881798
> asc_pln21_shift_acres asc_pln2_shift_myield asc_pln3_shift_myield
> -1.141659e+05 8.137443e+03 3.104285e+04
> asc_pln4_shift_myield asc_pln5_shift_myield asc_pln6_shift_myield
> 3.709959e+05 1.786418e+05 4.149352e+04
> asc_pln7_shift_myield asc_pln8_shift_myield asc_pln9_shift_myield
> 2.242817e+04 1.420262e+04 2.752967e+05
> asc_pln10_shift_myield asc_pln11_shift_myield asc_pln12_shift_myield
> 3.996238e+05 1.682282e+05 1.466890e+05
> asc_pln13_shift_myield asc_pln14_shift_myield asc_pln15_shift_myield
> 1.057988e+04 2.763163e+04 1.064466e+04
> asc_pln16_shift_myield asc_pln17_shift_myield asc_pln18_shift_myield
> 4.061014e+04 6.155642e+04 1.736578e+05
> asc_pln19_shift_myield asc_pln20_shift_myield asc_pln21_shift_myield
> -6.299008e+05 -1.396788e+06 -4.286428e+04
[1] -2069553
> asc_pln2_shift_vyield asc_pln3_shift_vyield asc_pln4_shift_vyield
> 2.959071e+02 2.330179e+03 2.046515e+04
> asc_pln5_shift_vyield asc_pln6_shift_vyield asc_pln7_shift_vyield
> 1.263482e+04 2.687462e+03 2.020828e+03
> asc_pln8_shift_vyield asc_pln9_shift_vyield asc_pln10_shift_vyield
> 8.253027e+02 2.504818e+04 4.395995e+04
> asc_pln11_shift_vyield asc_pln12_shift_vyield asc_pln13_shift_vyield
> 2.154000e+04 1.435153e+04 -4.164513e+03
> asc_pln14_shift_vyield asc_pln15_shift_vyield asc_pln16_shift_vyield
> 2.233912e+03 9.314523e+02 4.085625e+03
> asc_pln17_shift_vyield asc_pln18_shift_vyield asc_pln19_shift_vyield
> 1.007901e+03 1.868507e+04 -4.176729e+04
> asc_pln20_shift_vyield asc_pln21_shift_vyield delta_a
> -1.255821e+05 -6.279214e+03 3.080251e+03
> gamma_atten_a gamma_d03_a gamma_d03_b
> 4.896354e+02 1.737360e+02 -1.737360e+02
> gamma_d09_a gamma_d09_b gamma_acres_a
> 1.610152e+02 -1.610152e+02 8.101376e+05
> gamma_acres_b gamma_myield_a gamma_myield_b
> -8.101376e+05 3.973745e+05 -3.973745e+05
> gamma_vyield_a gamma_vyield_b gamma_disas_a
> 3.577876e+04 -3.577876e+04 5.130609e+01
> gamma_disas_b mu_log_b_prm_a sigma_log_b_prm_a
> -5.130609e+01 2.681349e+04 6.206285e+04
> mu_log_b_prm_b sigma_log_b_prm_b mu_log_b_sub_a
> 2.739474e+03 6.573969e+03 -4.700985e+03
> sigma_log_b_sub_a mu_log_b_sub_b sigma_log_b_sub_b
> 6.322032e+03 -6.025724e+02 9.646499e+02
> mu_log_b_lia_a sigma_log_b_lia_a mu_log_b_lia_b
> -9.107004e+03 4.336067e+03 -6.783765e+02
> sigma_log_b_lia_b
> 5.362621e+02
[1] 536.2621
> initial value 134085.381398
> iter 2 value 123127.656383
> iter 3 value 118831.582857
> iter 4 value 115342.376402
> iter 5 value 113809.503872
> iter 6 value 112151.837252
> iter 7 value 110325.291527
> Initial iteration could not be written to output/LC_MMNL_iterations.csv.
> iter 8 value 110087.365938
> Initial iteration could not be written to output/LC_MMNL_iterations.csv.
> iter 9 value 109382.868316
> iter 10 value 109328.562504
> iter 11 value 108450.577036
> iter 12 value 107919.278041
> iter 13 value 107465.022182
> iter 14 value 105671.001323
> iter 15 value 105102.603963
> iter 16 value 104854.591912
> iter 17 value 104487.984860
> iter 18 value 104000.529539
> iter 19 value 103430.090085
> iter 20 value 103075.309091
> iter 21 value 102934.203564
> Initial iteration could not be written to output/LC_MMNL_iterations.csv.
> iter 22 value 102715.997330
> iter 23 value 102280.496251
> iter 24 value 101956.539594
> iter 25 value 101736.306186
> iter 26 value 101463.604276
> iter 27 value 100992.388089
> iter 28 value 100316.915510
> iter 29 value 99191.842421
> iter 30 value 98356.115501
> iter 31 value 97679.337578
> iter 32 value 97138.500178
> iter 33 value 96245.976780
> iter 34 value 95769.375341
> iter 35 value 95466.050595
> iter 36 value 94876.903465
> iter 37 value 93741.912069
> iter 38 value 92358.084543
> iter 39 value 91728.112271
> iter 40 value 91094.983750
> iter 41 value 91079.219641
> iter 42 value 91063.364399
> iter 43 value 91016.813738
> iter 44 value 90996.858868
> iter 45 value 90995.361598
> iter 46 value 90993.105731
> iter 47 value 89306.608675
> iter 48 value 88017.548661
> iter 49 value 86383.863286
> iter 50 value 85571.267927
> iter 51 value 85382.232224
> iter 52 value 85209.330174
> iter 53 value 85059.090240
> iter 54 value 84994.792230
> iter 55 value 84942.596827
> iter 56 value 84938.675851
> iter 57 value 84933.190249
> iter 58 value 84932.349027
> iter 59 value 84931.504645
> iter 59 value 84931.504645
> final value 84931.504645
> converged

Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS", :
NA in the initial gradient
In addition: There were 16 warnings (use warnings() to see them)

-------Code-----------------------------------------------
##----------------------------------
## estimation using Apollo
##----------------------------------

# /mnt/rdrive/xdu23
# cd /mnt/rdrive/xdu23/Corn\ BE\ Yield\ Impact\

##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")
temp <- which(data_sample$unit_id == 706193 | data_sample$unit_id == 338891) #for this data points, eva=Inf
data_sample <- data_sample[-temp,]

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "LC_MMNL",
modelDescr = "Latent class with continuous random parameters",
indivID = "unit_id",
mixing = TRUE,
nCores = 4,
outputDirectory = "output",
panelData = TRUE
)

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 = 1.13, asc_pln3 = 4.83, asc_pln4 = 9.93, asc_pln5 = 10.20,
asc_pln6 = 10.18, asc_pln7 = 6.07, asc_pln8 = 1.27, asc_pln9 = 11.25, asc_pln10 = 13.24,
asc_pln11 = 12.74, asc_pln12 = 9.42, asc_pln13 = 4.78, asc_pln14 = -0.46, asc_pln15 = 2.54,
asc_pln16 = 6.89, asc_pln17 = 10.76, asc_pln18 = 12.64, asc_pln19 = 12.86, asc_pln20 = 10.64, asc_pln21 = 5.88,
# 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.0009, 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.002,asc_pln15_shift_myield = -0.011, asc_pln16_shift_myield = -0.012,
asc_pln17_shift_myield = -0.001, 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,
delta_a = 0,
#delta_a_mu = 0, delta_a_sig = 0,
delta_b = 0, gamma_atten_a = 0, gamma_atten_b = 0, gamma_d03_a = 0, gamma_d03_b = 0, gamma_d09_a = 0, gamma_d09_b = 0, gamma_acres_a = 0, gamma_acres_b = 0,
gamma_myield_a = 0, gamma_myield_b = 0, gamma_vyield_a = 0, gamma_vyield_b = 0, gamma_disas_a = 0, gamma_disas_b = 0,
mu_log_b_prm_a = -2.5, sigma_log_b_prm_a = 0.23, mu_log_b_prm_b = -2.5, sigma_log_b_prm_b = 0.23,
mu_log_b_sub_a = -2.5, sigma_log_b_sub_a = 0.25, mu_log_b_sub_b = -2.5, sigma_log_b_sub_b = 0.25,
mu_log_b_lia_a = -5, sigma_log_b_lia_a = 0.10, mu_log_b_lia_b = -2.5, sigma_log_b_lia_b = 0.10)

### 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","delta_b", "gamma_atten_b")

# ################################################################# #
#### 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_a"]] = -exp( mu_log_b_prm_a + sigma_log_b_prm_a * draws_prm )
randcoeff[["b_prm_b"]] = -exp( mu_log_b_prm_b + sigma_log_b_prm_b * draws_prm )

randcoeff[["b_sub_a"]] = exp( mu_log_b_sub_a + sigma_log_b_sub_a * draws_sub )
randcoeff[["b_sub_b"]] = exp( mu_log_b_sub_b + sigma_log_b_sub_b * draws_sub )

randcoeff[["b_lia_a"]] = exp( mu_log_b_lia_a + sigma_log_b_lia_a * draws_lia )
randcoeff[["b_lia_b"]] = exp( mu_log_b_lia_b + sigma_log_b_lia_b * draws_lia )

#randcoeff[["delta_a"]] = delta_a_mu + delta_a_sig * draws_pi

return(randcoeff)
}

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_prm"]] = list(b_prm_a, b_prm_b)
lcpars[["b_sub"]] = list(b_sub_a, b_sub_b)
lcpars[["b_lia"]] = list(b_lia_a, b_lia_b)

V=list()
V[["class_a"]] = delta_a + gamma_atten_a*atten + gamma_d03_a * D03 + gamma_d09_a * D09 + gamma_disas_a * disas + gamma_acres_a * acres + gamma_myield_a * yield_mean + gamma_vyield_a * yield_var
V[["class_b"]] = delta_b + gamma_atten_b*atten + gamma_d03_b * D03 + gamma_d09_b * D09 + gamma_disas_b * disas + gamma_acres_b * acres + gamma_myield_b * yield_mean + gamma_vyield_b * yield_var

classAlloc_settings = list(
alternatives = c(class_a=1, class_b=2),
avail = 1,
V = V
)
lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)

return(lcpars)
}

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

### Define settings for MNL model component that are generic across classes
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
)

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


### Loop over classes
for(s in 1:2){

### Compute class-specific utilities
V=list()

### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V[["pln1"]] = asc_pln1 + b_prm[[s]] * prm1 + b_sub[[s]] * sub1 + b_lia[[s]] * lia1
V[["pln2"]] = asc_pln2_value + b_prm[[s]] * prm2 + b_sub[[s]] * sub2 + b_lia[[s]] * lia2
V[["pln3"]] = asc_pln3_value + b_prm[[s]] * prm3 + b_sub[[s]] * sub3 + b_lia[[s]] * lia3
V[["pln4"]] = asc_pln4_value + b_prm[[s]] * prm4 + b_sub[[s]] * sub4 + b_lia[[s]] * lia4
V[["pln5"]] = asc_pln5_value + b_prm[[s]] * prm5 + b_sub[[s]] * sub5 + b_lia[[s]] * lia5
V[["pln6"]] = asc_pln6_value + b_prm[[s]] * prm6 + b_sub[[s]] * sub6 + b_lia[[s]] * lia6
V[["pln7"]] = asc_pln7_value + b_prm[[s]] * prm7 + b_sub[[s]] * sub7 + b_lia[[s]] * lia7
V[["pln8"]] = asc_pln8_value + b_prm[[s]] * prm8 + b_sub[[s]] * sub8 + b_lia[[s]] * lia8
V[["pln9"]] = asc_pln9_value + b_prm[[s]] * prm9 + b_sub[[s]] * sub9 + b_lia[[s]] * lia9
V[["pln10"]] = asc_pln10_value + b_prm[[s]] * prm10 + b_sub[[s]] * sub10 + b_lia[[s]] * lia10
V[["pln11"]] = asc_pln11_value + b_prm[[s]] * prm11 + b_sub[[s]] * sub11 + b_lia[[s]] * lia11
V[["pln12"]] = asc_pln12_value + b_prm[[s]] * prm12 + b_sub[[s]] * sub12 + b_lia[[s]] * lia12
V[["pln13"]] = asc_pln13_value + b_prm[[s]] * prm13 + b_sub[[s]] * sub13 + b_lia[[s]] * lia13
V[["pln14"]] = asc_pln14_value + b_prm[[s]] * prm14 + b_sub[[s]] * sub14 + b_lia[[s]] * lia14
V[["pln15"]] = asc_pln15_value + b_prm[[s]] * prm15 + b_sub[[s]] * sub15 + b_lia[[s]] * lia15
V[["pln16"]] = asc_pln16_value + b_prm[[s]] * prm16 + b_sub[[s]] * sub16 + b_lia[[s]] * lia16
V[["pln17"]] = asc_pln17_value + b_prm[[s]] * prm17 + b_sub[[s]] * sub17 + b_lia[[s]] * lia17
V[["pln18"]] = asc_pln18_value + b_prm[[s]] * prm18 + b_sub[[s]] * sub18 + b_lia[[s]] * lia18
V[["pln19"]] = asc_pln19_value + b_prm[[s]] * prm19 + b_sub[[s]] * sub19 + b_lia[[s]] * lia19
V[["pln20"]] = asc_pln20_value + b_prm[[s]] * prm20 + b_sub[[s]] * sub20 + b_lia[[s]] * lia20
V[["pln21"]] = asc_pln21_value + b_prm[[s]] * prm21 + b_sub[[s]] * sub21 + b_lia[[s]] * lia21

mnl_settings$utilities = V
mnl_settings$componentName = paste0("Class_",s)

### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)

### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs, functionality)

### Average across inter-individual draws within classes
P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
}

### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)

# ### Average across inter-individual draws in class allocation probabilities
# P[["model"]] = apollo_avgInterDraws(P[["model"]], 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: 974
Joined: 24 Apr 2020, 16:29

Re: Error after convergence when estimating a latent class model

Post by stephanehess »

To help us fix this, could you look at the iterations file and see what the values were after the first convergence?
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply