`ylocsch` is a numeric variable that describes the choice. It is coded in a convenient way. It has 7 digits, where the first 6 digits code the location and the last digit is whether school is chosen or not. For example, 0000021 is "location number 2, child goes to school", 0000030 is "location number 3, child does not go to school". There are 450 locations and 2 choices of school/no school, hence 450*2 = 900 alternatives.
V_ind = bschool - bprice*Price_loc if school == 1
V_ind = 0 otherwise
Because of the large number of alternatives, I use section 11.5 of Apollo's (excellent) manual. Every individual can choose any location. Non-stochastic part of utility V is very simple: there is a constant for "going to school" and there is a price that has to be paid for going to school. Locations differ by the price. The utility from "school==1" is the same for every location, and the utility from "school==0" is normalized to 0.
Below is the code that I am using. It works if I am using a single node, nCores = 1 (albeit very, very slowly). It fails if I try to use more than one node.
Related issue:
Error when estimating a ICLV model with two sources of information
Code: Select all
### Load Apollo library
library(apollo)
library(data.table)
### Initialise code
apollo_initialise()
parallel::detectCores() #check how many cores the system has
### Set core controls
apollo_control = list(
modelName = "Nested_logit_loc_sch",
modelDescr = "Two-level NL model",
indivID = "ind",
nCores = 3 #[color=#FF0000]if set to 1, the code works[/color]
)
# # ################################################################# #
# #### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# # ################################################################# #
database = fread(paste0(data_dir, "choice_nested_logit.csv"))
list_alt <- unique(database[, ylocsch]) # this is where I define the list of alternatives, I am sure each is chosen at least once.
alternatives_set <- list_alt
names(alternatives_set) <- as.character(list_alt)
list_loc <- unique(database[, yloc]) # this defines the list of locations, useful for defining the nesting structure.
## sort data by id
setorder(database, ind)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(c(bprice = -0.5,
bsch = 1),
setNames(rep(0.5, length(list_loc)), paste0("lambda_", list_loc)))
### 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()
### Read in starting values for at least some parameters from existing model output file
# apollo_beta = apollo_readBeta(apollo_beta, apollo_fixed, "Apollo_example_1", overwriteFixed=FALSE)
# ################################################################# #
#### 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()
V = list()
# A = list() adjust availability based on year
### Create alternative specific constants and coefficients
for (ylocsch_iter in as.numeric(list_alt)){
sch_iter <- as.integer(ylocsch_iter %% 2)
alt_name <- toString(ylocsch_iter)
if (sch_iter == 0) {
V[[as.character(ylocsch_iter)]] = 0
} else if (sch_iter == 1) {
V[[as.character(ylocsch_iter)]] = bsch + bprice*get(paste0("PRICE_", ylocsch_iter) )
}
}
### Specify lambdas for all nests for NL model
nlNests = list(root=1)
for (loc in list_loc) {
nlNests[[as.character(loc)]] <- get(paste0("lambda_", loc))
}
### Specify tree structure for NL model
nlStructure= list()
nlStructure[["root"]] = as.character(list_loc)
for (loc in list_loc) {
nlStructure[[as.character(loc)]] = c(paste0(loc, "0"), paste0(loc, "1"))
}
### Define settings for MNL model component
nl_settings = list(
alternatives = alternatives_set,
avail = 1,
choiceVar = ylocsch_choice,
V = V,
nlNests = nlNests,
nlStructure = nlStructure
)
### Compute probabilities using NL model
P[["model"]] = apollo_nl(nl_settings, functionality)
### Take product across observations for same individual
# P = apollo_panelProd(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### 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)
Vasily