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

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Mustansir
Posts: 2
Joined: 06 Aug 2022, 11:18

Error in LCCM using RP SP data

Post by Mustansir »

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)
stephanehess
Site Admin
Posts: 1355
Joined: 24 Apr 2020, 16:29

Re: Error in LCCM using RP SP data

Post by stephanehess »

Hi

do you have some NA in your data? That would cause this problem

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Mustansir
Posts: 2
Joined: 06 Aug 2022, 11:18

Re: Error in LCCM using RP SP data

Post by Mustansir »

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.
stephanehess
Site Admin
Posts: 1355
Joined: 24 Apr 2020, 16:29

Re: Error in LCCM using RP SP data

Post by stephanehess »

Hi

just put 0 instead of NA.

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