I recently came across the following error message
Preparing workersError in makePSOCKcluster(names = spec, ...) :
Cluster setup failed. 4 of 4 workers failed to connect.
when estimating an MNL or MXL (error message is identical) using nCores>1.
For nCores = 1 everything works fine (but somewhat slow).
Below is a working example using the Car data from mlogit().
Any suggestions are very welcome.
Best
Sven
Example
Code: Select all
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="car_purchase_mxl",
modelDescr ="Mixed logit model on Train's car purchase data",
indivID ="ID",
mixing = TRUE,
nCores = 1 ## here is the trouble maker
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
data("Car") ### load data from mlogit() package
Car$ID = 1:nrow(Car)
Car$Choice = as.numeric(substring(Car$choice, 7))
database = Car
# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #
choiceAnalysis_settings <- list(
alternatives = c(car1=1, car2=2,car3=3,car4=4,car5=5, car6=6),
avail = 1,
explanators = database[,c("college","hsg2","coml5")],
choiceVar = database$Choice
#rows = database$income>30000
)
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(priceLogInc = 0,
range = 0,
mu_log_b_oc = 0,
sigma_log_b_oc = -2,
meth = 0,
cng = 0,
mu_b_ev = 0,
sigma_b_ev = -2,
truck = 0,
mu_b_suv = 0,
sigma_b_suv = -2,
van = 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()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 0,
interUnifDraws = c(),
interNormDraws = c(),
intraDrawsType = "halton",
intraNDraws = 10,
intraUnifDraws = c(),
intraNormDraws = c("draws_oc", "draws_ev", "draws_suv")
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["opcost"]] = -exp( mu_log_b_oc + sigma_log_b_oc * draws_oc )
randcoeff[["ev"]] = mu_b_ev + sigma_b_ev * draws_oc
randcoeff[["suv"]] = mu_b_suv + sigma_b_suv * draws_suv
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['car1']] = suv * (type1 == "sportuv") + truck * (type1 == "truck") + van * (type1 == "van") + meth * (fuel1 == "methanol") + cng * (fuel1 == "cng") + ev * (fuel1 == "electric") + priceLogInc * price1 + range * range1 + opcost * cost1
V[['car2']] = suv * (type2 == "sportuv") + truck * (type2 == "truck") + van * (type2 == "van") + meth * (fuel2 == "methanol") + cng * (fuel2 == "cng") + ev * (fuel2 == "electric") + priceLogInc * price2 + range * range2 + opcost * cost2
V[['car3']] = suv * (type3 == "sportuv") + truck * (type3 == "truck") + van * (type3 == "van") + meth * (fuel3 == "methanol") + cng * (fuel3 == "cng") + ev * (fuel3 == "electric") + priceLogInc * price3 + range * range3 + opcost * cost3
V[['car4']] = suv * (type4 == "sportuv") + truck * (type4 == "truck") + van * (type4 == "van") + meth * (fuel4 == "methanol") + cng * (fuel4 == "cng") + ev * (fuel4 == "electric") + priceLogInc * price4 + range * range4 + opcost * cost4
V[['car5']] = suv * (type5 == "sportuv") + truck * (type5 == "truck") + van * (type5 == "van") + meth * (fuel5 == "methanol") + cng * (fuel5 == "cng") + ev * (fuel5 == "electric") + priceLogInc * price5 + range * range5 + opcost * cost5
V[['car6']] = suv * (type6 == "sportuv") + truck * (type6 == "truck") + van * (type6 == "van") + meth * (fuel6 == "methanol") + cng * (fuel6 == "cng") + ev * (fuel6 == "electric") + priceLogInc * price6 + range * range6 + opcost * cost6
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(car1=1, car2=2, car3=3, car4=4, car5=5, car6=6),
avail = 1,
choiceVar = Choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
#P = apollo_panelProd(P, apollo_inputs, functionality)
### Average across intra-individual draws
P = apollo_avgIntraDraws(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,
estimate_settings=list(hessianRoutine="maxLik"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)