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 in 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
hanno_kriek
Posts: 1
Joined: 09 Mar 2022, 09:05

Error in Latent Class Model

Post by hanno_kriek »

Hello,

I am estimating a latent class model. Yet, I am running into some problems. My code is working for 2 and 3 classes, although I do get an error message in 3 classes, but my code is not working for 4 and 5 classes.

In the estimation the following error messages are coming up for the 4 and 5 classes.

Testing likelihood function...
Pre-processing likelihood function...
Preparing workers for multithreading...

##################################################################################################################################
This model could potentially be estimated faster using analytical gradients, yet some issue is preventing it from using them. You might want to
ask for help in the Apollo forum (http://www.apollochoicemodelling.com/forum) on how to solve this issue. If you do, please post your code
and data (if not confidential).
##################################################################################################################################

Testing influence of parametersError in checkForRemoteErrors(lapply(cl, recvResult)) :
3 nodes produced errors; first error: invalid (do_set) left-hand side to assignment


The following is what comes up if I use - traceback()

5: stop(count, " nodes produced errors; first error: ", firstmsg,
domain = NA)
4: checkForRemoteErrors(lapply(cl, recvResult))
3: parallel::clusterCall(cl = cl, fun = apollo_parProb, b = b)
2: apollo_logLike(beta1)
1: apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities,
apollo_inputs)



The message between the ## also show up in the 3 Class model. That is also why I am posting this message to ask for some help and figure out what is going wrong.

If you have any suggestions why this could be and how to solve it, please could you advise.


My code is the following:

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="LC 4 class",
modelDescr ="LC 4",
indivID ="ID",
nCores = 3, #########Make 4???
noDiagnostics = TRUE

)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
load("database2.RData")
database = database2

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
asc_SQ_1 = 0,
asc_SQ_2 = 0,
asc_SQ_3 = 0,
asc_SQ_4 = 0,

beta_intro_lion_a = 0,
beta_intro_lion_b = 0,
beta_intro_lion_c = 0,
beta_intro_lion_d = 0,

beta_intro_che_a = 0,
beta_intro_che_b = 0,
beta_intro_che_c = 0,
beta_intro_che_d = 0,

beta_eco_threat_a = 0,
beta_eco_threat_b = 0,
beta_eco_threat_c = 0,
beta_eco_threat_d = 0,

beta_eco_nonthreat_a = 0,
beta_eco_nonthreat_b = 0,
beta_eco_nonthreat_c = 0,
beta_eco_nonthreat_d = 0,

beta_icon_roan_a = 0,
beta_icon_roan_b = 0,
beta_icon_roan_c = 0,
beta_icon_roan_d = 0,

beta_icon_rhino_a = 0,
beta_icon_rhino_b = 0,
beta_icon_rhino_c = 0,
beta_icon_rhino_d = 0,

beta_icon_tsess_a = 0,
beta_icon_tsess_b = 0,
beta_icon_tsess_c = 0,
beta_icon_tsess_d = 0,

beta_price_a = 0,
beta_price_b = 0,
beta_price_c = 0,
beta_price_d = 0,

delta_a = 0,
delta_b = 0,
delta_c = 0,
delta_d = 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_beta_fixed = c()

apollo_fixed = c("delta_a")

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()

lcpars[["asc_SQ"]] = list(asc_SQ_1, asc_SQ_2, asc_SQ_3, asc_SQ_4)
lcpars[["beta_intro_lion"]] = list(beta_intro_lion_a, beta_intro_lion_b, beta_intro_lion_c, beta_intro_lion_d)
lcpars[["beta_intro_che"]] = list(beta_intro_che_a, beta_intro_che_b, beta_intro_che_c, beta_intro_che_d)
lcpars[["beta_eco_threat"]] = list(beta_eco_threat_a, beta_eco_threat_b, beta_eco_threat_c, beta_eco_threat_d)
lcpars[["beta_eco_nonthreat"]] = list(beta_eco_nonthreat_a, beta_eco_nonthreat_b, beta_eco_nonthreat_c, beta_eco_nonthreat_d)
lcpars[["beta_icon_roan"]] = list(beta_icon_roan_a, beta_icon_roan_b, beta_icon_roan_c, beta_icon_roan_d)
lcpars[["beta_icon_rhino"]] = list(beta_icon_rhino_a, beta_icon_rhino_b, beta_icon_rhino_c, beta_icon_rhino_d)
lcpars[["beta_icon_tsess"]] = list(beta_icon_tsess_a, beta_icon_tsess_b, beta_icon_tsess_c, beta_icon_tsess_d)
lcpars[["beta_price"]] = list(beta_price_a, beta_price_b, beta_price_c, beta_price_d)


V=list()
V[["class_a"]] = delta_a
V[["class_b"]] = delta_b
V[["class_c"]] = delta_c
V[["class_d"]] = delta_d

mnl_settings = list(
alternatives = c(class_a=1, class_b=2,class_c=3, class_d=4),
avail = 1,
choiceVar = NA,
V = V
)
lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")

lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)

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(one=1, two=2, SQ=3),
avail = list(one=1, two=1, SQ=1),
choiceVar = Choice
)

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


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

V[['one']] =
beta_intro_lion[[s]]*alt1_car_lion +
beta_intro_che[[s]]*alt1_car_che +
beta_eco_threat[[s]]*alt1_eco_threat +
beta_eco_nonthreat[[s]]*alt1_eco_nonthreat +
beta_icon_roan[[s]]*alt1_endg_roan +
beta_icon_rhino[[s]]*alt1_endg_rhino +
beta_icon_tsess[[s]]*alt1_endg_tsess +
beta_price[[s]]*alt1.price

V[['two']] =
beta_intro_lion[[s]]*alt2_car_lion +
beta_intro_che[[s]]*alt2_car_che +
beta_eco_threat[[s]]*alt2_eco_threat +
beta_eco_nonthreat[[s]]*alt2_eco_nonthreat +
beta_icon_roan[[s]]*alt2_endg_roan +
beta_icon_rhino[[s]]*alt2_endg_rhino +
beta_icon_tsess[[s]]*alt2_endg_tsess +
beta_price[[s]]*alt2.price

V[['SQ']] = asc_SQ[[s]] + beta_price[[s]]* sq.price


mnl_settings$V = 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)

s=s+1
}

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

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

### Optional starting values search
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

traceback()
#Output to screen
apollo_modelOutput(model)



And my Output is the above as mentioned.


Thank you very much

Hanno
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error in Latent Class Model

Post by stephanehess »

Hanno

could you please first confirm what version of Apollo you are using?

You are also still using the old Apollo syntax for latent class, please see the new examples on the website

Thanks

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