I am trying to run a Hybrid latent class choice model for my dataset. I have tried to develop the model in both ways (LVs as a part of class membership and LVs as a part of class specific model). However I am getting the following error: Error in f(init, x[]) : non-conformable arrays[/b in both the cases.
Could you please look into the same?
Please find the code for LVs as a part of class allocation/membership
Code: Select all
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Apollo_GS",
modelDescr = "Hybrid latent class choice",
indivID = "ID",
nCores = 16,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("Apollo_CS.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(b_tc_cs_A = 0,
b_tc_ncs_A = 0,
b_tt_cs_A = 0,
b_tt_ncs_A = 0,
b_service_frequency_cs_A = 0,
b_Shopping_card_A = 0,
b_Loan_A = 1,
b_Wifi_A = 0,
b_tc_cs_B = 0,
b_tc_ncs_B = 0,
b_tt_cs_B = 0,
b_tt_ncs_B = 0,
b_service_frequency_cs_B = 0,
b_Shopping_card_B = 0,
b_Loan_B = 1,
b_Wifi_B = 0,
asc_cs_A = 1,
asc_ncs_A = 0,
asc_cs_B = 1,
asc_ncs_B = 0,
b_Mode_use_Car_A = 0,
b_female_A = 0,
b_Age_21_30_A = 0,
b_Age_40_60_A = 0,
b_Income_0_50k_A = 0,
b_Income_above_100k_A = 0,
b_Education_PG_A = 0,
Lambda_C_A = 0,
Lambda_O_A = 0,
b_Mode_use_Car_B = 0,
b_female_B = 0,
b_Age_21_30_B = 0,
b_Age_40_60_B = 0,
b_Income_0_50k_B = 0,
b_Income_above_100k_B = 0,
b_Education_PG_B = 0,
Lambda_C_B = 0,
Lambda_O_B = 0,
zeta_C_a = 1,
zeta_C_b = 1,
zeta_C_c = 1,
zeta_C_d = 1,
zeta_O_a = 1,
zeta_O_b = 1,
zeta_O_c = 1,
tau_C_a_1 = -2,
tau_C_a_2 = -1,
tau_C_a_3 = 1,
tau_C_a_4 = 2,
tau_C_b_1 = -2,
tau_C_b_2 = -1,
tau_C_b_3 = 1,
tau_C_b_4 = 2,
tau_C_c_1 = -2,
tau_C_c_2 = -1,
tau_C_c_3 = 1,
tau_C_c_4 = 2,
tau_C_d_1 = -2,
tau_C_d_2 = -1,
tau_C_d_3 = 1,
tau_C_d_4 = 2,
tau_O_a_1 = -2,
tau_O_a_2 = -1,
tau_O_a_3 = 1,
tau_O_a_4 = 2,
tau_O_b_1 = -2,
tau_O_b_2 = -1,
tau_O_b_3 = 1,
tau_O_b_4 = 2,
tau_O_c_1 = -2,
tau_O_c_2 = -1,
tau_O_c_3 = 1,
tau_O_c_4 = 2,
delta_a = 0,
delta_b = 0)
### 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("asc_cs_A", "b_Loan_A", "delta_a")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="halton",
interNDraws=100,
interUnifDraws=c(),
interNormDraws=c("eta_C", "eta_O"),
intraDrawsType="",
intraNDraws=0,
intraUnifDraws=c(),
intraNormDraws=c()
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["LV_C"]] = eta_C
randcoeff[["LV_O"]] = eta_O
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_tc_cs"]] = list(b_tc_cs_A , b_tc_cs_B)
lcpars[["b_tc_ncs"]] = list(b_tc_ncs_A , b_tc_ncs_B)
lcpars[["b_tt_cs"]] = list(b_tt_cs_A , b_tt_cs_B)
lcpars[["b_tt_ncs"]] = list(b_tt_ncs_A , b_tt_ncs_B)
lcpars[["b_service_frequency_cs"]] = list(b_service_frequency_cs_A , b_service_frequency_cs_B)
lcpars[["b_Shopping_card"]] = list(b_Shopping_card_A , b_Shopping_card_B)
lcpars[["b_Loan"]] = list(b_Loan_A , b_Loan_B)
lcpars[["b_Wifi"]] = list(b_Wifi_A , b_Wifi_B)
lcpars[["asc_cs"]] = list(asc_cs_A , asc_cs_B)
lcpars[["asc_ncs"]] = list(asc_ncs_A , asc_ncs_B)
### Utilities of class allocation model
V=list()
V[["class_a"]] = delta_a + b_Age_40_60_A*Age_40_60 + b_Mode_use_Car_A*Mode_use_Car + b_female_A*female + b_Age_21_30_A*Age_21_30 + Lambda_C_A*LV_C + Lambda_O_A*LV_O +
b_Income_0_50k_A*Income_0_50k + b_Education_PG_A*Education_PG + b_Income_above_100k_A*Income_above_100k
V[["class_b"]] = delta_b + b_Age_40_60_B*Age_40_60 + b_Mode_use_Car_B*Mode_use_Car + b_female_B*female + b_Age_21_30_B*Age_21_30 + Lambda_C_B*LV_C + Lambda_O_B*LV_O +
b_Income_0_50k_B*Income_0_50k + b_Education_PG_B*Education_PG + b_Income_above_100k_B*Income_above_100k
### Settings for class allocation models
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"){
### Initialise
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = C_a,
V = zeta_C_a*LV_C,
tau = list(tau_C_a_1, tau_C_a_2, tau_C_a_3, tau_C_a_4),
rows = (task==1),
componentName = "indic_C_a")
ol_settings2 = list(outcomeOrdered = C_b,
V = zeta_C_b*LV_C,
tau = list(tau_C_b_1, tau_C_b_2, tau_C_b_3, tau_C_b_4),
rows = (task==1),
componentName = "indic_C_b")
ol_settings3 = list(outcomeOrdered = C_c,
V = zeta_C_c*LV_C,
tau = list(tau_C_c_1, tau_C_c_2, tau_C_c_3, tau_C_c_4),
rows = (task==1),
componentName = "indic_C_c")
ol_settings4 = list(outcomeOrdered = C_d,
V = zeta_C_d*LV_C,
tau = list(tau_C_d_1, tau_C_d_2, tau_C_d_3, tau_C_d_4),
rows = (task==1),
componentName = "indic_C_d")
ol_settings5 = list(outcomeOrdered = O_a,
V = zeta_O_a*LV_O,
tau = list(tau_O_a_1, tau_O_a_2, tau_O_a_3, tau_O_a_4),
rows = (task==1),
componentName = "indic_O_a")
ol_settings6 = list(outcomeOrdered = O_b,
V = zeta_O_b*LV_O,
tau = list(tau_O_b_1, tau_O_b_2, tau_O_b_3, tau_O_b_4),
rows = (task==1),
componentName = "indic_O_b")
ol_settings7 = list(outcomeOrdered = O_c,
V = zeta_O_c*LV_O,
tau = list(tau_O_c_1, tau_O_c_2, tau_O_c_3, tau_O_c_4),
rows = (task==1),
componentName = "indic_O_c")
P[["indic_C_a"]] = apollo_ol(ol_settings1, functionality)
P[["indic_C_b"]] = apollo_ol(ol_settings2, functionality)
P[["indic_C_c"]] = apollo_ol(ol_settings3, functionality)
P[["indic_C_d"]] = apollo_ol(ol_settings4, functionality)
P[["indic_O_a"]] = apollo_ol(ol_settings5, functionality)
P[["indic_O_b"]] = apollo_ol(ol_settings6, functionality)
P[["indic_O_c"]] = apollo_ol(ol_settings7, functionality)
### Likelihood of choices inside each class
S <- 2
for(s in 1:S){
### Utilities for alternatives
V = list()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["cs"]] = asc_cs[[s]] + b_tc_cs[[s]]*tc_cs + b_tt_cs[[s]]*tt_cs + b_service_frequency_cs[[s]]*service_frequency_cs +
b_Loan[[s]]*(benefits_cs==2) + b_Wifi[[s]]*(benefits_cs==3) + b_Shopping_card[[s]]*(benefits_cs==1)
V[["ncs"]] = asc_ncs[[s]] + b_tc_ncs[[s]]*tc_ncs + b_tt_ncs[[s]]*tt_ncs
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(cs=1, ncs=2),
choiceVar = choice,
utilities = V
)
### mnl_settings$componentName = paste0("Class_",s)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
### 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[paste0("Class_", 1:S)], classProb=pi_values)
P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
### Comment out as necessary
P = apollo_combineModels(P, apollo_inputs, functionality)
P = apollo_avgInterDraws(P, apollo_inputs, functionality)
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### CALCULATE LL AT STARTING VALUES ####
# ################################################################# #
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)