Error in value[[3L]](cond) : unused argument (cond) in in Hybrid MMNL Model
Posted: 28 Oct 2025, 07:17
Hi David and Stephane!
Thank you for developing Apollo—it's an incredible tool that has significantly improved my research efficiency.
I'm currently working on a hybrid model combining MMNL with Latent Variables (LV) that include attributes. During estimation, I encounter the following error:
Error in value[[3L]](cond) : unused argument (cond).
I've reviewed my code but haven't been able to identify the source of the bug. Could you please take a look at the relevant section of my code below? Thank you so much!
Thank you for developing Apollo—it's an incredible tool that has significantly improved my research efficiency.
I'm currently working on a hybrid model combining MMNL with Latent Variables (LV) that include attributes. During estimation, I encounter the following error:
Error in value[[3L]](cond) : unused argument (cond).
I've reviewed my code but haven't been able to identify the source of the bug. Could you please take a look at the relevant section of my code below? Thank you so much!
Code: Select all
### Load Apollo library
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="hybrid",
modelDescr = "hybrid with wtp spacce",
indivID ="id",
# mixing = TRUE,
# panelData = FALSE,
outputDirectory = "hybrid_MMNL_wtpspace",
nCores = 7
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("D:/BaiduSyncdisk/0A_Erhai certifiactes/data/8rows.csv",header=TRUE)
# database <- subset(database, basePrice <= 750) # upper <- Q3 + 1.5 * IQR
database <- subset(database, basePrice <= 1050) # upper <- Q3 + 3 * IQR
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(
asc_alt4=0,
mu_b_ec= 0.1,
sigma_b_ec=0.1,
mu_b_no= 0.1,
sigma_b_no=0.1,
mu_b_ev=0.1,
sigma_b_ev=0.1,
mu_b_rat = 0.02,
sigma_b_rat=0.1,
mu_b_trans=0.1,
sigma_b_trans=0.1,
mu_b_qua=0.1,
sigma_b_qua=0.1,
mu_b_p = -0.1,
sigma_b_p=0.1,
lambda_ec = -0.03,
lambda_no= 0.0005,
lambda_ev = 0.1,
lambda_rat = -0.1,
lambda_trans = -0.1,
lambda_qua=0.1,
#### pending
gamma_lv_age1 = -0.25,
gamma_lv_age3 = -0.25,
gamma_lv_gen1 = 0.2,
gamma_lv_inc=0.1,
gamma_lv_edu = 0,
gamma_lv_sin = 0,
gamma_lv_chi = 0,
gamma_lv_stu = 0,
gamma_lv_une = 0,
gamma_lv_ret = 0,
zeta_aa1 = 1,
zeta_aa2 = 1,
zeta_aa3 = 1,
zeta_pa1 = 1,
zeta_pa2 = 1,
zeta_pa3 = 1,
zeta_psb1 = 1,
zeta_psb2 = 1,
zeta_psb3 = 1,
zeta_pi1 = 1,
zeta_pi2 = 1,
zeta_pi3 = 1,
tau_aa1_1 = -3,
# tau_aa1_2 = -2,
tau_aa1_3 = -1,
tau_aa1_4 = 1,
tau_aa1_5 = 2,
tau_aa1_6 = 3,
tau_aa2_1 = -3,
tau_aa2_2 = -2,
tau_aa2_3 = -1,
tau_aa2_4 = 1,
tau_aa2_5 = 2,
tau_aa2_6 = 3,
# tau_aa2_1 = -3,
# tau_aa2_2 = -2,
tau_aa3_3 = -1,
tau_aa3_4 = 1,
tau_aa3_5 = 2,
tau_aa3_6 = 3,
tau_pa1_1 = -3,
tau_pa1_2 = -2,
tau_pa1_3 = -1,
tau_pa1_4 = 1,
tau_pa1_5 = 2,
tau_pa1_6 = 3,
# tau_aa2_1 = -3,
tau_pa2_2 = -2,
tau_pa2_3 = -1,
tau_pa2_4 = 1,
tau_pa2_5 = 2,
tau_pa2_6 = 3,
# tau_aa2_1 = -3,
tau_pa3_2 = -2,
tau_pa3_3 = -1,
tau_pa3_4 = 1,
tau_pa3_5 = 2,
tau_pa3_6 = 3,
tau_psb1_1 = -3,
tau_psb1_2 = -2,
tau_psb1_3 = -1,
tau_psb1_4 = 1,
tau_psb1_5 = 2,
tau_psb1_6 = 3,
tau_psb2_1 = -3,
tau_psb2_2 = -2,
tau_psb2_3 = -1,
tau_psb2_4 = 1,
tau_psb2_5 = 2,
tau_psb2_6 = 3,
tau_psb3_1 = -3,
tau_psb3_2 = -2,
tau_psb3_3 = -1,
tau_psb3_4 = 1,
tau_psb3_5 = 2,
tau_psb3_6 = 3,
tau_pi1_1 = -3,
tau_pi1_2 = -2,
tau_pi1_3 = -1,
tau_pi1_4 = 1,
tau_pi1_5 = 2,
tau_pi1_6 = 3,
# tau_pi1_1 = -3,
# tau_pi1_2 = -2,
tau_pi2_3 = -1,
tau_pi2_4 = 1,
tau_pi2_5 = 2,
tau_pi2_6 = 3,
# tau_pi1_1 = -3,
# tau_pi1_2 = -2,
tau_pi3_3 = -1,
tau_pi3_4 = 1,
tau_pi3_5 = 2,
tau_pi3_6 = 3
)
### 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_fixed = c()
### Set parameters for generating draws
apollo_draws = list(
# interDrawsType = "halton",
interDrawsType = "pmc",
interNDraws = 100,
interUnifDraws = c(),
interNormDraws=c("draws_ec","draws_no","draws_ev","draws_rat","draws_trans","draws_qua","draws_p","eta"),
#intraDrawsType = "halton",
intraDrawsType = "pmc",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)
### Create random parameters
###? how to decide distribution
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
### pending
randcoeff[["LV"]] = gamma_lv_age1*(age==1) + gamma_lv_age3*(age==3) +gamma_lv_gen1*(gen==1)+gamma_lv_sin*(car==1)+gamma_lv_chi*(gen==2) + gamma_lv_inc*inc +gamma_lv_edu*edu +gamma_lv_stu*(car==1)+ gamma_lv_une*(car==8)+gamma_lv_ret*(car==9)+eta
##########supllement later##################################################
randcoeff[["wtp_ec"]] = mu_b_ec + sigma_b_ec * draws_ec
+lambda_ec*(gamma_lv_age1*(age==1) + gamma_lv_age3*(age==3) +gamma_lv_gen1*(gen==1)+gamma_lv_sin*(car==1)+gamma_lv_chi*(gen==2) + gamma_lv_inc*inc +gamma_lv_edu*edu +gamma_lv_stu*(car==1)+ gamma_lv_une*(car==8)+gamma_lv_ret*(car==9)+eta
)
randcoeff[["wtp_no"]] = mu_b_no + sigma_b_no * draws_no
randcoeff[["wtp_ev"]] = mu_b_ev + sigma_b_ev * draws_ev
randcoeff[["wtp_rat"]] = mu_b_rat + sigma_b_rat * draws_rat
randcoeff[["wtp_trans"]] = mu_b_trans + sigma_b_trans * draws_trans
randcoeff[["wtp_qua"]] = mu_b_qua + sigma_b_qua * draws_qua
randcoeff[["b_p"]] = -exp(mu_b_p + sigma_b_p * draws_p)
return(randcoeff)
}
# ################################################################# #
#### 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()
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = aa1,
V = zeta_aa1*LV,
tau = c(tau_aa1_1, tau_aa1_3,tau_aa1_4, tau_aa1_5, tau_aa1_6),
coding = c(1,3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_aa1")
ol_settings2 = list(outcomeOrdered = aa2,
V = zeta_aa2*LV,
tau = c(tau_aa2_1, tau_aa2_2, tau_aa2_3, tau_aa2_4, tau_aa2_5, tau_aa2_6),
rows =(choiceid==1),
componentName = "indic_aa2")
ol_settings3 = list(outcomeOrdered = aa3,
V = zeta_aa3*LV,
tau = c( tau_aa3_3, tau_aa3_4, tau_aa3_5,tau_aa3_6),
coding = c(3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_aa3")
ol_settings4 = list(outcomeOrdered = pa1,
V = zeta_pa1*LV,
tau = c(tau_pa1_1, tau_pa1_2, tau_pa1_3,tau_pa1_4,tau_pa1_5,tau_pa1_6),
rows =(choiceid==1),
componentName = "indic_pa1")
ol_settings5 = list(outcomeOrdered = pa2,
V = zeta_pa2*LV,
tau = c(tau_pa2_2, tau_pa2_3,tau_pa2_4,tau_pa2_5,tau_pa2_6),
coding = c(2,3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_pa2")
ol_settings6 = list(outcomeOrdered = pa3,
V = zeta_pa3*LV,
tau = c(tau_pa3_2, tau_pa3_3,tau_pa3_4,tau_pa3_5,tau_pa3_6),
coding = c(2,3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_pa3")
ol_settings7= list(outcomeOrdered = psb1,
V = zeta_psb1*LV,
tau = c(tau_psb1_1, tau_psb1_2, tau_psb1_3,tau_psb1_4,tau_psb1_5,tau_psb1_6),
rows =(choiceid==1),
componentName = "indic_psb1")
ol_settings8= list(outcomeOrdered = psb2,
V = zeta_psb2*LV,
tau = c(tau_psb2_1, tau_psb2_2, tau_psb2_3,tau_psb2_4,tau_psb2_5,tau_psb2_6),
rows =(choiceid==1),
componentName = "indic_psb2")
ol_settings9 = list(outcomeOrdered = psb3,
V = zeta_psb3*LV,
tau = c(tau_psb3_1, tau_psb3_2, tau_psb3_3,tau_psb3_4,tau_psb3_5,tau_psb3_6),
rows =(choiceid==1),
componentName = "indic_psb3")
ol_settings10 = list(outcomeOrdered = pi1,
V = zeta_pi1*LV,
tau = c(tau_pi1_1, tau_pi1_2, tau_pi1_3,tau_pi1_4,tau_pi1_5,tau_pi1_6),
rows =(choiceid==1),
componentName = "indic_pi1")
ol_settings11 = list(outcomeOrdered = pi2,
V = zeta_pi2*LV,
tau = c(tau_pi2_3,tau_pi2_4,tau_pi2_5,tau_pi2_6),
coding = c(3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_pi2")
ol_settings12 = list(outcomeOrdered = pi3,
V = zeta_pi3*LV,
tau = c(tau_pi3_3,tau_pi3_4,tau_pi3_5,tau_pi3_6),
coding = c(3,4,5,6,7),
rows =(choiceid==1),
componentName = "indic_pi3")
P[["indic_aa1"]] = apollo_ol(ol_settings1, functionality)
P[["indic_aa1"]] = apollo_panelProd(P[["indic_aa1"]] , apollo_inputs, functionality)
P[["indic_aa2"]] = apollo_ol(ol_settings2, functionality)
P[["indic_aa2"]] = apollo_panelProd(P[["indic_aa2"]] , apollo_inputs, functionality)
P[["indic_aa3"]] = apollo_ol(ol_settings3, functionality)
P[["indic_aa3"]] = apollo_panelProd(P[["indic_aa3"]] , apollo_inputs, functionality)
P[["indic_pa1"]] = apollo_ol(ol_settings4, functionality)
P[["indic_pa1"]] = apollo_panelProd(P[["indic_pa1"]] , apollo_inputs, functionality)
P[["indic_pa2"]] = apollo_ol(ol_settings5, functionality)
P[["indic_pa2"]] = apollo_panelProd(P[["indic_pa2"]] , apollo_inputs, functionality)
P[["indic_pa3"]] = apollo_ol(ol_settings6, functionality)
P[["indic_pa3"]] = apollo_panelProd(P[["indic_pa3"]] , apollo_inputs, functionality)
P[["indic_psb1"]] = apollo_ol(ol_settings7, functionality)
P[["indic_psb1"]] = apollo_panelProd(P[["indic_psb1"]] , apollo_inputs, functionality)
P[["indic_psb2"]] = apollo_ol(ol_settings8, functionality)
P[["indic_psb2"]] = apollo_panelProd(P[["indic_psb2"]] , apollo_inputs, functionality)
P[["indic_psb3"]] = apollo_ol(ol_settings9, functionality)
P[["indic_psb3"]] = apollo_panelProd(P[["indic_psb3"]] , apollo_inputs, functionality)
P[["indic_pi1"]] = apollo_ol(ol_settings10, functionality)
P[["indic_pi1"]] = apollo_panelProd(P[["indic_pi1"]] , apollo_inputs, functionality)
P[["indic_pi2"]] = apollo_ol(ol_settings11, functionality)
P[["indic_pi2"]] = apollo_panelProd(P[["indic_pi2"]] , apollo_inputs, functionality)
P[["indic_pi3"]] = apollo_ol(ol_settings12, functionality)
P[["indic_pi3"]] = apollo_panelProd(P[["indic_pi3"]] , apollo_inputs, functionality)
### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['alt1']] = b_p* (-( wtp_ec*(cer_alt1==0) +wtp_no*(cer_alt1==2)+
wtp_ev*(vie_alt1==1) +
wtp_rat*(rat_alt1) +
wtp_trans*trans_alt1 +
wtp_qua*qua_alt1)+
p_alt1/100
)
V[['alt2']] = b_p*(-( wtp_ec*(cer_alt2==0) +wtp_no*(cer_alt2==2)+
wtp_ev*(vie_alt2==1) +
wtp_rat*(rat_alt2) +
wtp_trans*trans_alt2 +
wtp_qua*qua_alt2)+
p_alt2/100
)
V[['alt3']] = b_p*(-( wtp_ec*(cer_alt3==0) +wtp_no*(cer_alt3==2)+
wtp_ev*(vie_alt3==1) +
wtp_rat*(rat_alt3) +
wtp_trans*trans_alt3 +
wtp_qua*qua_alt3)+
p_alt3/100
)
V[['alt4']] = ( asc_alt4)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3,alt4=4),
avail = list(alt1=1, alt2=1, alt3=1, alt4=1),
choiceVar = choice_alt12,
utilities = V
)
### Compute probabilities for MNL model component
P[["choice"]] = apollo_mnl(mnl_settings, functionality)
P[["choice"]] = apollo_panelProd(P[["choice"]] , apollo_inputs, functionality)
### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, functionality)
### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Optional: calculate LL before model estimation
# apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)