Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html.
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

HB_MMNL does not run

Ask questions about existing examples and put in requests to software developers and users for other example implementations of models.
Post Reply
intllawKuma
Posts: 4
Joined: 29 Apr 2022, 08:06

HB_MMNL does not run

Post by intllawKuma » 01 May 2022, 17:41

Hello,

I tried to run HB_MMNL in Example Files, but I got the following error message.
Could you tell me what is wrong?



> # ################################################################# #
> #### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
> # ################################################################# #
>
> ### Clear memory
> rm(list = ls())
>
> ### Load Apollo library
> library(apollo)
Apollo 0.2.7
http://www.ApolloChoiceModelling.com
See url for a detailed manual, examples and a user forum.
Sign up to to user forum to receive updates on new releases.
>
> ### Initialise code
> apollo_initialise()
Apollo ignition sequence completed
>
> ### Set core controls
> apollo_control = list(
+ modelName = "HB_MMNL",
+ modelDescr = "HB model on mode choice SP data",
+ indivID = "ID",
+ HB = TRUE,
+ outputDirectory = "output"
+ )
>
> # ################################################################# #
> #### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
> # ################################################################# #
>
> ### Loading data from package
> ### if data is to be loaded from a file (e.g. called data.csv),
> ### the code would be: database = read.csv("data.csv",header=TRUE)
> database = apollo_modeChoiceData
> ### for data dictionary, use ?apollo_modeChoiceData
>
> ### Use only SP data
> database = subset(database,database$SP==1)
>
> ### Create new variable with average income
> database$mean_income = mean(database$income)
>
> # ################################################################# #
> #### DEFINE MODEL PARAMETERS ####
> # ################################################################# #
>
> ### Vector of parameters, including any that are kept fixed in estimation
> apollo_beta=c(asc_car = 0,
+ asc_bus = 0,
+ asc_air = 0,
+ asc_rail = 0,
+ asc_bus_shift_female = 0,
+ asc_air_shift_female = 0,
+ asc_rail_shift_female = 0,
+ b_tt_car =-3,
+ b_tt_bus =-3,
+ b_tt_air =-3,
+ b_tt_rail =-3,
+ b_tt_shift_business = 0,
+ b_access =-3,
+ b_cost =-3,
+ b_cost_shift_business = 0,
+ cost_income_elast = 0,
+ b_no_frills = 0,
+ b_wifi = 0,
+ b_food = 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_car","b_no_frills")
>
> # ################################################################# #
> #### HB settings ####
> # ################################################################# #
>
> apollo_HB = list(
+ hbDist = c(asc_car = "NR",
+ asc_bus = "N",
+ asc_air = "N",
+ asc_rail = "N",
+ asc_bus_shift_female = "NR",
+ asc_air_shift_female = "NR",
+ asc_rail_shift_female = "NR",
+ b_tt_car = "LN-",
+ b_tt_bus = "LN-",
+ b_tt_air = "LN-",
+ b_tt_rail = "LN-",
+ b_tt_shift_business = "NR",
+ b_access = "LN-",
+ b_cost = "LN-",
+ b_cost_shift_business = "NR",
+ cost_income_elast = "NR",
+ b_no_frills = "NR",
+ b_wifi = "CN+",
+ b_food = "CN+"),
+ gNCREP = 10000, # burn-in iterations
+ gNEREP = 50000, # post burn-in iterations
+ gINFOSKIP = 500)
>
> # ################################################################# #
> #### GROUP AND VALIDATE INPUTS ####
> # ################################################################# #
>
> apollo_inputs = apollo_validateInputs()

outputDirectory provided by user does not exist, so will be created.
Several observations per individual detected based on the value of
ID. Setting panelData in apollo_control set to TRUE.
All checks on apollo_control completed.
All checks on database completed.
All checks on apollo_HB completed.
Warning message:
In dir.create(apollo_control$outputDirectory) :
cannot create dir 'output', reason 'Permission denied'
>
> # ################################################################# #
> #### 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()
+
+ ### Create alternative specific constants and coefficients using interactions with socio-demographics
+ asc_bus_value = asc_bus + asc_bus_shift_female * female
+ asc_air_value = asc_air + asc_air_shift_female * female
+ asc_rail_value = asc_rail + asc_rail_shift_female * female
+ b_tt_car_value = b_tt_car + b_tt_shift_business * business
+ b_tt_bus_value = b_tt_bus + b_tt_shift_business * business
+ b_tt_air_value = b_tt_air + b_tt_shift_business * business
+ b_tt_rail_value = b_tt_rail + b_tt_shift_business * business
+ b_cost_value = ( b_cost + b_cost_shift_business * business ) * ( income / mean_income ) ^ cost_income_elast
+
+ ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant V = list()
+ V=list()
+ V[["car"]] = asc_car + b_tt_car_value * time_car + b_cost_value * cost_car
+ V[["bus"]] = asc_bus_value + b_tt_bus_value * time_bus + b_access * access_bus + b_cost_value * cost_bus
+ V[["air"]] = asc_air_value + b_tt_air_value * time_air + b_access * access_air + b_cost_value * cost_air + b_no_frills * ( service_air == 1 ) + b_wifi * ( service_air == 2 ) + b_food * ( service_air == 3 )
+ V[["rail"]] = asc_rail_value + b_tt_rail_value * time_rail + b_access * access_rail + b_cost_value * cost_rail + b_no_frills * ( service_rail == 1 ) + b_wifi * ( service_rail == 2 ) + b_food * ( service_rail == 3 )
+
+ ### Define settings for MNL model component
+ mnl_settings = list(
+ alternatives = c(car=1, bus=2, air=3, rail=4),
+ avail = list(car=av_car, bus=av_bus, air=av_air, rail=av_rail),
+ choiceVar = choice,
+ utilities = V
+ )
+
+ ### Compute probabilities using MNL model
+ P[["model"]] = apollo_mnl(mnl_settings, 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)
Preparing user-defined functions.
Testing probability function (apollo_probabilities)

Overview of choices for MNL model component :
car bus air rail
Times available 5446.00 6314.00 5264.00 6118.00
Times chosen 1946.00 358.00 1522.00 3174.00
Percentage chosen overall 27.80 5.11 21.74 45.34
Percentage chosen when available 35.73 5.67 28.91 51.88



Diagnostic checks passed. Please review before proceeding
-----------------------------------------------------------

Number of Individuals: 500
Number of Observations: 7000
Prior variance: 2
Target Acceptance (Fixed): 0.3
Target Acceptance (Normal): 0.3
Degrees of Freedom: 5
Avg. Number of Observations per Individual: 14
Initial Log-Likelihood: -19242.47502

-----------------------------------------------------------

Fixed Parameters Start
asc_bus_shift_female 0
asc_air_shift_female 0
asc_rail_shift_female 0
b_tt_shift_business 0
b_cost_shift_business 0
cost_income_elast 0

-----------------------------------------------------------

Random Parameters Start Dist.
asc_bus 0 N
asc_air 0 N
asc_rail 0 N
b_tt_car -3 LN-
b_tt_bus -3 LN-
b_tt_air -3 LN-
b_tt_rail -3 LN-
b_access -3 LN-
b_cost -3 LN-
b_wifi 0 CN+
b_food 0 CN+

-----------------------------------------------------------



Estimate Model? (Y/N):
Error in colMeans(model$F) :
'x' must be an array of at least two dimensions
> # ################################################################# #
> #### MODEL OUTPUTS ####
> # ################################################################# #
>
> # ----------------------------------------------------------------- #
> #---- FORMATTED OUTPUT (TO SCREEN) ----
> # ----------------------------------------------------------------- #
>
> apollo_modelOutput(model)
Error in apollo_modelOutput(model) : object 'model' not found
>
> # ----------------------------------------------------------------- #
> #---- FORMATTED OUTPUT (TO FILE, using model name) ----
> # ----------------------------------------------------------------- #
>
> apollo_saveOutput(model)
Error in apollo_saveOutput(model) : object 'model' not found
>
> # ################################################################# #
> ##### POST-PROCESSING ####
> # ################################################################# #
>
> ### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
> apollo_sink()
Writing output to file output/HB_MMNL_additional_output.txt. Please
run "apollo_sink()" again after finishing writing results.
Error in file(file, if (append) "a" else "w") :
cannot open the connection
In addition: Warning message:
In file(file, if (append) "a" else "w") :
cannot open file 'output/HB_MMNL_additional_output.txt': No such file or directory
>
> # ----------------------------------------------------------------- #
> #---- MODEL PREDICTIONS AND ELASTICITY CALCULATIONS ----
> # ----------------------------------------------------------------- #
>
> ### Use the estimated model to make predictions
> predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)
Error in apollo_prediction(model, apollo_probabilities, apollo_inputs) :
object 'model' not found
>
> ### Look at summary of the predicted choice probabilities
> summary(predictions_base[,3:ncol(predictions_base)])
Error in summary(predictions_base[, 3:ncol(predictions_base)]) :
object 'predictions_base' not found
>
> ### Now imagine the cost for rail increases by 10%
> database$cost_rail = 1.01*database$cost_rail
>
> ### Rerun predictions with the new data, and save into a separate matrix
> apollo_inputs = apollo_validateInputs()

