Hi Stephen
I am trying a LCCM model using RP and SP data. I am getting the following error.
"Error in apollo_mnl(c(mnl_settings_RP, componentName2 = paste0("RP_class_", :
CALCULATION ISSUE - Some utilities for model component "RP_class_1" contain values that are not finite numbers!".
The code of the model is given below, please provide a fix. Thanks and Regards
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "LC_TwoClasses_RP_SP",
modelDescr = "LC model on RP and SP conf, 2 classes with socio-demographic variables",
indivID = "ID",
nCores = 11,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = DATA_POOLED # Load dataset
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters
apollo_beta = c(
asc_1 = 0, asc_2 = 0, asc_3 = 0,
b_SW_a = 0, b_SW_b = 0,
b_CT_a = 0, b_CT_b = 0,
b_Meals_a= 0, b_Meals_b = 0,
b_Fare_a = 0, b_Fare_b = 0,
b_Legroom_a = 0, b_Legroom_b = 0,
b_OTP_a = 0, b_OTP_b = 0,
b_ET_a = 0, b_ET_b = 0,
### Class allocation parameters including socio-demographics
delta_a = 0, delta_b = 0,
### Latent variables (if kept)
gamma_LV1_a = 0, gamma_LV2_a = 0, gamma_LV3_a = 0,
gamma_LV1_b = 0, gamma_LV2_b = 0, gamma_LV3_b = 0,
### NEW PARAMETERS: Socio-demographics in class allocation
beta_age_a = 0, beta_age_b = 0 ,
#beta_gender_a = 0, beta_gender_b = 0,
beta_income_a = 0, beta_income_b = 0,
mu_RP = 1,
mu_SP = 1
)
### Vector with fixed parameters
apollo_fixed = c("asc_1", "mu_RP", "delta_b", "gamma_LV1_b", "gamma_LV2_b", "gamma_LV3_b", "beta_age_b", "beta_income_b" ) # Fix reference class for identification
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars = function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_SW"]] = list(b_SW_a, b_SW_b)
lcpars[["b_CT"]] = list(b_CT_a, b_CT_b)
lcpars[["b_Meals"]] = list(b_Meals_a, b_Meals_b)
lcpars[["b_Fare"]] = list(b_Fare_a, b_Fare_b)
lcpars[["b_Legroom"]] = list(b_Legroom_a, b_Legroom_b)
lcpars[["b_OTP"]] = list(b_OTP_a, b_OTP_b)
lcpars[["b_ET"]] = list(b_ET_a, b_ET_b)
### Class allocation utility functions including latent variables and socio-demographics
V = list()
V[["class_a"]] = delta_a + gamma_LV1_a * LV1 + gamma_LV2_a * LV2 + gamma_LV3_a * LV3 +
beta_age_a * Age + beta_income_a * income
V[["class_b"]] = delta_b + gamma_LV1_b * LV1 + gamma_LV2_b * LV2 + gamma_LV3_b * LV3 +
beta_age_b * Age + beta_income_b * income
classAlloc_settings = list(
classes = c(class_a=1, class_b=2),
utilities = 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()
P_temp = list()
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3),
avail = list(alt1=1, alt2=1, alt3=1),
choiceVar = Choice
)
### Loop over classes
for(s in 1:2){
V = list()
V[["alt1"]] = asc_1 + b_SW[[s]]*(SW_N == "20") + b_Meals[[s]]*Meals_N + b_Fare[[s]]*Fare_N_L + b_ET[[s]]*ET_N_L
V[["alt2"]] = asc_2 + b_SW[[s]]*(SW_D == "20") + b_CT[[s]]*CT_D_L + b_Meals[[s]]*Meals_D + b_Fare[[s]]*Fare_D_L +
b_Legroom[[s]]*(Legroom_D == "34") + b_OTP[[s]]*OTP_D + b_ET[[s]]*ET_D_L
V[["alt3"]] = asc_3 + b_SW[[s]]*(SW_C1 == "20") + b_CT[[s]]*CT_C_L + b_Meals[[s]]*Meals_C1 + b_Fare[[s]]*Fare_C_L +
b_Legroom[[s]]*(Legroom_C1 == "34") + b_OTP[[s]]*OTP_C1 + b_ET[[s]]*ET_C_L
### Compute probabilities for the RP part of the data using MNL model
mnl_settings_RP = list(
alternatives = c(nonstop=1, direct=2, connected =3),
avail = list(nonstop=1, direct=1, connected =1),
choiceVar = Choice,
utilities = list(nonstop = mu_RP*V[["nonstop"]],
direct = mu_RP*V[["direct"]],
connected = mu_RP*V[["connected"]]),
rows = (RP==1)
)
P_temp[[paste0("RP_class_",s)]] = apollo_mnl(mnl_settings_RP, functionality)
### Compute probabilities for the SP part of the data using MNL model
mnl_settings_SP = list(
alternatives = c(nonstop=1, direct=2, connected =3),
avail = list(nonstop=1, direct=1, connected =1),
choiceVar = Choice,
utilities = list(nonstop = mu_SP*V[["nonstop"]],
direct = mu_SP*V[["direct"]],
connected = mu_SP*V[["connected"]]),
rows = (SP==1)
)
P_temp[[paste0("SP_class_",s)]] = apollo_mnl(mnl_settings_SP, functionality)
### Combine models, then apply panelProd
P[[paste0("Class_",s)]] = apollo_combineModels(P_temp, apollo_inputs, functionality,
components=c(paste0("RP_class_",s),paste0("SP_class_",s)),asList=FALSE)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(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)
### Prepare and return outputs
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model_two_classes = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
apollo_modelOutput(model_two_classes)
apollo_saveOutput(model_two_classes)
Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- posts will not immediately appear on the forum, but will be checked by a moderator first. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.
Error in LCCM using RP SP data
-
stephanehess
- Site Admin
- Posts: 1355
- Joined: 24 Apr 2020, 16:29
Re: Error in LCCM using RP SP data
Hi
do you have some NA in your data? That would cause this problem
Stephane
do you have some NA in your data? That would cause this problem
Stephane
Re: Error in LCCM using RP SP data
Thanks for the response,
Yes I do have some variables which are specific to SP only. For those variables in RP i have put NA.
Is there any way to handle NAs.
Yes I do have some variables which are specific to SP only. For those variables in RP i have put NA.
Is there any way to handle NAs.
-
stephanehess
- Site Admin
- Posts: 1355
- Joined: 24 Apr 2020, 16:29
Re: Error in LCCM using RP SP data
Hi
just put 0 instead of NA.
Stephane
just put 0 instead of NA.
Stephane