Error in LC MMNL
Posted: 28 May 2025, 12:31
Dear Stephane and Apollo team,
I am trying to construct a Latent Class Mixed Multinomial Model following your 2012 paper for data that stems from a DCE. The experiment has two unlabeled alternatives and 6 attributes. However, I have been running into an error and cannot figure out how to solve it. I am using the latest versions of R and Apollo (0.3.5).
This is the code:
And this the output:
Do you have any idea where the error might come from? I appreciate your time and help.
Best,
Sophie
I am trying to construct a Latent Class Mixed Multinomial Model following your 2012 paper for data that stems from a DCE. The experiment has two unlabeled alternatives and 6 attributes. However, I have been running into an error and cannot figure out how to solve it. I am using the latest versions of R and Apollo (0.3.5).
This is the code:
Code: Select all
apollo_initialise()
apollo_control = list(
modelName = "LCMMNL_Model",
modelDescr = "Latent class mixed multinomial logit",
indivID = "pat_id",
outputDirectory = "output",
nCores = 4,
panelData = TRUE,
mixing = TRUE
)
apollo_beta = c(
asc2 = 0,
asc1 = 0,
mu_adverse = -0.3,
mu_protect = 0.2,
mu_admin = 0.1,
mu_doses = -0.1,
mu_price = -0.5,
mu_popfam = 0.15,
mu_popchild = 0.1,
sigma_adverse = 0.1,
sigma_protect = 0.1,
sigma_admin = 0.1,
sigma_doses = 0.1,
sigma_price = 0.1,
sigma_popfam = 0.1,
sigma_popchild= 0.1,
delta2 = -2,
delta3 = -1,
delta4 = 0,
delta5 = 1,
delta6 = 2,
delta7 = 3,
delta8 = 4
)
apollo_fixed = c("asc1")
apollo_draws = list(
interDrawsType = "sobol",
interNDraws = 500,
interNormDraws = c("draws_adverse", "draws_protect", "draws_admin",
"draws_doses", "draws_price", "draws_popfam", "draws_popchild")
)
apollo_randCoeff = function(apollo_beta, apollo_inputs) {
with(as.list(apollo_beta), {
list(
beta_adverse = mu_adverse + sigma_adverse * draws_adverse,
beta_protect = mu_protect + sigma_protect * draws_protect,
beta_admin = mu_admin + sigma_admin * draws_admin,
beta_doses = mu_doses + sigma_doses * draws_doses,
beta_price = mu_price + sigma_price * draws_price,
beta_pop_fam = mu_popfam + sigma_popfam * draws_popfam,
beta_pop_child = mu_popchild + sigma_popchild * draws_popchild
)
})
}
apollo_lcPars = function(apollo_beta, apollo_inputs) {
n <- nrow(apollo_inputs$database)
V <- list(
class1 = rep(0, n),
class2 = rep(delta2, n),
class3 = rep(delta3, n),
class4 = rep(delta4, n),
class5 = rep(delta5, n),
class6 = rep(delta6, n),
class7 = rep(delta7, n),
class8 = rep(delta8, n)
)
expV <- lapply(V, exp)
denom <- Reduce(`+`, expV)
pi_values <- lapply(expV, function(ev) ev / denom)
return(list(pi_values = pi_values))
}
apollo_probabilities = function(apollo_beta, apollo_inputs, functionality = "estimate") {
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P <- list()
for (k in 1:8) {
V <- list()
V[["alt1"]] = 0
if (k != 2) V[["alt1"]] = V[["alt1"]] + beta_adverse * adverse_react1
if (k != 3) V[["alt1"]] = V[["alt1"]] + beta_protect * protection_duration1
if (k != 4) V[["alt1"]] = V[["alt1"]] + beta_admin * administration1
if (k != 5) V[["alt1"]] = V[["alt1"]] + beta_doses * doses1
if (k != 6) V[["alt1"]] = V[["alt1"]] + beta_price * price1
if (k != 7) V[["alt1"]] = V[["alt1"]] + beta_pop_fam * target_fam1
if (k != 8) V[["alt1"]] = V[["alt1"]] + beta_pop_child * target_hijos1
V[["alt2"]] = asc2
if (k != 2) V[["alt2"]] = V[["alt2"]] + beta_adverse * adverse_react2
if (k != 3) V[["alt2"]] = V[["alt2"]] + beta_protect * protection_duration2
if (k != 4) V[["alt2"]] = V[["alt2"]] + beta_admin * administration2
if (k != 5) V[["alt2"]] = V[["alt2"]] + beta_doses * doses2
if (k != 6) V[["alt2"]] = V[["alt2"]] + beta_price * price2
if (k != 7) V[["alt2"]] = V[["alt2"]] + beta_pop_fam * target_fam2
if (k != 8) V[["alt2"]] = V[["alt2"]] + beta_pop_child * target_hijos2
mnl_settings = list(
alternatives = c(alt1 = 1, alt2 = 2),
avail = list(alt1 = 1, alt2 = 1),
choiceVar = choice,
utilities = V,
componentName = paste0("Class_", k)
)
P[[paste0("Class_", k)]] = apollo_mnl(mnl_settings, functionality)
}
lcpars = apollo_inputs$apollo_lcPars(apollo_beta, apollo_inputs)
lc_settings = list(
inClassProb = P,
classProb = lcpars$pi_values,
componentName = "lc"
)
P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
P[["model"]] = apollo_panelProd(P[["model"]], apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
apollo_inputs = apollo_validateInputs()
apollo_inputs$apollo_lcPars <- apollo_lcPars
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
apollo_modelOutput(model)
And this the output:
Code: Select all
> apollo_inputs = apollo_validateInputs()
apollo_draws and apollo_randCoeff were found, so apollo_control$mixing was set to TRUE
All checks on apollo_control completed.
All checks on database completed.
Generating inter-individual draws ....... Done
> apollo_inputs$apollo_lcPars <- apollo_lcPars
>
> model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
Error in test && (as.character(e[[3]][[1]]) %in% c("apollo_mnl", "apollo_el", :
'length = 3' in coercion to 'logical(1)'[code]Do you have any idea where the error might come from? I appreciate your time and help.
Best,
Sophie