checkForRemoteErrors(lapply(cl, recvResult)): 5 nodes produced errors; first error: non-conformable arrays
Posted: 18 Oct 2024, 03:18
Hi,
I have developed a hybrid choice model with latent class. However, I encountered one problem.
Everything is ok if I do not include the covariates in the class model part, but when I include one of the covariates, an error is happened and shows that "Testing influence of parameters error in checkForRemoteErrors(lapply(cl, recvResult)): 5 nodes produced errors; first error: non-conformable arrays".
In other words, if I include the "+B_Belong_class_a*Belong" in V[["class_a"]] = delta_a #+B_Belong_class_a*Belong, the error is occurred. Could you kindly help me with it? Thank you!
Here is my code:
"#Hybrid Choice Model with Latent Class
### Initialise
rm(list = ls())
library(apollo)
library(readxl)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Hybrid__with_OrderMeasurement",
modelDescr = "Hybrid choice model on Route choice data, using ordered measurement model for indicators",
indivID = "ID",
nCores = 5,
outputDirectory = ""
)
database = read_excel("ModelUse.xlsx",sheet="010ModelUse")
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(# Choice parameters
#SocialDemographicVariable
#B_Gender =0,
#B_Age =0,
#B_Education_1 =0,
#B_Education_2 =0,
#B_Education_3 =0,
#B_Education_4 =0,
#B_Education_5 =0,
B_Income_1 =0,
B_Income_2 =0,
B_Income_3 =0,
#B_Income_1_class =0,
#B_Income_2_class =0,
#B_Income_3_class =0,
B_DriveYear_1 =0,
B_DriveYear_2 =0,
B_DriveYear_3 =0,
#B_DriveYear_1_class =0,
#B_DriveYear_2_class =0,
#B_DriveYear_3_class =0,
B_VehicleType_1 =0,
B_VehicleType_2 =0,
B_VehicleType_3 =0,
B_VehicleType_4 =0,
B_VehicleType_5 =0,
#B_VehicleType_1_class =0,
#B_VehicleType_2_class =0,
#B_VehicleType_3_class =0,
#B_VehicleType_4_class =0,
#B_VehicleType_5_class =0,
B_Belong =0,
#B_Belong_class_a =0,
#B_Belong_class_b =0,
#ContextVariable
B_DateType_a =0,
B_TimeofDay_a =0,
B_CargoType_a =0,
B_SizeOfTruck_1_a =0,
B_SizeOfTruck_2_a =0,
B_SizeOfTruck_3_a =0,
B_DistanceToDestination_a =0,
B_TimeWindow_a =0,
B_TimeSinceRest_a =0,
B_WeatherCondition_a =0,
B_DateType_b =0,
B_TimeofDay_b =0,
B_CargoType_b =0,
B_SizeOfTruck_1_b =0,
B_SizeOfTruck_2_b =0,
B_SizeOfTruck_3_b =0,
B_DistanceToDestination_b =0,
B_TimeWindow_b =0,
B_TimeSinceRest_b =0,
B_WeatherCondition_b =0,
#AlternativeSpecificAttribute
ASC_Highway =0,
ASC_Road =0,
B_TravelCostFuelRoad_a =0,
B_TravelTimeUncertainty_Road_a =0,
B_TravelCostCharge_Highway_a =0,
B_TravelTime_Highway_a =0,
B_TravelTimeUncertainty_Highway_a =0,
B_HighwayBonus_a =0,
B_FuelPrice_a =0,
B_EatingBonus_a =0,
B_RestFacility_a =0,
B_TravelCostFuelRoad_b =0,
B_TravelTimeUncertainty_Road_b =0,
B_TravelCostCharge_Highway_b =0,
B_TravelTime_Highway_b =0,
B_TravelTimeUncertainty_Highway_b =0,
B_HighwayBonus_b =0,
B_FuelPrice_b =0,
B_EatingBonus_b =0,
B_RestFacility_b =0,
#latent variable parameters
Lambda_TiredDrive_a =1,
Lambda_Safe_a =1,
Lambda_Law_a =1,
Lambda_Sleep_a =1,
Lambda_Food_a =1,
Lambda_TiredDrive_b =1,
Lambda_Safe_b =1,
Lambda_Law_b =1,
Lambda_Sleep_b =1,
Lambda_Food_b =1,
#Measurement quations parameters
#For each indicators, the first one should be fixed
#constant_TiredDrive_1 =0,
zeta_TiredDrive_1 =1,
#constant_TiredDrive_3 =0,
zeta_TiredDrive_3 =1,
# Class allocation parameters
delta_a = 0, delta_b = 0,
#constant_Safe_1 =0,
zeta_Safe_1 =1,
#constant_Safe_2 =0,
zeta_Safe_2 =1,
#constant_Safe_3 =0,
zeta_Safe_3 =1,
#constant_Safe_4 =0,
zeta_Safe_4 =1,
#constant_Law_1 =0,
zeta_Law_1 =1,
#constant_Law_2 =0,
zeta_Law_2 =1,
#constant_Law_3 =0,
zeta_Law_3 =1,
#constant_Law_4 =0,
zeta_Law_4 =1,
#constant_Law_5 =0,
zeta_Law_5 =1,
#
#constant_Sleep_1 =0,
zeta_Sleep_1 =1,
#constant_Sleep_2 =0,
zeta_Sleep_2 =1,
#constant_Sleep_3 =0,
zeta_Sleep_3 =1,
#constant_Sleep_4 =0,
zeta_Sleep_4 =1,
#constant_Food_1 =0,
#zeta_Food_1 =1,
#constant_Food_2 =0,
zeta_Food_2 =1,
#constant_Food_3 =0,
zeta_Food_3 =1,
#constant_Food_4 =0,
zeta_Food_4 =1,
#constant_Food_5 =0,
zeta_Food_5 =1,
tau_TiredDrive_1_1 =-2,
tau_TiredDrive_1_2 =-1,
tau_TiredDrive_1_3 =1,
tau_TiredDrive_1_4 =2,
tau_TiredDrive_3_1 =-2,
#tau_TiredDrive_3_2 =-1,
tau_TiredDrive_3_3 =1,
tau_TiredDrive_3_4 =2,
tau_Safe_1_1 =-2,
tau_Safe_1_2 =-1,
tau_Safe_1_3 =1,
tau_Safe_1_4 =2,
tau_Safe_2_1 =-2,
tau_Safe_2_2 =-1,
tau_Safe_2_3 =1,
tau_Safe_2_4 =2,
tau_Safe_3_1 =-2,
tau_Safe_3_2 =-1,
tau_Safe_3_3 =1,
tau_Safe_3_4 =2,
tau_Safe_4_1 =-2,
tau_Safe_4_2 =-1,
tau_Safe_4_3 =1,
tau_Safe_4_4 =2,
tau_Law_1_1 =-2,
tau_Law_1_2 =-1,
tau_Law_1_3 =1,
tau_Law_1_4 =2,
# tau_Law_2_1 =-2,
tau_Law_2_2 =-1,
tau_Law_2_3 =1,
tau_Law_2_4 =2,
tau_Law_3_1 =-2,
tau_Law_3_2 =-1,
tau_Law_3_3 =1,
tau_Law_3_4 =2,
tau_Law_4_1 =-2,
tau_Law_4_2 =-1,
tau_Law_4_3 =1,
tau_Law_4_4 =2,
tau_Law_5_1 =-2,
tau_Law_5_2 =-1,
tau_Law_5_3 =1,
tau_Law_5_4 =2,
tau_Sleep_1_1 =-2,
tau_Sleep_1_2 =-1,
tau_Sleep_1_3 =1,
tau_Sleep_1_4 =2,
tau_Sleep_2_1 =-2,
tau_Sleep_2_2 =-1,
tau_Sleep_2_3 =1,
tau_Sleep_2_4 =2,
tau_Sleep_3_1 =-2,
tau_Sleep_3_2 =-1,
tau_Sleep_3_3 =1,
tau_Sleep_3_4 =2,
tau_Sleep_4_1 =-2,
tau_Sleep_4_2 =-1,
tau_Sleep_4_3 =1,
tau_Sleep_4_4 =2,
tau_Food_2_1 =-2,
tau_Food_2_2 =-1,
tau_Food_2_3 =1,
tau_Food_2_4 =2,
tau_Food_3_1 =-2,
tau_Food_3_2 =-1,
tau_Food_3_3 =1,
tau_Food_3_4 =2,
# tau_Food_4_1 =-2,
tau_Food_4_2 =-1,
tau_Food_4_3 =1,
tau_Food_4_4 =2,
tau_Food_5_1 =-2,
tau_Food_5_2 =-1,
tau_Food_5_3 =1,
tau_Food_5_4 =2
)
### 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_Road",
"delta_b",
#"B_Belong_class_b",
#"constant_TiredDrive_1",
"zeta_TiredDrive_1",
#"constant_Safe_1",
"zeta_Safe_1",
#"constant_Law_1",
"zeta_Law_1",
#"constant_Sleep_1",
"zeta_Sleep_1",
#"constant_Food_2",
"zeta_Food_2"
)
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interNormDraws = c("eta_TiredDrive","eta_Safe","eta_Law","eta_Sleep","eta_Food")
#interNormDraws = c("eta_TiredDrive")
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["LV_TiredDrive"]] = eta_TiredDrive+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Safe"]] = eta_Safe+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Law"]] = eta_Law+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Sleep"]] = eta_Sleep+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Food"]] = eta_Food+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
#先定义class
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["B_DateType" ]] = list(B_DateType_a , B_DateType_b )
lcpars[["B_TimeofDay" ]] = list(B_TimeofDay_a , B_TimeofDay_b )
lcpars[["B_CargoType" ]] = list(B_CargoType_a , B_CargoType_b )
lcpars[["B_SizeOfTruck_1" ]] = list(B_SizeOfTruck_1_a , B_SizeOfTruck_1_b )
lcpars[["B_SizeOfTruck_2" ]] = list(B_SizeOfTruck_2_a , B_SizeOfTruck_2_b )
lcpars[["B_SizeOfTruck_3" ]] = list(B_SizeOfTruck_3_a , B_SizeOfTruck_3_b )
lcpars[["B_DistanceToDestination" ]] = list(B_DistanceToDestination_a , B_DistanceToDestination_b )
lcpars[["B_TimeWindow" ]] = list(B_TimeWindow_a , B_TimeWindow_b )
lcpars[["B_TimeSinceRest" ]] = list(B_TimeSinceRest_a , B_TimeSinceRest_b )
lcpars[["B_WeatherCondition" ]] = list(B_WeatherCondition_a , B_WeatherCondition_b )
lcpars[["B_TravelCostFuelRoad" ]] = list(B_TravelCostFuelRoad_a , B_TravelCostFuelRoad_b )
lcpars[["B_TravelTimeUncertainty_Road" ]] = list(B_TravelTimeUncertainty_Road_a , B_TravelTimeUncertainty_Road_b )
lcpars[["B_TravelCostCharge_Highway" ]] = list(B_TravelCostCharge_Highway_a , B_TravelCostCharge_Highway_b )
lcpars[["B_TravelTime_Highway" ]] = list(B_TravelTime_Highway_a , B_TravelTime_Highway_b )
lcpars[["B_TravelTimeUncertainty_Highway" ]] = list(B_TravelTimeUncertainty_Highway_a , B_TravelTimeUncertainty_Highway_b )
lcpars[["B_HighwayBonus" ]] = list(B_HighwayBonus_a , B_HighwayBonus_b )
lcpars[["B_FuelPrice" ]] = list(B_FuelPrice_a , B_FuelPrice_b )
lcpars[["B_EatingBonus" ]] = list(B_EatingBonus_a , B_EatingBonus_b )
lcpars[["B_RestFacility" ]] = list(B_RestFacility_a , B_RestFacility_b )
lcpars[["Lambda_TiredDrive" ]] = list(Lambda_TiredDrive_a , Lambda_TiredDrive_b )
lcpars[["Lambda_Safe" ]] = list(Lambda_Safe_a , Lambda_Safe_b )
lcpars[["Lambda_Law" ]] = list(Lambda_Law_a , Lambda_Law_b )
lcpars[["Lambda_Sleep" ]] = list(Lambda_Sleep_a , Lambda_Sleep_b )
lcpars[["Lambda_Food" ]] = list(Lambda_Food_a , Lambda_Food_b )
### Utilities of class allocation model
V=list()
V[["class_a"]] = delta_a #+B_Belong_class_a*Belong_class
V[["class_b"]] = delta_b #+B_Belong_class_b*Belong
### 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()
#再整理出来latent variable
### Likelihood of indicators
#把V中的constant都删掉先
ol_settings_TiredDrive_1 = list(outcomeOrdered = TiredDrive_1,
V = zeta_TiredDrive_1*LV_TiredDrive,
tau = list(tau_TiredDrive_1_1, tau_TiredDrive_1_2, tau_TiredDrive_1_3, tau_TiredDrive_1_4),
coding =c(1,2,3,4,5),
#只用人的第一个选择
rows = (No==1),
componentName = "indic_TiredDrive_1")
#TiredDrive_3不是所有的level都有,没有2
ol_settings_TiredDrive_3 = list(outcomeOrdered = TiredDrive_3,
V = zeta_TiredDrive_3*LV_TiredDrive,
tau = list(tau_TiredDrive_3_1, tau_TiredDrive_3_3, tau_TiredDrive_3_4),
coding =c(1,3,4,5), # there are only four levels:1,3,4, and 5
rows = (No==1),
componentName = "indic_TiredDrive_3")
ol_settings_Safe_1 = list(outcomeOrdered = Safe_1,
V = zeta_Safe_1*LV_Safe,
tau = list(tau_Safe_1_1, tau_Safe_1_2, tau_Safe_1_3, tau_Safe_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_1")
ol_settings_Safe_2 = list(outcomeOrdered = Safe_2,
V = zeta_Safe_2*LV_Safe,
tau = list(tau_Safe_2_1, tau_Safe_2_2, tau_Safe_2_3, tau_Safe_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_2")
ol_settings_Safe_3 = list(outcomeOrdered = Safe_3,
V = zeta_Safe_3*LV_Safe,
tau = list(tau_Safe_3_1, tau_Safe_3_2, tau_Safe_3_3, tau_Safe_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_3")
ol_settings_Safe_4 = list(outcomeOrdered = Safe_4,
V = zeta_Safe_4*LV_Safe,
tau = list(tau_Safe_4_1, tau_Safe_4_2, tau_Safe_4_3, tau_Safe_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_4")
#
ol_settings_Law_1 = list(outcomeOrdered = Law_1,
V = zeta_Law_1*LV_Law,
tau = list(tau_Law_1_1, tau_Law_1_2, tau_Law_1_3, tau_Law_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_1")
#Law_2不是所有的level都有,没有1
ol_settings_Law_2 = list(outcomeOrdered = Law_2,
V = zeta_Law_2*LV_Law,
tau = list(tau_Law_2_2, tau_Law_2_3, tau_Law_2_4),
coding =c(2,3,4,5),
rows = (No==1),
componentName = "indic_Law_2")
ol_settings_Law_3 = list(outcomeOrdered = Law_3,
V = zeta_Law_3*LV_Law,
tau = list(tau_Law_3_1, tau_Law_3_2, tau_Law_3_3, tau_Law_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_3")
ol_settings_Law_4 = list(outcomeOrdered = Law_4,
V = zeta_Law_4*LV_Law,
tau = list(tau_Law_4_1, tau_Law_4_2, tau_Law_4_3, tau_Law_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_4")
ol_settings_Law_5 = list(outcomeOrdered = Law_5,
V = zeta_Law_5*LV_Law,
tau = list(tau_Law_5_1, tau_Law_5_2, tau_Law_5_3, tau_Law_5_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_5")
ol_settings_Sleep_1 = list(outcomeOrdered = Sleep_1,
V = zeta_Sleep_1*LV_Sleep,
tau = list(tau_Sleep_1_1, tau_Sleep_1_2, tau_Sleep_1_3, tau_Sleep_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_1")
ol_settings_Sleep_2 = list(outcomeOrdered = Sleep_2,
V = zeta_Sleep_2*LV_Sleep,
tau = list(tau_Sleep_2_1, tau_Sleep_2_2, tau_Sleep_2_3, tau_Sleep_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_2")
ol_settings_Sleep_3 = list(outcomeOrdered = Sleep_3,
V = zeta_Sleep_3*LV_Sleep,
tau = list(tau_Sleep_3_1, tau_Sleep_3_2, tau_Sleep_3_3, tau_Sleep_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_3")
ol_settings_Sleep_4 = list(outcomeOrdered = Sleep_4,
V = zeta_Sleep_4*LV_Sleep,
tau = list(tau_Sleep_4_1, tau_Sleep_4_2, tau_Sleep_4_3, tau_Sleep_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_4")
ol_settings_Food_2 = list(outcomeOrdered = Food_2,
V = zeta_Food_2*LV_Food,
tau = list(tau_Food_2_1, tau_Food_2_2, tau_Food_2_3, tau_Food_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_2")
ol_settings_Food_3 = list(outcomeOrdered = Food_3,
V = zeta_Food_3*LV_Food,
tau = list(tau_Food_3_1, tau_Food_3_2, tau_Food_3_3, tau_Food_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_3")
#Food_4不是所有的level都有,没有1
ol_settings_Food_4 = list(outcomeOrdered = Food_4,
V = zeta_Food_4*LV_Food,
tau = list(tau_Food_4_2, tau_Food_4_3, tau_Food_4_4),
coding =c(2,3,4,5),
rows = (No==1),
componentName = "indic_Food_4")
ol_settings_Food_5 = list(outcomeOrdered = Food_5,
V = zeta_Food_5*LV_Food,
tau = list(tau_Food_5_1, tau_Food_5_2, tau_Food_5_3, tau_Food_5_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_5")
P[["indic_TiredDrive_1"]] = apollo_ol(ol_settings_TiredDrive_1, functionality)
P[["indic_TiredDrive_3"]] = apollo_ol(ol_settings_TiredDrive_3, functionality)
P[["indic_Safe_1"]]= apollo_ol(ol_settings_Safe_1, functionality)
P[["indic_Safe_2"]]= apollo_ol(ol_settings_Safe_2, functionality)
P[["indic_Safe_3"]]= apollo_ol(ol_settings_Safe_3, functionality)
P[["indic_Safe_4"]]= apollo_ol(ol_settings_Safe_4, functionality)
P[["indic_Law_1"]]= apollo_ol(ol_settings_Law_1, functionality)
P[["indic_Law_2"]]= apollo_ol(ol_settings_Law_2, functionality)
P[["indic_Law_3"]]= apollo_ol(ol_settings_Law_3, functionality)
P[["indic_Law_4"]]= apollo_ol(ol_settings_Law_4, functionality)
P[["indic_Law_5"]]= apollo_ol(ol_settings_Law_5, functionality)
P[["indic_Sleep_1"]]= apollo_ol(ol_settings_Sleep_1, functionality)
P[["indic_Sleep_2"]]= apollo_ol(ol_settings_Sleep_2, functionality)
P[["indic_Sleep_3"]]= apollo_ol(ol_settings_Sleep_3, functionality)
P[["indic_Sleep_4"]]= apollo_ol(ol_settings_Sleep_4, functionality)
P[["indic_Food_2"]]= apollo_ol(ol_settings_Food_2, functionality)
P[["indic_Food_3"]]= apollo_ol(ol_settings_Food_3, functionality)
P[["indic_Food_4"]]= apollo_ol(ol_settings_Food_4, functionality)
P[["indic_Food_5"]]= apollo_ol(ol_settings_Food_5, functionality)
P[["indic_TiredDrive_1"]] = apollo_panelProd(P[["indic_TiredDrive_1"]] , apollo_inputs, functionality)
P[["indic_TiredDrive_3"]] = apollo_panelProd(P[["indic_TiredDrive_3"]] , apollo_inputs, functionality)
P[["indic_Safe_1"]] = apollo_panelProd(P[["indic_Safe_1"]] , apollo_inputs, functionality)
P[["indic_Safe_2"]] = apollo_panelProd(P[["indic_Safe_2"]] , apollo_inputs, functionality)
P[["indic_Safe_3"]] = apollo_panelProd(P[["indic_Safe_3"]] , apollo_inputs, functionality)
P[["indic_Safe_4"]] = apollo_panelProd(P[["indic_Safe_4"]] , apollo_inputs, functionality)
P[["indic_Law_1"]] = apollo_panelProd(P[["indic_Law_1"]] , apollo_inputs, functionality)
P[["indic_Law_2"]] = apollo_panelProd(P[["indic_Law_2"]] , apollo_inputs, functionality)
P[["indic_Law_3"]] = apollo_panelProd(P[["indic_Law_3"]] , apollo_inputs, functionality)
P[["indic_Law_4"]] = apollo_panelProd(P[["indic_Law_4"]] , apollo_inputs, functionality)
P[["indic_Law_5"]] = apollo_panelProd(P[["indic_Law_5"]] , apollo_inputs, functionality)
P[["indic_Sleep_1"]] = apollo_panelProd(P[["indic_Sleep_1"]] , apollo_inputs, functionality)
P[["indic_Sleep_2"]] = apollo_panelProd(P[["indic_Sleep_2"]] , apollo_inputs, functionality)
P[["indic_Sleep_3"]] = apollo_panelProd(P[["indic_Sleep_3"]] , apollo_inputs, functionality)
P[["indic_Sleep_4"]] = apollo_panelProd(P[["indic_Sleep_4"]] , apollo_inputs, functionality)
P[["indic_Food_2"]] = apollo_panelProd(P[["indic_Food_2"]] , apollo_inputs, functionality)
P[["indic_Food_3"]] = apollo_panelProd(P[["indic_Food_3"]] , apollo_inputs, functionality)
P[["indic_Food_4"]] = apollo_panelProd(P[["indic_Food_4"]] , apollo_inputs, functionality)
P[["indic_Food_5"]] = apollo_panelProd(P[["indic_Food_5"]] , apollo_inputs, functionality)
#最后将latent variable装入class里面
### Likelihood of choices inside each class
S <- 2
for(s in 1:S){
### Utilities for alternatives
V = list()
#alt1和alt2包括latent variable
V[["alt1"]] = ASC_Road + B_TravelCostFuelRoad[[s]]*TravelCostFuelRoad+B_TravelTimeUncertainty_Road[[s]]*(TravelTimeUncertainty_Road_1*Probability_Road_1 +TravelTimeUncertainty_Road_2*Probability_Road_2 + TravelTimeUncertainty_Road_3*Probability_Road_3 + TravelTimeUncertainty_Road_4*Probability_Road_4)
V[["alt2"]] = Lambda_TiredDrive[[s]]*LV_TiredDrive+Lambda_Safe[[s]]*LV_Safe+Lambda_Law[[s]]*LV_Law+Lambda_Sleep[[s]]*LV_Sleep+Lambda_Food[[s]]*LV_Food+ASC_Highway+ B_DateType[[s]]*DateType+B_TimeofDay[[s]]*TimeOfDay+B_CargoType[[s]]*CargoType+B_SizeOfTruck_1[[s]]*SizeOfTruck_1+B_SizeOfTruck_2[[s]]*SizeOfTruck_2+B_SizeOfTruck_3[[s]]*SizeOfTruck_3+B_DistanceToDestination[[s]]*DistanceToDestination+B_TimeWindow[[s]]*TimeWindow+B_TimeSinceRest[[s]]*TimeSinceRest+B_WeatherCondition[[s]]*WeatherCondition+B_TravelCostCharge_Highway[[s]]*TravelCostCharge_Highway+B_TravelTime_Highway[[s]]*TravelTime_Highway+B_TravelTimeUncertainty_Highway[[s]]*(TravelTimeUncertainty_Highway_1*Probability_Highway_1+ TravelTimeUncertainty_Highway_2*Probability_Highway_2+TravelTimeUncertainty_Highway_3*Probability_Highway_3+TravelTimeUncertainty_Highway_4*Probability_Highway_4)+B_HighwayBonus[[s]]*HighwayBonus+B_FuelPrice[[s]]*FuelPrice+B_EatingBonus[[s]]*EatingBonus+B_RestFacility[[s]]*RestFacility
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = Choice,
utilities = V,
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,
estimate_settings = list(estimationRoutine="BFGS"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
"
I have developed a hybrid choice model with latent class. However, I encountered one problem.
Everything is ok if I do not include the covariates in the class model part, but when I include one of the covariates, an error is happened and shows that "Testing influence of parameters error in checkForRemoteErrors(lapply(cl, recvResult)): 5 nodes produced errors; first error: non-conformable arrays".
In other words, if I include the "+B_Belong_class_a*Belong" in V[["class_a"]] = delta_a #+B_Belong_class_a*Belong, the error is occurred. Could you kindly help me with it? Thank you!
Here is my code:
"#Hybrid Choice Model with Latent Class
### Initialise
rm(list = ls())
library(apollo)
library(readxl)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Hybrid__with_OrderMeasurement",
modelDescr = "Hybrid choice model on Route choice data, using ordered measurement model for indicators",
indivID = "ID",
nCores = 5,
outputDirectory = ""
)
database = read_excel("ModelUse.xlsx",sheet="010ModelUse")
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(# Choice parameters
#SocialDemographicVariable
#B_Gender =0,
#B_Age =0,
#B_Education_1 =0,
#B_Education_2 =0,
#B_Education_3 =0,
#B_Education_4 =0,
#B_Education_5 =0,
B_Income_1 =0,
B_Income_2 =0,
B_Income_3 =0,
#B_Income_1_class =0,
#B_Income_2_class =0,
#B_Income_3_class =0,
B_DriveYear_1 =0,
B_DriveYear_2 =0,
B_DriveYear_3 =0,
#B_DriveYear_1_class =0,
#B_DriveYear_2_class =0,
#B_DriveYear_3_class =0,
B_VehicleType_1 =0,
B_VehicleType_2 =0,
B_VehicleType_3 =0,
B_VehicleType_4 =0,
B_VehicleType_5 =0,
#B_VehicleType_1_class =0,
#B_VehicleType_2_class =0,
#B_VehicleType_3_class =0,
#B_VehicleType_4_class =0,
#B_VehicleType_5_class =0,
B_Belong =0,
#B_Belong_class_a =0,
#B_Belong_class_b =0,
#ContextVariable
B_DateType_a =0,
B_TimeofDay_a =0,
B_CargoType_a =0,
B_SizeOfTruck_1_a =0,
B_SizeOfTruck_2_a =0,
B_SizeOfTruck_3_a =0,
B_DistanceToDestination_a =0,
B_TimeWindow_a =0,
B_TimeSinceRest_a =0,
B_WeatherCondition_a =0,
B_DateType_b =0,
B_TimeofDay_b =0,
B_CargoType_b =0,
B_SizeOfTruck_1_b =0,
B_SizeOfTruck_2_b =0,
B_SizeOfTruck_3_b =0,
B_DistanceToDestination_b =0,
B_TimeWindow_b =0,
B_TimeSinceRest_b =0,
B_WeatherCondition_b =0,
#AlternativeSpecificAttribute
ASC_Highway =0,
ASC_Road =0,
B_TravelCostFuelRoad_a =0,
B_TravelTimeUncertainty_Road_a =0,
B_TravelCostCharge_Highway_a =0,
B_TravelTime_Highway_a =0,
B_TravelTimeUncertainty_Highway_a =0,
B_HighwayBonus_a =0,
B_FuelPrice_a =0,
B_EatingBonus_a =0,
B_RestFacility_a =0,
B_TravelCostFuelRoad_b =0,
B_TravelTimeUncertainty_Road_b =0,
B_TravelCostCharge_Highway_b =0,
B_TravelTime_Highway_b =0,
B_TravelTimeUncertainty_Highway_b =0,
B_HighwayBonus_b =0,
B_FuelPrice_b =0,
B_EatingBonus_b =0,
B_RestFacility_b =0,
#latent variable parameters
Lambda_TiredDrive_a =1,
Lambda_Safe_a =1,
Lambda_Law_a =1,
Lambda_Sleep_a =1,
Lambda_Food_a =1,
Lambda_TiredDrive_b =1,
Lambda_Safe_b =1,
Lambda_Law_b =1,
Lambda_Sleep_b =1,
Lambda_Food_b =1,
#Measurement quations parameters
#For each indicators, the first one should be fixed
#constant_TiredDrive_1 =0,
zeta_TiredDrive_1 =1,
#constant_TiredDrive_3 =0,
zeta_TiredDrive_3 =1,
# Class allocation parameters
delta_a = 0, delta_b = 0,
#constant_Safe_1 =0,
zeta_Safe_1 =1,
#constant_Safe_2 =0,
zeta_Safe_2 =1,
#constant_Safe_3 =0,
zeta_Safe_3 =1,
#constant_Safe_4 =0,
zeta_Safe_4 =1,
#constant_Law_1 =0,
zeta_Law_1 =1,
#constant_Law_2 =0,
zeta_Law_2 =1,
#constant_Law_3 =0,
zeta_Law_3 =1,
#constant_Law_4 =0,
zeta_Law_4 =1,
#constant_Law_5 =0,
zeta_Law_5 =1,
#
#constant_Sleep_1 =0,
zeta_Sleep_1 =1,
#constant_Sleep_2 =0,
zeta_Sleep_2 =1,
#constant_Sleep_3 =0,
zeta_Sleep_3 =1,
#constant_Sleep_4 =0,
zeta_Sleep_4 =1,
#constant_Food_1 =0,
#zeta_Food_1 =1,
#constant_Food_2 =0,
zeta_Food_2 =1,
#constant_Food_3 =0,
zeta_Food_3 =1,
#constant_Food_4 =0,
zeta_Food_4 =1,
#constant_Food_5 =0,
zeta_Food_5 =1,
tau_TiredDrive_1_1 =-2,
tau_TiredDrive_1_2 =-1,
tau_TiredDrive_1_3 =1,
tau_TiredDrive_1_4 =2,
tau_TiredDrive_3_1 =-2,
#tau_TiredDrive_3_2 =-1,
tau_TiredDrive_3_3 =1,
tau_TiredDrive_3_4 =2,
tau_Safe_1_1 =-2,
tau_Safe_1_2 =-1,
tau_Safe_1_3 =1,
tau_Safe_1_4 =2,
tau_Safe_2_1 =-2,
tau_Safe_2_2 =-1,
tau_Safe_2_3 =1,
tau_Safe_2_4 =2,
tau_Safe_3_1 =-2,
tau_Safe_3_2 =-1,
tau_Safe_3_3 =1,
tau_Safe_3_4 =2,
tau_Safe_4_1 =-2,
tau_Safe_4_2 =-1,
tau_Safe_4_3 =1,
tau_Safe_4_4 =2,
tau_Law_1_1 =-2,
tau_Law_1_2 =-1,
tau_Law_1_3 =1,
tau_Law_1_4 =2,
# tau_Law_2_1 =-2,
tau_Law_2_2 =-1,
tau_Law_2_3 =1,
tau_Law_2_4 =2,
tau_Law_3_1 =-2,
tau_Law_3_2 =-1,
tau_Law_3_3 =1,
tau_Law_3_4 =2,
tau_Law_4_1 =-2,
tau_Law_4_2 =-1,
tau_Law_4_3 =1,
tau_Law_4_4 =2,
tau_Law_5_1 =-2,
tau_Law_5_2 =-1,
tau_Law_5_3 =1,
tau_Law_5_4 =2,
tau_Sleep_1_1 =-2,
tau_Sleep_1_2 =-1,
tau_Sleep_1_3 =1,
tau_Sleep_1_4 =2,
tau_Sleep_2_1 =-2,
tau_Sleep_2_2 =-1,
tau_Sleep_2_3 =1,
tau_Sleep_2_4 =2,
tau_Sleep_3_1 =-2,
tau_Sleep_3_2 =-1,
tau_Sleep_3_3 =1,
tau_Sleep_3_4 =2,
tau_Sleep_4_1 =-2,
tau_Sleep_4_2 =-1,
tau_Sleep_4_3 =1,
tau_Sleep_4_4 =2,
tau_Food_2_1 =-2,
tau_Food_2_2 =-1,
tau_Food_2_3 =1,
tau_Food_2_4 =2,
tau_Food_3_1 =-2,
tau_Food_3_2 =-1,
tau_Food_3_3 =1,
tau_Food_3_4 =2,
# tau_Food_4_1 =-2,
tau_Food_4_2 =-1,
tau_Food_4_3 =1,
tau_Food_4_4 =2,
tau_Food_5_1 =-2,
tau_Food_5_2 =-1,
tau_Food_5_3 =1,
tau_Food_5_4 =2
)
### 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_Road",
"delta_b",
#"B_Belong_class_b",
#"constant_TiredDrive_1",
"zeta_TiredDrive_1",
#"constant_Safe_1",
"zeta_Safe_1",
#"constant_Law_1",
"zeta_Law_1",
#"constant_Sleep_1",
"zeta_Sleep_1",
#"constant_Food_2",
"zeta_Food_2"
)
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interNormDraws = c("eta_TiredDrive","eta_Safe","eta_Law","eta_Sleep","eta_Food")
#interNormDraws = c("eta_TiredDrive")
)
### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["LV_TiredDrive"]] = eta_TiredDrive+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Safe"]] = eta_Safe+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Law"]] = eta_Law+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Sleep"]] = eta_Sleep+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
randcoeff[["LV_Food"]] = eta_Food+B_Belong*Belong+B_DriveYear_1*DriveYear_1+B_DriveYear_2*DriveYear_2+B_DriveYear_3*DriveYear_3+B_VehicleType_1*VehicleType_1+B_VehicleType_2*VehicleType_2+B_VehicleType_3*VehicleType_3+B_VehicleType_4*VehicleType_4+B_VehicleType_5*VehicleType_5+B_Income_1*Income_1+B_Income_2*Income_2+B_Income_3*Income_3
return(randcoeff)
}
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
#先定义class
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["B_DateType" ]] = list(B_DateType_a , B_DateType_b )
lcpars[["B_TimeofDay" ]] = list(B_TimeofDay_a , B_TimeofDay_b )
lcpars[["B_CargoType" ]] = list(B_CargoType_a , B_CargoType_b )
lcpars[["B_SizeOfTruck_1" ]] = list(B_SizeOfTruck_1_a , B_SizeOfTruck_1_b )
lcpars[["B_SizeOfTruck_2" ]] = list(B_SizeOfTruck_2_a , B_SizeOfTruck_2_b )
lcpars[["B_SizeOfTruck_3" ]] = list(B_SizeOfTruck_3_a , B_SizeOfTruck_3_b )
lcpars[["B_DistanceToDestination" ]] = list(B_DistanceToDestination_a , B_DistanceToDestination_b )
lcpars[["B_TimeWindow" ]] = list(B_TimeWindow_a , B_TimeWindow_b )
lcpars[["B_TimeSinceRest" ]] = list(B_TimeSinceRest_a , B_TimeSinceRest_b )
lcpars[["B_WeatherCondition" ]] = list(B_WeatherCondition_a , B_WeatherCondition_b )
lcpars[["B_TravelCostFuelRoad" ]] = list(B_TravelCostFuelRoad_a , B_TravelCostFuelRoad_b )
lcpars[["B_TravelTimeUncertainty_Road" ]] = list(B_TravelTimeUncertainty_Road_a , B_TravelTimeUncertainty_Road_b )
lcpars[["B_TravelCostCharge_Highway" ]] = list(B_TravelCostCharge_Highway_a , B_TravelCostCharge_Highway_b )
lcpars[["B_TravelTime_Highway" ]] = list(B_TravelTime_Highway_a , B_TravelTime_Highway_b )
lcpars[["B_TravelTimeUncertainty_Highway" ]] = list(B_TravelTimeUncertainty_Highway_a , B_TravelTimeUncertainty_Highway_b )
lcpars[["B_HighwayBonus" ]] = list(B_HighwayBonus_a , B_HighwayBonus_b )
lcpars[["B_FuelPrice" ]] = list(B_FuelPrice_a , B_FuelPrice_b )
lcpars[["B_EatingBonus" ]] = list(B_EatingBonus_a , B_EatingBonus_b )
lcpars[["B_RestFacility" ]] = list(B_RestFacility_a , B_RestFacility_b )
lcpars[["Lambda_TiredDrive" ]] = list(Lambda_TiredDrive_a , Lambda_TiredDrive_b )
lcpars[["Lambda_Safe" ]] = list(Lambda_Safe_a , Lambda_Safe_b )
lcpars[["Lambda_Law" ]] = list(Lambda_Law_a , Lambda_Law_b )
lcpars[["Lambda_Sleep" ]] = list(Lambda_Sleep_a , Lambda_Sleep_b )
lcpars[["Lambda_Food" ]] = list(Lambda_Food_a , Lambda_Food_b )
### Utilities of class allocation model
V=list()
V[["class_a"]] = delta_a #+B_Belong_class_a*Belong_class
V[["class_b"]] = delta_b #+B_Belong_class_b*Belong
### 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()
#再整理出来latent variable
### Likelihood of indicators
#把V中的constant都删掉先
ol_settings_TiredDrive_1 = list(outcomeOrdered = TiredDrive_1,
V = zeta_TiredDrive_1*LV_TiredDrive,
tau = list(tau_TiredDrive_1_1, tau_TiredDrive_1_2, tau_TiredDrive_1_3, tau_TiredDrive_1_4),
coding =c(1,2,3,4,5),
#只用人的第一个选择
rows = (No==1),
componentName = "indic_TiredDrive_1")
#TiredDrive_3不是所有的level都有,没有2
ol_settings_TiredDrive_3 = list(outcomeOrdered = TiredDrive_3,
V = zeta_TiredDrive_3*LV_TiredDrive,
tau = list(tau_TiredDrive_3_1, tau_TiredDrive_3_3, tau_TiredDrive_3_4),
coding =c(1,3,4,5), # there are only four levels:1,3,4, and 5
rows = (No==1),
componentName = "indic_TiredDrive_3")
ol_settings_Safe_1 = list(outcomeOrdered = Safe_1,
V = zeta_Safe_1*LV_Safe,
tau = list(tau_Safe_1_1, tau_Safe_1_2, tau_Safe_1_3, tau_Safe_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_1")
ol_settings_Safe_2 = list(outcomeOrdered = Safe_2,
V = zeta_Safe_2*LV_Safe,
tau = list(tau_Safe_2_1, tau_Safe_2_2, tau_Safe_2_3, tau_Safe_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_2")
ol_settings_Safe_3 = list(outcomeOrdered = Safe_3,
V = zeta_Safe_3*LV_Safe,
tau = list(tau_Safe_3_1, tau_Safe_3_2, tau_Safe_3_3, tau_Safe_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_3")
ol_settings_Safe_4 = list(outcomeOrdered = Safe_4,
V = zeta_Safe_4*LV_Safe,
tau = list(tau_Safe_4_1, tau_Safe_4_2, tau_Safe_4_3, tau_Safe_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Safe_4")
#
ol_settings_Law_1 = list(outcomeOrdered = Law_1,
V = zeta_Law_1*LV_Law,
tau = list(tau_Law_1_1, tau_Law_1_2, tau_Law_1_3, tau_Law_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_1")
#Law_2不是所有的level都有,没有1
ol_settings_Law_2 = list(outcomeOrdered = Law_2,
V = zeta_Law_2*LV_Law,
tau = list(tau_Law_2_2, tau_Law_2_3, tau_Law_2_4),
coding =c(2,3,4,5),
rows = (No==1),
componentName = "indic_Law_2")
ol_settings_Law_3 = list(outcomeOrdered = Law_3,
V = zeta_Law_3*LV_Law,
tau = list(tau_Law_3_1, tau_Law_3_2, tau_Law_3_3, tau_Law_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_3")
ol_settings_Law_4 = list(outcomeOrdered = Law_4,
V = zeta_Law_4*LV_Law,
tau = list(tau_Law_4_1, tau_Law_4_2, tau_Law_4_3, tau_Law_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_4")
ol_settings_Law_5 = list(outcomeOrdered = Law_5,
V = zeta_Law_5*LV_Law,
tau = list(tau_Law_5_1, tau_Law_5_2, tau_Law_5_3, tau_Law_5_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Law_5")
ol_settings_Sleep_1 = list(outcomeOrdered = Sleep_1,
V = zeta_Sleep_1*LV_Sleep,
tau = list(tau_Sleep_1_1, tau_Sleep_1_2, tau_Sleep_1_3, tau_Sleep_1_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_1")
ol_settings_Sleep_2 = list(outcomeOrdered = Sleep_2,
V = zeta_Sleep_2*LV_Sleep,
tau = list(tau_Sleep_2_1, tau_Sleep_2_2, tau_Sleep_2_3, tau_Sleep_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_2")
ol_settings_Sleep_3 = list(outcomeOrdered = Sleep_3,
V = zeta_Sleep_3*LV_Sleep,
tau = list(tau_Sleep_3_1, tau_Sleep_3_2, tau_Sleep_3_3, tau_Sleep_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_3")
ol_settings_Sleep_4 = list(outcomeOrdered = Sleep_4,
V = zeta_Sleep_4*LV_Sleep,
tau = list(tau_Sleep_4_1, tau_Sleep_4_2, tau_Sleep_4_3, tau_Sleep_4_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Sleep_4")
ol_settings_Food_2 = list(outcomeOrdered = Food_2,
V = zeta_Food_2*LV_Food,
tau = list(tau_Food_2_1, tau_Food_2_2, tau_Food_2_3, tau_Food_2_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_2")
ol_settings_Food_3 = list(outcomeOrdered = Food_3,
V = zeta_Food_3*LV_Food,
tau = list(tau_Food_3_1, tau_Food_3_2, tau_Food_3_3, tau_Food_3_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_3")
#Food_4不是所有的level都有,没有1
ol_settings_Food_4 = list(outcomeOrdered = Food_4,
V = zeta_Food_4*LV_Food,
tau = list(tau_Food_4_2, tau_Food_4_3, tau_Food_4_4),
coding =c(2,3,4,5),
rows = (No==1),
componentName = "indic_Food_4")
ol_settings_Food_5 = list(outcomeOrdered = Food_5,
V = zeta_Food_5*LV_Food,
tau = list(tau_Food_5_1, tau_Food_5_2, tau_Food_5_3, tau_Food_5_4),
coding =c(1,2,3,4,5),
rows = (No==1),
componentName = "indic_Food_5")
P[["indic_TiredDrive_1"]] = apollo_ol(ol_settings_TiredDrive_1, functionality)
P[["indic_TiredDrive_3"]] = apollo_ol(ol_settings_TiredDrive_3, functionality)
P[["indic_Safe_1"]]= apollo_ol(ol_settings_Safe_1, functionality)
P[["indic_Safe_2"]]= apollo_ol(ol_settings_Safe_2, functionality)
P[["indic_Safe_3"]]= apollo_ol(ol_settings_Safe_3, functionality)
P[["indic_Safe_4"]]= apollo_ol(ol_settings_Safe_4, functionality)
P[["indic_Law_1"]]= apollo_ol(ol_settings_Law_1, functionality)
P[["indic_Law_2"]]= apollo_ol(ol_settings_Law_2, functionality)
P[["indic_Law_3"]]= apollo_ol(ol_settings_Law_3, functionality)
P[["indic_Law_4"]]= apollo_ol(ol_settings_Law_4, functionality)
P[["indic_Law_5"]]= apollo_ol(ol_settings_Law_5, functionality)
P[["indic_Sleep_1"]]= apollo_ol(ol_settings_Sleep_1, functionality)
P[["indic_Sleep_2"]]= apollo_ol(ol_settings_Sleep_2, functionality)
P[["indic_Sleep_3"]]= apollo_ol(ol_settings_Sleep_3, functionality)
P[["indic_Sleep_4"]]= apollo_ol(ol_settings_Sleep_4, functionality)
P[["indic_Food_2"]]= apollo_ol(ol_settings_Food_2, functionality)
P[["indic_Food_3"]]= apollo_ol(ol_settings_Food_3, functionality)
P[["indic_Food_4"]]= apollo_ol(ol_settings_Food_4, functionality)
P[["indic_Food_5"]]= apollo_ol(ol_settings_Food_5, functionality)
P[["indic_TiredDrive_1"]] = apollo_panelProd(P[["indic_TiredDrive_1"]] , apollo_inputs, functionality)
P[["indic_TiredDrive_3"]] = apollo_panelProd(P[["indic_TiredDrive_3"]] , apollo_inputs, functionality)
P[["indic_Safe_1"]] = apollo_panelProd(P[["indic_Safe_1"]] , apollo_inputs, functionality)
P[["indic_Safe_2"]] = apollo_panelProd(P[["indic_Safe_2"]] , apollo_inputs, functionality)
P[["indic_Safe_3"]] = apollo_panelProd(P[["indic_Safe_3"]] , apollo_inputs, functionality)
P[["indic_Safe_4"]] = apollo_panelProd(P[["indic_Safe_4"]] , apollo_inputs, functionality)
P[["indic_Law_1"]] = apollo_panelProd(P[["indic_Law_1"]] , apollo_inputs, functionality)
P[["indic_Law_2"]] = apollo_panelProd(P[["indic_Law_2"]] , apollo_inputs, functionality)
P[["indic_Law_3"]] = apollo_panelProd(P[["indic_Law_3"]] , apollo_inputs, functionality)
P[["indic_Law_4"]] = apollo_panelProd(P[["indic_Law_4"]] , apollo_inputs, functionality)
P[["indic_Law_5"]] = apollo_panelProd(P[["indic_Law_5"]] , apollo_inputs, functionality)
P[["indic_Sleep_1"]] = apollo_panelProd(P[["indic_Sleep_1"]] , apollo_inputs, functionality)
P[["indic_Sleep_2"]] = apollo_panelProd(P[["indic_Sleep_2"]] , apollo_inputs, functionality)
P[["indic_Sleep_3"]] = apollo_panelProd(P[["indic_Sleep_3"]] , apollo_inputs, functionality)
P[["indic_Sleep_4"]] = apollo_panelProd(P[["indic_Sleep_4"]] , apollo_inputs, functionality)
P[["indic_Food_2"]] = apollo_panelProd(P[["indic_Food_2"]] , apollo_inputs, functionality)
P[["indic_Food_3"]] = apollo_panelProd(P[["indic_Food_3"]] , apollo_inputs, functionality)
P[["indic_Food_4"]] = apollo_panelProd(P[["indic_Food_4"]] , apollo_inputs, functionality)
P[["indic_Food_5"]] = apollo_panelProd(P[["indic_Food_5"]] , apollo_inputs, functionality)
#最后将latent variable装入class里面
### Likelihood of choices inside each class
S <- 2
for(s in 1:S){
### Utilities for alternatives
V = list()
#alt1和alt2包括latent variable
V[["alt1"]] = ASC_Road + B_TravelCostFuelRoad[[s]]*TravelCostFuelRoad+B_TravelTimeUncertainty_Road[[s]]*(TravelTimeUncertainty_Road_1*Probability_Road_1 +TravelTimeUncertainty_Road_2*Probability_Road_2 + TravelTimeUncertainty_Road_3*Probability_Road_3 + TravelTimeUncertainty_Road_4*Probability_Road_4)
V[["alt2"]] = Lambda_TiredDrive[[s]]*LV_TiredDrive+Lambda_Safe[[s]]*LV_Safe+Lambda_Law[[s]]*LV_Law+Lambda_Sleep[[s]]*LV_Sleep+Lambda_Food[[s]]*LV_Food+ASC_Highway+ B_DateType[[s]]*DateType+B_TimeofDay[[s]]*TimeOfDay+B_CargoType[[s]]*CargoType+B_SizeOfTruck_1[[s]]*SizeOfTruck_1+B_SizeOfTruck_2[[s]]*SizeOfTruck_2+B_SizeOfTruck_3[[s]]*SizeOfTruck_3+B_DistanceToDestination[[s]]*DistanceToDestination+B_TimeWindow[[s]]*TimeWindow+B_TimeSinceRest[[s]]*TimeSinceRest+B_WeatherCondition[[s]]*WeatherCondition+B_TravelCostCharge_Highway[[s]]*TravelCostCharge_Highway+B_TravelTime_Highway[[s]]*TravelTime_Highway+B_TravelTimeUncertainty_Highway[[s]]*(TravelTimeUncertainty_Highway_1*Probability_Highway_1+ TravelTimeUncertainty_Highway_2*Probability_Highway_2+TravelTimeUncertainty_Highway_3*Probability_Highway_3+TravelTimeUncertainty_Highway_4*Probability_Highway_4)+B_HighwayBonus[[s]]*HighwayBonus+B_FuelPrice[[s]]*FuelPrice+B_EatingBonus[[s]]*EatingBonus+B_RestFacility[[s]]*RestFacility
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = Choice,
utilities = V,
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,
estimate_settings = list(estimationRoutine="BFGS"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
"