outputDirectory provided by user does not exist, so will be created.
Several observations per individual detected based on the value of
ID. Setting panelData in apollo_control set to TRUE.
All checks on apollo_control completed.
All checks on database completed.
All checks on apollo_HB completed.
Warning message:
In dir.create(apollo_control$outputDirectory) :
cannot create dir 'output', reason 'Permission denied'
> predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)
Error in apollo_prediction(model, apollo_probabilities, apollo_inputs) :
object 'model' not found
>
> ### Look at summary of the predicted choice probabilities
> summary(predictions_new[,3:ncol(predictions_new)])
Error in summary(predictions_new[, 3:ncol(predictions_new)]) :
object 'predictions_new' not found
>
> ### Return to original data
> database$cost_rail = 1/1.01*database$cost_rail
> apollo_inputs = apollo_validateInputs()

outputDirectory provided by user does not exist, so will be created.
Several observations per individual detected based on the value of
ID. Setting panelData in apollo_control set to TRUE.
All checks on apollo_control completed.
All checks on database completed.
All checks on apollo_HB completed.
Warning message:
In dir.create(apollo_control$outputDirectory) :
cannot create dir 'output', reason 'Permission denied'
>
> ### Compute change in probabilities
> change=(predictions_new-predictions_base)/predictions_base
Error: object 'predictions_new' not found
>
> ### Not interested in chosen alternative now, so drop last column
> change=change[,-ncol(change)]
Error: object 'change' not found
> ### First two columns (change in ID and task) also not needed
> change=change[,-c(1,2)]
Error: object 'change' not found
>
> ### Look at first individual
> change[database$ID==1,]
Error: object 'change' not found
> ### And person 9, who has all 4 modes available
> change[database$ID==9,]
Error: object 'change' not found
>
> ### Summary of changes (possible presence of NAs for unavailable alternatives)
> summary(change)
Error in summary(change) : object 'change' not found
>
> ### Look at mean changes for subsets of the data, ignoring NAs
> colMeans(change,na.rm=TRUE)
Error in is.data.frame(x) : object 'change' not found
> colMeans(subset(change,database$business==1),na.rm=TRUE)
Error in subset(change, database$business == 1) :
object 'change' not found
> colMeans(subset(change,database$business==0),na.rm=TRUE)
Error in subset(change, database$business == 0) :
object 'change' not found
> colMeans(subset(change,(database$income<quantile(database$income,0.25))),na.rm=TRUE)
Error in subset(change, (database$income < quantile(database$income, 0.25))) :
object 'change' not found
> colMeans(subset(change,(database$income>=quantile(database$income,0.25))|(database$income<=quantile(database$income,0.75))),na.rm=TRUE)
Error in subset(change, (database$income >= quantile(database$income, :
object 'change' not found
> colMeans(subset(change,(database$income>quantile(database$income,0.75))),na.rm=TRUE)
Error in subset(change, (database$income > quantile(database$income, 0.75))) :
object 'change' not found
>
> ### Compute own elasticity for rail:
> log(sum(predictions_new[,6])/sum(predictions_base[,6]))/log(1.01)
Error: object 'predictions_new' not found
>
> ### Compute cross-elasticities for other modes
> log(sum(predictions_new[,3])/sum(predictions_base[,3]))/log(1.01)
Error: object 'predictions_new' not found
> log(sum(predictions_new[,4])/sum(predictions_base[,4]))/log(1.01)
Error: object 'predictions_new' not found
> log(sum(predictions_new[,5])/sum(predictions_base[,5]))/log(1.01)
Error: object 'predictions_new' not found
>
> # ----------------------------------------------------------------- #
> #---- switch off writing to file ----
> # ----------------------------------------------------------------- #
>
> apollo_sink()
Writing output to file output/HB_MMNL_additional_output.txt. Please
run "apollo_sink()" again after finishing writing results.
Error in file(file, if (append) "a" else "w") :
cannot open the connection
In addition: Warning message:
In file(file, if (append) "a" else "w") :
cannot open file 'output/HB_MMNL_additional_output.txt': No such file or directory
> apollo_sink()

stephanehess
Site Admin
Posts: 632
Joined: 24 Apr 2020, 16:29

Re: HB_MMNL does not run

Post by stephanehess » 03 May 2022, 03:11

Hi

this happens as you ran the whole file in one go. Please only run in down to apollo_estimate and then when you get to the prompt of whether to proceed

Code: Select all

Estimate Model? (Y/N):
press Y

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk

intllawKuma
Posts: 4
Joined: 29 Apr 2022, 08:06

Re: HB_MMNL does not run

Post by intllawKuma » 03 May 2022, 07:09

Dear Prof. Hess,

Thank you for answering my question.
As you pointed out, I ran the whole file in one go by using RStudio.
It worked well when I followed your advice.

Post Reply