Prof. Hess, and Dr. Palma
I am trying to develop a latent class model with Joint RP-SP data. However, I am getting the following error: object 'mnl_settings_RP' not found
Could you please help me out in resolving this?
The code is as follows:
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_IPA",
modelDescr = "RP_SP_HCM",
indivID = "ID",
nCores = 16,
outputDirectory = "output"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("RP_SP_CP_3.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_local = 0,
asc_metro = 0,
asc_car = 0,
asc_tw = 0,
b_tt_a = 0,
b_tc_a = 0,
b_at_a = 0,
b_tt_b = 0,
b_tc_b = 0,
b_at_b = 0,
delta_a = 0,
delta_b = 0,
female_a = 0,
Income_0_50k_a = 0,
Age_18_30_a = 0,
female_b = 0,
Income_0_50k_b = 0,
Age_18_30_b = 0,
mu_RP = 1,
mu_SP = 1)
### 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("mu_RP", "asc_car", "female_a")
# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS ####
# ################################################################# #
apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_tt"]] = list(b_tt_a, b_tt_b)
lcpars[["b_tc"]] = list(b_tc_a, b_tc_b)
lcpars[["b_at"]] = list(b_at_a, b_at_b)
### Utilities of class allocation model
V=list()
V[["class_a"]] = delta_a + female_a*female + Income_0_50k_a*Income_0_50k + Age_18_30_a*Age_18_30
V[["class_b"]] = delta_b + female_b*female + Income_0_50k_b*Income_0_50k + Age_18_30_b*Age_18_30
### 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"){
### 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()
### Loop over classes
for(s in 1:2){
### List of utilities (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["local"]] = asc_local + b_tt[[s]]*tt_local + b_tc[[s]]*tc_local + b_at[[s]]*at_local
V[["metro"]] = asc_metro + b_tt[[s]]*tt_metro + b_tc[[s]]*tc_metro + b_at[[s]]*at_metro
V[["car"]] = asc_car + b_tt[[s]]*tt_car + b_tc[[s]]*tc_car
V[["tw"]] = asc_tw + b_tt[[s]]*tt_tw + b_tc[[s]]*tc_tw
mnl_settings_RP$componentName = paste0("Class_",s)
mnl_settings_SP$componentName = paste0("Class_",s)
### Compute probabilities for the RP part of the data using MNL model
mnl_settings_RP = list(
alternatives = c(local=1, metro=2, car=3, tw=4),
avail = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw),
choiceVar = Choice,
utilities = list(local = mu_RP*V[["local"]],
metro = mu_RP*V[["metro"]],
car = mu_RP*V[["car"]],
tw = mu_RP*V[["tw"]]),
rows = (RP==1)
)
mnl_settings_RP$componentName = paste0("Class_",s)
P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
### Compute probabilities for the RP part of the data using MNL model
mnl_settings_SP = list(
alternatives = c(local=1, metro=2, car=3, tw=4),
avail = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw),
choiceVar = Choice,
utilities = list(local = mu_SP*V[["local"]],
metro = mu_SP*V[["metro"]],
car = mu_SP*V[["car"]],
tw = mu_SP*V[["tw"]]),
rows = (SP==1)
)
mnl_settings_SP$componentName = paste0("Class_",s)
P[["SP"]] = apollo_mnl(mnl_settings_SP, functionality)
### Compute within-class choice probabilities using MNL model
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_RP, functionality)
P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_SP, functionality)
### Take product across observation for same individual
P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
}
### Combined model
P = apollo_combineModels(P, apollo_inputs, functionality)
### Compute latent class model probabilities
lc_settings = list(inClassProb = P, classProb=pi_values)
P[["RP"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
P[["SP"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs)
### Show output in screen
apollo_modelOutput(model)
### Save output to file(s)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)