Page 1 of 1

Error in LCCM using RP SP data

Posted: 13 Mar 2025, 09:27
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)

Re: Error in LCCM using RP SP data

Posted: 13 Mar 2025, 09:45
by stephanehess
Hi

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

Stephane

Re: Error in LCCM using RP SP data

Posted: 13 Mar 2025, 09:56
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.

Re: Error in LCCM using RP SP data

Posted: 20 Mar 2025, 08:18
by stephanehess
Hi

just put 0 instead of NA.

Stephane