Type: | Package |
Title: | Tools for Choice Model Estimation and Application |
Version: | 0.3.5 |
Description: | Choice models are a widely used technique across numerous scientific disciplines. The Apollo package is a very flexible tool for the estimation and application of choice models in R. Users are able to write their own model functions or use a mix of already available ones. Random heterogeneity, both continuous and discrete and at the level of individuals and choices, can be incorporated for all models. There is support for both standalone models and hybrid model structures. Both classical and Bayesian estimation is available, and multiple discrete continuous models are covered in addition to discrete choice. Multi-threading processing is supported for estimation and a large number of pre and post-estimation routines, including for computing posterior (individual-level) distributions are available. For examples, a manual, and a support forum, visit http://www.ApolloChoiceModelling.com. For more information on choice models see Train, K. (2009) <isbn:978-0-521-74738-7> and Hess, S. & Daly, A.J. (2014) <isbn:978-1-781-00314-5> for an overview of the field. |
License: | GPL-2 |
URL: | http://www.apolloChoiceModelling.com |
BugReports: | http://www.apolloChoiceModelling.com/forum/ |
Encoding: | UTF-8 |
LazyData: | true |
Depends: | R (≥ 4.3.0), stats, utils |
Imports: | Rcpp (≥ 1.0.0), maxLik, mnormt, mvtnorm, graphics, randtoolbox, numDeriv, parallel, Deriv, matrixStats, RSGHB, coda, tibble, stringr, bgw (≥ 0.1.3), cli, Rsolnp, rstudioapi |
LinkingTo: | Rcpp, RcppArmadillo, RcppEigen |
Suggests: | knitr, rmarkdown, testthat |
VignetteBuilder: | knitr |
RoxygenNote: | 7.3.2 |
NeedsCompilation: | yes |
Packaged: | 2025-03-12 09:21:23 UTC; stephane.hess |
Author: | Stephane Hess [aut, cre], David Palma [aut], Thomas Hancock [ctb] |
Maintainer: | Stephane Hess <S.Hess@leeds.ac.uk> |
Repository: | CRAN |
Date/Publication: | 2025-03-12 17:00:02 UTC |
Prints package startup message
Description
This function is only called by R when attaching the package.
Usage
.onAttach(libname, pkgname)
Arguments
libname |
Name of library. |
pkgname |
Name of package. |
Value
Nothing
Adds covariance matrix to Apollo model
Description
Receives an estimated model object, calculates its Hessian, and classical and robust covariance matrix, and returns the same model object, but with these additional elements.
Usage
apollo_addCovariance(model, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Value
model.
Attaches predefined variables.
Description
Attaches parameters and data to allow users to refer to individual variables by name without reference to the object that contains them.
Usage
apollo_attach(apollo_beta, apollo_inputs)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This function should be called at the beginning of apollo_probabilities
to make writing the log-likelihood more user-friendly. If used, then apollo_detach
should be called at the end of apollo_probabilities
, or more conveniently,
using on.exit after the initial call to apollo_attach
.
apollo_attach
attaches apollo_beta
, database
, draws
,
and the output of apollo_randCoeff
and apollo_lcPars
, if they are
defined by the user.
Value
Nothing.
Averages across inter-individual draws.
Description
Averages individual-specific likelihood across inter-individual draws.
Usage
apollo_avgInterDraws(P, apollo_inputs, functionality)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
Argument P
with (for most functionalities) the original contents averaged over inter-individual draws. Shape depends on argument functionality
.
-
"components"
: ReturnsP
without changes. -
"conditionals"
: ReturnsP
without averaging across draws. Drops all components except"model"
. -
"estimate"
: ReturnsP
containing the likelihood of the model averaged across inter-individual draws. Drops all components except"model"
. -
"gradient"
: ReturnsP
containing the gradient of the likelihood averaged across inter-individual draws. Drops all components except"model"
. -
"output"
: ReturnsP
containing the likelihood of all model components averaged across inter-individual draws. -
"prediction"
: ReturnsP
containing the probabilities/likelihoods of all alternatives for all model components averaged across inter-individual draws. -
"preprocess"
: ReturnsP
without changes. -
"raw"
: ReturnsP
without changes. -
"report"
: ReturnsP
without changes. -
"shares_LL"
: ReturnsP
without changes. -
"validate"
: ReturnsP
containing the likelihood of the model averaged across inter-individual draws. Drops all components except"model"
. -
"zero_LL"
: ReturnsP
without changes.
Averages across intra-individual draws.
Description
Averages observation-specific likelihood across intra-individual draws.
Usage
apollo_avgIntraDraws(P, apollo_inputs, functionality)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
Argument P
with (for most functionalities) the original contents averaged over intra-individual draws. Shape depends on argument functionality
.
-
"components"
: ReturnsP
without changes. -
"conditionals"
: ReturnsP
containing the likelihood of the model averaged across intra-individual draws. Drops all components except for"model"
. -
"estimate"
: ReturnsP
containing the likelihood of the model averaged across intra-individual draws. Drops all components except"model"
. -
"gradient"
: ReturnsP
containing the gradient of the likelihood averaged across intra-individual draws. Drops all components except"model"
. -
"output"
: ReturnsP
containing the likelihood of all model components averaged across intra-individual draws. -
"prediction"
: ReturnsP
containing the probabilities of all alternatives for all model components averaged across intra-individual draws. -
"preprocess"
: ReturnsP
without changes. -
"raw"
: ReturnsP
without changes. -
"report"
: ReturnsP
without changes. -
"validate"
: ReturnsP
containing the likelihood of the model averaged across intra-individual draws. Drops all components but"model"
. -
"zero_LL"
: ReturnsP
without changes.
Ben-Akiva & Swait test
Description
Carries out the Ben-Akiva & Swait test for non-nested models and reports the corresponding p-value.
Usage
apollo_basTest(model1, model2)
Arguments
model1 |
Either a character variable with the name (and possibly path) of a previously estimated model, or an estimated model in memory, as returned by apollo_estimate. |
model2 |
Either a character variable with the name (and possibly path) of a previously estimated model, or an estimated model in memory, as returned by apollo_estimate. |
Details
The two models need to both be discrete choice, and need to have been estimated on the same data.
Value
Ben-Akiva & Swait test p-value (invisibly)
Bootstrap a model
Description
Samples individuals with replacement from the database, and estimates the model for each sample.
Usage
apollo_bootstrap(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
estimate_settings = list(estimationRoutine = "bgw", maxIterations = 200, writeIter =
FALSE, hessianRoutine = "none", printLevel = 2L, silent = FALSE, maxLik_settings =
list()),
bootstrap_settings = list(nRep = 30, samples = NA, calledByEstimate = FALSE, recycle =
TRUE)
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
estimate_settings |
List. Options controlling the estimation process. See apollo_estimate.
|
bootstrap_settings |
List containing settings for the sampling procedure. User input is required for all settings except those with a default or marked as optional.
|
Details
This function implements a basic block bootstrap. It estimates the model parameters on nRep
different samples.
Each new sample is constructed by sampling with replacement from the original full sample. Each new sample has as many
individuals as the original sample, though some of them may be repeated. Sampling is done at the individual level,
therefore if different individuals have different number of observations, each re-sample does not necessarily have the same number of observations.
If the sampling should be done at the individual level (not recommended for panel data), then the optional
bootstrap_settings$samples
argument should be provided.
For each sample, only the parameters and log-likelihood are estimated. Standard errors are not calculated (they may be added in future versions). The composition of the re-samples is stored in a file, but is stable with the same seed.
This function writes three different files to the working or output directory:
-
modelName_bootstrap_params.csv
: estimated parameters, final log-likelihood, and number of observations for each re-sample -
modelName_bootstrap_samples.csv
: composition of each re-sample. -
modelName_bootstrap_vcov.csv
: variance-covariance matrix of the estimated parameters across re-samples.
The first two files are updated throughout the run of this function, while the last one is only written once the function finishes.
When run, this function will look for the first two files above in the working/output directory. If they are found, the function will attempt to pick up re-sampling from where those files left off. This is useful in cases where the original bootstrapping was interrupted, or when additional re-sampling runs are to be performed.
Value
List with three elements.
-
estimates
: Matrix containing the parameter estimates for each repetition. As many rows as repetitions and as many columns as parameters. -
LL
: Vector of final log-likelihoods of each repetition. -
varcov
: Covariance matrix of the estimated parameters across the repetitions.
This function also writes three output files to the working/output directory, with the following names ('x' represents the model name):
-
x_bootstrap_params.csv: Table containing the parameter estimates, log-likelihood, and number of observations for each repetition.
-
x_bootstrap_samples.csv: Table containing the description of the sample used in each repetition. Same format than
bootstrap_settings$samples
. -
x_bootstrap_vcov: Table containing the covariance matrix of estimated parameters across the repetitions.
Checks definitions of Apollo functions
Description
Checks that the user-defined functions used by Apollo are correctly defined by the user.
Usage
apollo_checkArguments(
apollo_probabilities = NA,
apollo_randCoeff = NA,
apollo_lcPars = NA
)
Arguments
apollo_probabilities |
Function. Likelihood function as defined by the user. |
apollo_randCoeff |
Function. Used with mixing models. Constructs the random parameters of a mixing model. Receives two arguments:
|
apollo_lcPars |
Function. Used with latent class models. Constructs a list of parameters for each latent class. Receives two arguments:
|
Details
It only checks that the functions have the correct definition of inputs. It does not run the functions.
Value
Returns (invisibly) TRUE if definitions are correct, and FALSE otherwise.
Reports market share for subsamples
Description
Compares market shares across subsamples in dataset, and conducts statistical tests.
Usage
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
Arguments
choiceAnalysis_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
apollo_control |
List. Options controlling the running of the code. See apollo_validateInputs. |
database |
data.frame. Data used by model. |
Details
Saves the output to a csv file in the working/output directory.
Value
Silently returns a matrix containing the mean value for each explanator for those cases where an alternative is chosen and where it is not chosen,
as well as the t-test comparing those means (H0: equivalence).
The table is also written to a file called modelName_choiceAnalysis.csv
and printed to screen.
Calculates class allocation probabilities for a Latent Class model
Description
Calculates class allocation probabilities for a Latent Class model using a Multinomial Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_classAlloc(classAlloc_settings)
Arguments
classAlloc_settings |
List of inputs of the MNL model. It should contain the following.
|
Value
The returned object depends on the value of argument functionality
, which it fetches from the calling stack (see apollo_validateInputs).
-
"components"
: Same as"estimate"
. -
"conditionals"
: Same as"estimate"
. -
"estimate"
: List of vector/matrices/arrays with the allocation probabilities for each class. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
. -
"prediction"
: Same as"estimate"
. -
"preprocess"
: Returns a list with pre-processed inputs, based onclassAlloc_settings
. -
"raw"
: Same as"estimate"
. -
"report"
: Same as"estimate"
. -
"shares_LL"
: List with probabilities for each class in an equal shares setting. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: List with probabilities for each class in an equal shares setting.
Calculates Cross-Nested Logit probabilities
Description
Calculates the probabilities of a Cross-nested Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_cnl(cnl_settings, functionality)
Arguments
cnl_settings |
List of inputs of the CNL model. User input is required for all settings except those with a default or marked as optional.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
For the model to be consistent with utility maximisation, the estimated value of the lambda parameter of all nests
should be between 0 and 1. Lambda parameters are inversely proportional to the correlation between the error terms of
alternatives in a nest. If lambda=1, there is no relevant correlation between the unobserved
utility of alternatives in that nest.
Alpha parameters inside cnlStructure
should be between 0 and 1. Using a transformation to ensure
this constraint is satisfied is recommended for complex structures (e.g. logistic transformation).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: Not implemented. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the chosen alternative probability. -
"preprocess"
: Returns a list with pre-processed inputs, based oncnl_settings
. -
"raw"
: Same as"prediction"
. -
"report"
: List with tree structure and choice overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Calculates Cross-Nested Logit probabilities
Description
Calculates the probabilities of a Cross-nested Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_cnl2(cnl_settings, functionality)
Arguments
cnl_settings |
List of inputs of the CNL model. User input is required for all settings except those with a default or marked as optional.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
For the model to be consistent with utility maximisation, the estimated value of the lambda parameter of all nests
should be between 0 and 1. Lambda parameters are inversely proportional to the correlation between the error terms of
alternatives in a nest. If lambda=1, there is no relevant correlation between the unobserved
utility of alternatives in that nest.
Alpha parameters inside cnlStructure
should be between 0 and 1. Using a transformation to ensure
this constraint is satisfied is recommended for complex structures (e.g. logistic transformation).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: Not implemented. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the chosen alternative probability. -
"preprocess"
: Returns a list with pre-processed inputs, based oncnl_settings
. -
"raw"
: Same as"prediction"
. -
"report"
: List with tree structure and choice overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Combines separate model components.
Description
Combines model components to create likelihood for overall model.
Usage
apollo_combineModels(
P,
apollo_inputs,
functionality,
components = NULL,
asList = TRUE
)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
components |
Character vector. Optional argument. Names of elements in P that should be multiplied to construct the whole model likelihood. If a single element is provided, it is interpreted as a regular expression. Default is to include all components in P. |
asList |
Logical. Only used if |
Details
This function should be called inside apollo_probabilities after all model components have been produced.
It should be called before apollo_avgInterDraws, apollo_avgIntraDraws, apollo_panelProd and apollo_prepareProb, whichever apply, except where these functions are called inside any latent class components of the overall model.
Value
Argument P
with (for most functionalities) an extra element called "model", which is the product of all the other elements. Shape depends on argument functionality
.
-
"components"
: ReturnsP
without changes. -
"conditionals"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
. -
"estimate"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
. -
"gradient"
: ReturnsP
containing the gradient of the likelihood after applying the product rule across model components. -
"output"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
. -
"prediction"
: ReturnsP
without changes. -
"preprocess"
: ReturnsP
without changes. -
"raw"
: ReturnsP
without changes. -
"shares_LL"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
. -
"validate"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
. -
"zero_LL"
: ReturnsP
with an extra component called"model"
, which is the product of all other elements ofP
.
Write model results to file
Description
Writes results from various models to a single csv file.
Usage
apollo_combineResults(combineResults_settings = NULL)
Arguments
combineResults_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Value
Nothing, but writes a file called 'model_comparison_[date].csv' in the working/output directory.
Compares the content of apollo_inputs to their counterparts in the global environment
Description
Compares the content of apollo_inputs to their counterparts in the global environment
Usage
apollo_compareInputs(apollo_inputs)
Arguments
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Value
Logical. TRUE if the content of apollo_inputs
is the same than the one in the global environment, FALSE otherwise.
Calculates conditionals
Description
Calculates posterior expected values (conditionals) of random coefficient models (continuous or discrete mixtures/latent class)
Usage
apollo_conditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This functions is only meant for use with models using either continuous distributions or latent classes, not both at the same time
Value
Depends on whether the model uses continuous mixtures or latent class.
If the model contains a continuous mixture, the function returns a list of matrices. Each matrix has dimensions nIndiv x 3. One matrix per random component. Each row of each matrix contains the indivID of an individual, and the posterior mean and s.d. of this random component for this individual.
If the model contains latent classes, the function returns a matrix with the posterior class allocation probabilities for each individual.
If the model contains both continuous mixtures and latent classes, the function fails.
Calculates gradients of utility functions
Description
Calculates gradients (derivatives) of utility functions.
Usage
apollo_dVdB(apollo_beta, apollo_inputs, V)
Arguments
apollo_beta |
Named numeric vector of parameters. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
V |
List of functions |
Value
Named list. Each element is itself a list of functions: the partial derivatives of the elements of V.
Calculates gradients of utility functions
Description
Calculates gradients (derivatives) of utility functions.
Usage
apollo_dVdBOld(apollo_beta, apollo_inputs, V)
Arguments
apollo_beta |
Named numeric vector of parameters. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
V |
List of functions |
Value
Named list. Each element is a function that returns a list, where each element is the partial derivatives of the elements of V.
Delta method for Apollo models
Description
Applies the Delta method to calculate the standard errors of transformations of parameters.
Usage
apollo_deltaMethod(model, deltaMethod_settings)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
deltaMethod_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
apollo_deltaMethod
can be used in two ways. The first and recommended way is to provide an
element called expression
inside its argument deltaMethod_settings
. expression
should contain the expression or expressions for which the standard error is/are to be calculated, as text. For
example, to calculate the ratio between parameters b1 and b2, expression=c(vtt="b1/b2")
should be used.
The second method is to provide the name of a specific operation inside deltaMethod_settings
.
The following five operations are supported.
-
diff
: Calculates the s.e. ofparName1
-parName2
andparName2
-parName1
-
exp
: Calculates the s.e. ofexp(parName1)
-
logistic
: If onlyparName1
is provided, it calculates the s.e. ofexp(parName1)/(1+exp(parName1))
and1/(1+exp(parName1))
. IfparName1
andparName2
are provided, it calculatesexp(par_i)/(1+exp(parName1)+exp(parName2))
for i=1, 2, and 3 (par_3 = 1). -
lognormal
: Calculates the mean and s.d. of a lognormal distribution based on the mean (parName1
) and s.d. (parName2
) of the underlying normal. -
prod
: Calculates the s.e. ofparName1
*parName2
-
ratio
: Calculates the s.e. ofparName1
/parName2
andparName2
/parName1
-
sum
: Calculates the s.e. ofparName1
+parName2
By default, apollo_deltaMethod
uses the robust covariance matrix. However, the user can change this through the varcov
setting.
Value
Matrix containing value, s.e. and t-ratio resulting from the requested expression or operation. This is also printed to screen.
Detaches parameters and the database.
Description
Detaches variables attached by apollo_attach.
Usage
apollo_detach(apollo_beta = NA, apollo_inputs = NA)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This function detaches the variables attached by apollo_attach.
It should be called at the end of apollo_probabilities
, only if
apollo_attach was called and the beginning. This can also be achieved
by adding the line on.exit(apollo_detach(apollo_beta, apollo_inputs))
right after calling apollo_attach.
This function can also be called without any arguments, i.e. apollo_detach()
.
Value
Nothing.
Calculate DFT probabilities
Description
Calculate probabilities of a Decision Field Theory (DFT) model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_dft(dft_settings, functionality)
Arguments
dft_settings |
List of settings for the DFT model. It should contain the following elements.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: Not implemented. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the chosen alternative probability. -
"preprocess"
: Returns a list with pre-processed inputs, based ondft_settings
. -
"raw"
: Same as"prediction"
-
"report"
: Choice overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
-
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
References
Hancock, T.; Hess, S. and Choudhury, C. (2018) Decision field theory: Improvements to current methodology and comparisons with standard choice modelling techniques. Transportation Research 107B, 18 - 40. Hancock, T.; Hess, S. and Choudhury, C. (Submitted) An accumulation of preference: two alternative dynamic models for understanding transport choices. Roe, R.; Busemeyer, J. and Townsend, J. (2001) Multialternative decision field theory: A dynamic connectionist model of decision making. Psychological Review 108, 370
Pre-process input for common models return
Description
Pre-process input for common models return
Usage
apollo_diagnostics(inputs, modelType, apollo_inputs, data = TRUE, param = TRUE)
Arguments
inputs |
List of settings |
modelType |
Character. Type of model, e.g. "mnl", "nl", "cnl", etc. |
apollo_inputs |
List of main inputs to the model estimation process. See apollo_validateInputs. |
data |
Boolean. TRUE for printing report related to dependent and independent variables. FALSE for not printing it. Default is TRUE. |
param |
Boolean. TRUE for printing report related to estimated parameters (e.g. model structure). FALSE for not printing it. Default is TRUE. |
Value
(invisibly) TRUE if no error happend during execution.
Simulated dataset of medication choice.
Description
A simulated dataset containing 10,000 stated medication choices among four alternatives.
Usage
apollo_drugChoiceData
Format
A data.frame with 10,000 rows and 33 variables:
- ID
Numeric. Identification number of the individual.
- task
Numeric. Index of choice situations for each individual, going from 1 to 10.
- best
Numeric. Index of alternative selected as best option.
- second_pref
Numeric. Index of alternative selected as second-best option.
- third_pref
Numeric. Index of alternative selected as third-best option.
- worst
Numeric. Index of alternative selected as worst option.
- brand_1
Character. Brand for alternative 1.
- country_1
Character. Country of origin for alternative 1.
- char_1
Character. Characteristics of alternative 1 (standard, fast acting, or double strength).
- side_effects_1
Numeric. Chance of suffering negative side effects with alternative 1 (out of 100,000).
- price_1
Numeric. Cost of alternative 1 in Pounds sterling (GBP).
- brand_2
Character. Brand for alternative 2.
- country_2
Character. Country of origin for alternative 2.
- char_2
Character. Characteristics of alternative 2 (standard, fast acting, or double strength).
- side_effects_2
Numeric. Chance of suffering negative side effects with alternative 2 (out of 100,000).
- price_2
Numeric. Cost of alternative 2 in Pounds sterling (GBP).
- brand_3
Character. Brand for alternative 3.
- country_3
Character. Country of origin for alternative 3.
- char_3
Character. Characteristics of alternative 3 (standard, fast acting, or double strength).
- side_effects_3
Numeric. Chance of suffering negative side effects with alternative 3 (out of 100,000).
- price_3
Numeric. Cost of alternative 3 in Pounds sterling (GBP).
- brand_4
Character. Brand for alternative 4.
- country_4
Character. Country of origin for alternative 4.
- char_4
Character. Characteristics of alternative 4 (standard, fast acting, or double strength).
- side_effects_4
Numeric. Chance of suffering negative side effects with alternative 4 (out of 100,000).
- price_4
Numeric. Cost of alternative 4 in Pounds sterling (GBP).
- regular_user
Numeric. 1 if the respondent is a regular user of headache medicine, 0 otherwise.
- university_educated
Numeric. 1 if the respondent holds a university degree, 0 otherwise.
- over_50
Numeric. 1 if the respondent is 50 years old or older, 0 otherwise.
- attitude_quality
Numeric. Level of agreement from 1 (strongly disagree) to 5 (strongly agree) with the phrase 'I am concerned about the quality of drugs developed by unknown companies'.
- attitude_ingredients
Numeric. Level of agreement from 1 (strongly disagree) to 5 (strongly agree) with the phrase 'I believe that ingredients are the same no matter what brand'.
- attitude_patent
Numeric. Level of agreement from 1 (strongly disagree) to 5 (strongly agree) with the phrase 'The original patent holders have valuable experience with their medicines'.
- attitude_dominance
Numeric. Level of agreement from 1 (strongly disagree) to 5 (strongly agree) with the phrase 'I believe the dominance of big pharmaceutical companies is unhelpful'.
Details
This dataset is to be used for discrete choice modelling. Data comes from 1,000 individuals, each with ten stated choice (SC) scenarios involving a choice among headache medication. There are 10,000 choices in total. Data is simulated. Each observation contains attributes of the alternatives, characteristics of the respondent, and their answers to four attitudinal questions. All four alternatives are always available for all individuals. Alternatives 1 and 2 are branded, while alternatives 3 and 4 are generic. Respondents provide a full ranking of alternatives for each choice task (i.e. observation).
Source
http://www.apollochoicemodelling.com/
Calculates Exploded Logit probabilities
Description
Calculates the probabilities of an Exploded Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_el(el_settings, functionality)
Arguments
el_settings |
List of inputs of the Exploded Logit model. It shoud contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
The function calculates the probability of a ranking as a product of Multinomial Logit models with gradually reducing availability, where scale differences can be allowed for.
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: Not applicable (NA
). -
"preprocess"
: Returns a list with pre-processed inputs, based onel_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Choice overview across stages. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
-
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
MDC model with exogenous budget
Description
Calculates the likelihood function of the MDC model with exogenous budget. Can also predict and validate inputs.
Usage
apollo_emdc(emdc_settings, functionality = "estimate")
Arguments
emdc_settings |
List of settings for the model. It includes the following.
|
functionality |
Character. Either "validate", "zero_LL", "estimate", "conditionals", "raw", "output" or "prediction" |
Details
This model extends the traditional multiple discrete-continuous (MDC) framework by (i) making the marginal utility of the outside good deterministic, and (ii) including complementarity and substitution in the model formulation. See the following working paper for more details:
Palma, D. & Hess, S. (2022) Extending the Multiple Discrete Continuous (MDC) modelling framework to consider complementarity, substitution, and an unobserved budget. Transportation Reserarch 161B, 13 - 35. https://doi.org/10.1016/j.trb.2022.04.005
Value
The returned object depends on the value of argument functionality
as follows.
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero. -
"conditionals"
: Same as"estimate"
-
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"raw"
: Same as"prediction"
MDC model with exogenous budget
Description
Calculates the likelihood function of the MDC model with exogenous budget. Can also predict and validate inputs.
Usage
apollo_emdc1(emdc_settings, functionality = "estimate")
Arguments
emdc_settings |
List of settings for the model. It includes the following.
|
functionality |
Character. Either "validate", "zero_LL", "estimate", "conditionals", "raw", "output" or "prediction" |
Details
This model extends the traditional multiple discrete-continuous (MDC) framework by (i) making the marginal utility of the outside good deterministic, and (ii) including complementarity and substitution in the model formulation. See the following working paper for more details:
Palma, D. & Hess, S. (2022) Extending the Multiple Discrete Continuous (MDC) modelling framework to consider complementarity, substitution, and an unobserved budget. Transportation Reserarch 161B, 13 - 35. https://doi.org/10.1016/j.trb.2022.04.005
Value
The returned object depends on the value of argument functionality
as follows.
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero. -
"conditionals"
: Same as"estimate"
-
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"raw"
: Same as"prediction"
Extended MDC
Description
Calculates the likelihood function of the extended MDC model. Can also predict and validate inputs.
Usage
apollo_emdc2(emdc_settings, functionality = "estimate")
Arguments
emdc_settings |
List of settings for the model. It includes the following.
|
functionality |
Character. Either "validate", "zero_LL", "estimate", "conditionals", "raw", "output" or "prediction" |
Details
This model extends the traditional multiple discrete-continuous (MDC) framework by (i) dropping the need to define a budget, (ii) making the marginal utility of the outside good deterministic, and (iii) including complementarity and substitution in the model formulation. See the following working paper for more details:
Palma, D. & Hess, S. (Working Paper) Some adaptations of Multiple Discrete-Continuous Extreme Value (MDCEV) models for a computationally tractable treatment of complementarity and substitution effects, and reduced influence of budget assumptions
Avilable at: http://stephanehess.me.uk/publications.html
Value
The returned object depends on the value of argument functionality
as follows.
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero. -
"conditionals"
: Same as"estimate"
-
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"raw"
: Same as"prediction"
Estimates model
Description
Estimates a model using the likelihood function defined by apollo_probabilities
.
Usage
apollo_estimate(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
estimate_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
estimate_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
This is the main function of the Apollo package. The estimation process begins by running a number of checks on the
apollo_probabilities
function provided by the user.
If all checks are passed, estimation begins. There is no limit to estimation time other than reaching the maximum number of
iterations. If Bayesian estimation is used, estimation will finish once the predefined number of iterations are completed.
By default, this functions writes the estimated parameter values in each iteration to a file in the working/output directory. Writing
can be turned off by setting estimate_settings$writeIter
to FALSE
.
By default, final results are not written into a file nor printed to the console, so users must make sure
to call function apollo_modelOutput and/or apollo_saveOutput afterwards.
Users are strongly encouraged to visit http://www.apollochoicemodelling.com/ to download examples on how to use the Apollo package.
The webpage also provides a detailed manual for the package, as well as a user-group to get further help.
Value
model object
Estimates model using Bayesian estimation
Description
Estimates a model using Bayesian estimation on the likelihood function defined by apollo_probabilities
.
Usage
apollo_estimateHB(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
estimate_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
estimate_settings |
List. Options controlling the estimation process, as used for in apollo_estimate. |
Details
This is a sub function of apollo_estimate which is called when using Bayesian estimation.
Value
model object
Expands loops in a function or expression
Description
Expands loops replacing the index by its value. It also evaluates paste
and paste0
, and removes get
.
Usage
apollo_expandLoop(f, apollo_inputs, validate = TRUE)
Arguments
f |
function (usually |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
validate |
Logical. If TRUE, the new function will be validated before being returned |
Details
For example, the expression
for(j in 1:3) V[[paste0('alt',j)]] = b1*get(paste0('x',j)) + b2*X[,j]
would be expanded into:
V[[alt1]] = b1*x1 + b2*X[,1]
V[[alt2]] = b1*x2 + b2*X[,2]
V[[alt3]] = b1*x3 + b2*X[,3]
Value
A function or an expression (same type as input f
)
Keeps only the first row for each individual
Description
Given a multi-row input, keeps only the first row for each individual.
Usage
apollo_firstRow(P, apollo_inputs)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components (or other object). |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This a function to keep only the first row of an object per indidividual. It can handle multiple types of components, including scalars, vectors and three-dimensional arrays (cubes). The argument database MUST contain a column called 'apollo_sequence', which is created by apollo_validateData.
Value
If P
is a list, then it returns a list where each element has only the first row of each individual.
If P
is a single element, then it returns a single element with only the first row of each individual.
The size of the element is changed only in the first dimension. If input is a scalar, then it returns a vector with the element repeated as many
times as individuals in database
. If the element is a vector, its length will be changed to the number of individuals. If the element is
a matrix, then its first dimension will be changed to the number of individuals, while keeping the size of the second dimension. If the element
is a cube, then only the first dimension's length is changed, preserving the others.
Compares log-likelihood of model across categories
Description
Given the estimates of a model, it compares the log-likelihood at the observation level across categories of observations.
Usage
apollo_fitsTest(model, apollo_probabilities, apollo_inputs, fitsTest_settings)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
fitsTest_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
Prints a table comparing the average log-likelihood at the observation level for each category.
Value
Matrix with average log-likelihood at observation level per category (invisibly).
Calculates Fractional Multinomial Logit probabilities
Description
Calculates the probabilities of a Fractional Multinomial Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_fmnl(fmnl_settings, functionality)
Arguments
fmnl_settings |
List of inputs of the FMNL model. It should contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onfmnl_settings
. -
"raw"
: Same as"prediction"
-
"report"
: Overview of dependent variable -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Calculates Fractional Nested Logit probabilities
Description
Calculates the probabilities of a Fractional Nested Logit (FNL) model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_fnl(fnl_settings, functionality)
Arguments
fnl_settings |
List of inputs of the FNL model. It should contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
In this implementation of the Nested Logit model, each nest must have a lambda parameter associated to it.
For the model to be consistent with utility maximisation, the estimated value of the Lambda parameter of all nests
should be between 0 and 1. Lambda parameters are inversely proportional to the correlation between the error terms of
alternatives in a nest. If lambda=1, then there is no relevant correlation between the unobserved
utility of alternatives in that nest.
The tree must contain an upper nest called "root"
. The lambda parameter of the root is automatically
set to 1 if not specified in nlNests
, but can be changed by the user if desired (though not advised).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: Not implemented. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onfnl_settings
. -
"raw"
: Same as"prediction"
-
"report"
: List with tree structure and choice overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Prepares environment
Description
Prepares environment (the global environment if called by the user) for model definition and estimation.
Usage
apollo_initialise()
Details
This function detaches variables and makes sure that output is directed to console. It does not delete variables from the working environment.
Value
Nothing.
Adds componentName2 to model calls
Description
Adds componentName2 to model calls
Usage
apollo_insertComponentName(e)
Arguments
e |
An expression or a function. It will usually be apollo_probabilities. |
Value
The original argument 'e' but modified to incorporate a new setting called 'componentName2' to every call to apollo_<model> (e.g. apollo_mnl, apollo_nl, etc.).
Modifies function to make it compatible with analytic gradients
Description
Takes a likelihood function and inserts function ()
before key elements to allow for analytic gradient calculation
Usage
apollo_insertFunc(f, like = TRUE, randCoeff = FALSE, lcPars = FALSE)
Arguments
f |
Function. Expressions inside it will be turned into functions. Usually |
like |
Logical. Must be TRUE if |
randCoeff |
Logical. Must be TRUE if |
lcPars |
Logical. Must be TRUE if |
Details
It modifies the definition of the following models.
-
apollo_mnl
: Turns all elements insidemnl_settings$V
into functions. -
apollo_ol
: Turnsol_settings$V
and all elements insideol_settings$tau
into functions. -
apollo_op
: Turnsop_settings$V
and all elements insideop_settings$tau
into functions. -
apollo_normalDensity
: TurnsnormalDensity_settings$xNormal
,normalDensity_settings$mu
andnormalDensity_settings$sigma
into functions.
It can only track a maximum of 3 levels of depth in definitions. For example:
V <- list()
V[["A"]] <- b1*x1A + b2*x2A
V[["B"]] <- b1*x1B + b2*x2B
mnl_settings1 <- list(alternatives=c("A", "B"), V = V, choiceVar= Y, avail = 1, componentName="MNL1")
P[["MNL1"]] <- apollo_mnl(mnl_settings1, functionality)
But it may not be able to deal with the following:
VA <- b1*x1A + b2*x2A
V <- list()
V[["A"]] <- VA
V[["B"]] <- b1*x1B + b2*x2B
mnl_settings1 <- list(alternatives=c("A", "B"), V = V, choiceVar= Y, avail = 1, componentName="MNL1")
P[["MNL1"]] <- apollo_mnl(mnl_settings1, functionality)
But that might be enough given how apollo_dVdB works.
Value
Function f
but with relevant expressions turned into function definitions.
Replaces tau=c(...)
by tau=list(...)
in calls to apollo_ol
Description
Takes a function, looks for calls to apollo_ol, identifies the corresponding
ol_settings, then goes inside the definition of ol_settings and replaces
tau=c(...)
for tau=list(...)
.
Usage
apollo_insertOLList(f)
Arguments
f |
Function. Usually |
Details
This only goes one level deep in definitions. For example, it will work
correctly in the following cases:
ol_settings = list(outcomeOrdered = y1,
V = b1*x1,
tau = c(tau11, tau12))
P[["OL1"]] = apollo_ol(ol_settings, functionality)
P[["OL2"]] = apollo_ol(list(outcomeOrdered=y2, V=b2*x2, tau=c(tau21, tau22)),
functionality)
But it will not work on the following cases:
Tau = c(tau1, tau2, tau3)
ol_settings = list(outcomeOrdered = y2,
V = b2*x2,
tau = Tau)
P[["OL1"]] = apollo_ol(ol_settings, functionality)
P[["OL2"]] = apollo_ol(list(outcomeOrdered=y1, V=b1*x1, tau=Tau), functionality)
This function is called by apollo_modifyUserDefFunc to allow for analytical gradients when using apollo_ol.
Value
Function f
with tau=c(...)
replaced by
tau=list(...)
.
Introduces quotes into rrm_settings
Description
Takes a function, looks for the definition of relevant parts of rrm_settings, and introduces quotes on them. This is to facilitate their processing by apollo_rrm under functionality="preprocessing".
Usage
apollo_insertRRMQuotes(f)
Arguments
f |
Function. Usually |
Value
Function f
with relevant expressions turned into character.
Inserts rows
Description
Given a numeric object (scalar, vector, matrix or 3-dim array) inserts rows in the specified places.
Usage
apollo_insertRows(v, r, val)
Arguments
v |
Numeric scalar, vector, matrix or 3-dim array. |
r |
Boolean vector. TRUE for inserting a row from |
val |
Numeric scalar. Value that will fill new rows. |
Details
In general, r
should be longer than the number of rows in utilities
, and sum(r)=nrow(v)
.
If not, then a new object with as many rows as r
will be returned. Old rows will be taken
from utilities
from the top down.
Value
The same argument v
but with rows added where r==FALSE
.
Scales variables inside a function
Description
It changes the syntax of the function by replacing variable names for their scaled form, e.g. x –> x*apollo_inputs$apollo_scale[["x"]]. In assignments, it only scales the right side of the assignment.
Usage
apollo_insertScaling(e, sca)
Arguments
e |
Function, expression, call or symbol to alter. |
sca |
Named numeric vector with the scales. The names in these vectors determine which variables should be scaled. |
Value
A function, expression, call or symbol with the corresponding variables scaled.
Keeps only some rows
Description
Given a numeric object (scalar, vector, matrix or 3-dim array) keeps only the specified rows.
Usage
apollo_keepRows(v, r)
Arguments
v |
Numeric scalar, vector, matrix or 3-dim array. |
r |
Boolean vector. As many elements as rows in |
Value
The same argument utilities
but with the rows where r==FALSE
removed.
Calculates the likelihood of a latent class model
Description
Given within class probabilities, and class allocation probabilities, calculates the probabilities of an Exploded Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_lc(lc_settings, apollo_inputs, functionality)
Arguments
lc_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Returns nothing. -
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all models components, for each class. -
"preprocess"
: Returns a list with pre-processed inputs, based onlc_settings
. -
"raw"
: Same as"prediction"
-
"report"
: Class allocation overview. -
"shares_LL"
: Same as"estimate"
-
"validate"
: Same as"estimate"
, but also runs a set of tests on the given arguments. -
"zero_LL"
: Same as"estimate"
Calculates conditionals for latent class models.
Description
Calculates posterior expected values (conditionals) of class allocation probabilities for each individual.
Usage
apollo_lcConditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This function can only be used with latent class models without continuous heterogeneity.
Value
A matrix with the posterior class allocation probabilities for each individual.
Uses EM for latent class model
Description
Uses the EM algorithm for estimating a latent class model.
Usage
apollo_lcEM(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
lcEM_settings = NA,
estimate_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
lcEM_settings |
List. Options controlling the EM process.
|
estimate_settings |
List. Options controlling the estimation process within each EM iteration. See apollo_estimate for details. |
Details
This function uses the EM algorithm for estimating a Latent Class model. It is only suitable for models without
continuous mixing. All parameters need to vary across classes and need to be included in the apollo_lcPars
function which is used by apollo_lcEM
.
Value
model object
Returns unconditionals for a latent class model model
Description
Returns values for random parameters and class allocation probabilities in a latent class model model.
Usage
apollo_lcUnconditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Value
List of object, one per random component and one for the class allocation probabilities.
Calculates log-likelihood of all model components
Description
Calculates the log-likelihood of each model component as well as the whole model.
Usage
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs, silent = FALSE)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
silent |
Boolean. If TRUE, no information is printed to the console by the function. Default is FALSE. |
Details
This function calls apollo_probabilities with functionality="output". It then reorders the list of likelihoods so that "model" goes first.
Value
A list of vectors. Each vector corresponds to the log-likelihood of the whole model (first element) or a model component.
Loads model from file
Description
Loads a previously estimated model object from a file.
Usage
apollo_loadModel(modelName)
Arguments
modelName |
Character. Name of the model to load. |
Details
This function looks for a file named modelName_model.rds
in the working or output directory, loads the object contained in it, and returns it.
Value
A model object.
Converts data from long to wide format.
Description
Converts choice data from long to wide format, with one row per observation as opposed to one row per alternative/observation.
Usage
apollo_longToWide(longData, longToWide_settings)
Arguments
longData |
data.frame. Data in long format. |
longToWide_settings |
List. Contains settings for this function. User input is required for all settings.
|
Value
Silently returns a data.frame with the wide format version of the data. An overview of the data is printed to screen.
Likelihood ratio test
Description
Calculates the likelihood ratio test value between two models and reports the corresponding p-value.
Usage
apollo_lrTest(model1, model2)
Arguments
model1 |
Either a character variable with the name of a previously estimated model, or an estimated model in memory, as returned by apollo_estimate. |
model2 |
Either a character variable with the name of a previously estimated model, or an estimated model in memory, as returned by apollo_estimate. |
Details
The two models need to have been estimated on the same data, and one model needs to be nested within the other model.
Value
LR-test p-value (invisibly)
Creates cluster for estimation.
Description
Splits data, creates cluster and loads different pieces of the database on each worker.
Usage
apollo_makeCluster(
apollo_probabilities,
apollo_inputs,
silent = FALSE,
cleanMemory = FALSE
)
Arguments
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
silent |
Boolean. If TRUE, no messages are printed to the terminal. FALSE by default. It overrides |
cleanMemory |
Boolean. If TRUE, it saves apollo_inputs to disc, and removes database and draws from the apollo_inputs in .GlobalEnv and the parent environment. |
Details
Internal use only. Called by apollo_estimate
before estimation. Using multiple cores greatly increases memory consumption.
Value
Cluster (i.e. an object of class cluster from package parallel)
Creates draws for models with mixing
Description
Creates a list containing all draws necessary to estimate a model with mixing.
Usage
apollo_makeDraws(apollo_inputs, silent = FALSE)
Arguments
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
silent |
Boolean. If true, then no information is printed to console or default output. FALSE by default. |
Details
Internal use only. Called by apollo_validateInputs
.
This function creates a list whose elements are the sets of draws requested by the user for use in a model with mixing.
If the model does not include mixing, then it is not necessary to run this function.
The number of draws has a massive impact on memory usage and estimation time. Memory usage and number of computations
scale geometrically as N*interNDraws*intraNDraws (where N is the number of observations). Special care should be taken
when using both inter and intra-individual draws, as memory usage can easily reach the GB order of magnitude. Also, keep in
mind that using several threads (i.e. multicore) at least doubles the memory usage.
This function returns a list, with each element representing a random component of the mixing model. The dimensions
of the array depend on the type of draws used.
If only inter-individual draws are used, then draws are stored as 2-dimensional arrays (i.e. matrices).
If intra-individual draws are used, then draws are stored as 3-dimensional arrays.
The first dimension of the arrays (rows) correspond with the observations in the database.
The second dimension of the arrays (columns) correspond to the number of inter-individual draws.
The third dimension of the arrays correspond to the number of intra-individual draws.
Value
List. Each element is an array of draws representing a random component of the mixing model.
Creates gradient function.
Description
Creates gradient function from the likelihood function apollo_probabilities provided by the user. Returns NULL if the creation of gradient function fails.
Usage
apollo_makeGrad(
apollo_beta,
apollo_fixed,
apollo_logLike,
validateGrad = FALSE
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_logLike |
Function to calculate the log-likelihood of the model, as created by apollo_makeLogLike If provided, the value of the analytical gradient will be compared to the value of the numerical gradient as calculated using apollo_logLike and the numDeriv package. If the difference between the two is bigger than 1 that the analytical gradient is wrong and NULL will be returned. |
validateGrad |
Logical. If TRUE, it compares the value of the analytical gradient evaluated at apollo_beta against the numeric gradient (using numDeriv) at the same value. If the difference is bigger than 1 return NULL. |
Details
Internal use only. Called by apollo_estimate
before estimation.
The returned function can be single-threaded or multi-threaded based on the model options.
Value
apollo_gradient function. It receives the following arguments
-
b
Numeric vector of _variable_ parameters (i.e. must not include fixed parameters). -
countIter
Not used. Included only to mirror inputs of apollo_logLike. -
getNIter
Not used. Included only to mirror inputs of apollo_logLike. -
sumLL
Not used. Included only to mirror inputs of apollo_logLike. -
writeIter
Not used. Included only to mirror inputs of apollo_logLike.
If the creation of the gradient function fails, then it returns NULL.
Creates hessian function.
Description
Creates hessian function from the likelihood function apollo_probabilities provided by the user. Returns NULL if the creation of gradient function fails.
Usage
apollo_makeHessian(apollo_beta, apollo_fixed, apollo_logLike)
Arguments
apollo_beta |
Named numeric vector. Names and values for (all) parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_logLike |
Function to calculate the log-likelihood of the model, as created by apollo_makeLogLike If provided, the value of the analytical gradient will be compared to the value of the numerical gradient as calculated using apollo_logLike and the numDeriv package. If the difference between the two is bigger than 1 that the analytical gradient is wrong and NULL will be returned. |
Details
Internal use only. Called by apollo_estimate
before estimation.
The returned function can be single-threaded or multi-threaded based on the model options.
Value
apollo_hessian function. It receives a single argument called b
, which are the _variable_
parameters (i.e. must not include fixed parameters).
Creates log-likelihood function.
Description
Creates log-likelihood function from the likelihood function apollo_probabilities provided by the user.
Usage
apollo_makeLogLike(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
apollo_estSet = list(estimationRoutine = "bgw"),
cleanMemory = FALSE
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
apollo_estSet |
List of estimation options. It must contain at least one element called estimationRoutine defining the estimation algorithm. See apollo_estimate. |
cleanMemory |
Logical. If TRUE, then |
Details
Internal use only. Called by apollo_estimate
before estimation.
The returned function can be single-threaded or multi-threaded based on the model options.
Value
apollo_logLike function.
Calculates MDCEV likelihoods
Description
Calculates the likelihoods of a Multiple Discrete Continuous Extreme Value (MDCEV) model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_mdcev(mdcev_settings, functionality)
Arguments
mdcev_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the observed consumption for each observation. -
"gradient"
: Not implemented -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: A matrix with one row per observation, and columns indicating means and s.d. of continuous and discrete predicted consumptions. -
"preprocess"
: Returns a list with pre-processed inputs, based onmdcev_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Dependent variable overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Calculates MDCEV likelihoods
Description
Calculates the likelihoods of a Multiple Discrete Continuous Extreme Value (MDCEV) model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_mdcev2(mdcev_settings, functionality)
Arguments
mdcev_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the observed consumption for each observation. -
"gradient"
: Not implemented -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: A matrix with one row per observation, and columns indicating means and s.d. of continuous and discrete predicted consumptions. -
"preprocess"
: Returns a list with pre-processed inputs, based onmdcev_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Dependent variable overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Calculates MDCNEV likelihoods
Description
Calculates the likelihoods of a Multiple Discrete Continuous Nested Extreme Value (MDCNEV) model with an outside good and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_mdcnev(mdcnev_settings, functionality)
Arguments
mdcnev_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the observed consumption for each observation. -
"gradient"
: Not implemented -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: A matrix with one row per observation, and columns indicating means and s.d. of continuous and discrete predicted consumptions. -
"preprocess"
: Returns a list with pre-processed inputs, based onmdcnev_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Dependent variable overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Calculates conditionals for continuous mixture models
Description
Calculates posterior expected values (conditionals) of continuously distributed random coefficients, as well as their standard deviations.
Usage
apollo_mixConditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This functions is only meant for use with continuous distributions
Value
List of matrices. Each matrix has dimensions nIndiv x 3. One matrix per random component. Each row of each matrix contains the indivID of an individual, and the posterior mean and s.d. of this random component for this individual
Uses EM for models with continuous random coefficients
Description
Uses the EM algorithm for estimating a model with continuous random coefficients.
Usage
apollo_mixEM(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
mixEM_settings = NA,
estimate_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. These need to be provided in the following order. With K random parameters, K means for the underlying Normals, followed by the elements of the lower triangle of the Cholesky matrix, by row. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
mixEM_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
estimate_settings |
List. Options controlling the estimation process within each EM iteration. See apollo_estimate for details. |
Details
This function uses the EM algorithm for estimating a model with continuous random coefficients. It is only suitable for models where all parameters are random, with a full covariance matrix. All random parameters need to be based on underlying Normals with a full covariance matrix, but any transform thereof can be used.
Value
model object
Returns draws for continuously distributed random parameters in mixture model
Description
Returns draws (unconditionals) for random parameters in model, including interactions with deterministic covariates.
Usage
apollo_mixUnconditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This functions is only meant for use with continuous distributions
Value
List of object, one per random coefficient. With inter-individual draws only, this will be a matrix, with one row per individual, and one column per draw. With intra-individual draws, this will be a three-dimensional array, with one row per observation, inter-individual draws in the second dimension, and intra-individual draws in the third dimension.
Generate random draws using MLHS algorithm
Description
Generate random draws using the Modified Latin Hypercube Sampling algorithm.
Usage
apollo_mlhs(N, d, i)
Arguments
N |
Numeric. The number of draws to generate in each dimension |
d |
Numeric. The number of dimensions to generate draws in |
i |
Numeric. The number of individuals to generate draws for |
Details
Internal use only. Algorithm described in Hess, S., Train, K., and Polak, J. (2006) Transportation Research Part B, 40, 147 - 163.
Value
A (N*i) x d matrix with random draws
Calculates Multinomial Logit probabilities
Description
Calculates the probabilities of a Multinomial Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_mnl(mnl_settings, functionality)
Arguments
mnl_settings |
List of inputs of the MNL model. It should contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onmnl_settings
. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"raw"
: Same as"prediction"
-
"report"
: Choice overview -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
-
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Simulated dataset of mode choice.
Description
A simulated dataset containing 8,000 mode choices among four alternatives.
Usage
apollo_modeChoiceData
Format
A data.frame with 8,000 rows and 25 variables:
- ID
Numeric. Identification number of the individual.
- RP
Numeric. 1 if the row corresponds to a revealed preference (RP) observation. 0 otherwise.
- RP_journey
Numeric. Consecutive ID of RP observations. 0 if SP observation.
- SP
Numeric. 1 if the row corresponds to a stated preference (SP) observation. 0 otherwise.
- SP_task
Numeric. Consecutive ID of SP choice tasks. 0 if RP observation.
- access_air
Numeric. Access time (in minutes) of mode air.
- access_bus
Numeric. Access time (in minutes) of mode bus.
- access_rail
Numeric. Access time (in minutes) of mode rail.
- av_air
Numeric. 1 if the mode air (plane) is available. 0 otherwise.
- av_bus
Numeric. 1 if the mode bus is available. 0 otherwise.
- av_car
Numeric. 1 if the mode car is available. 0 otherwise.
- av_rail
Numeric. 1 if the mode rail (train) is available. 0 otherwise.
- business
Numeric. Purpose of the trip. 1 for business, 0 for other.
- choice
Numeric. Choice indicator, 1=car, 2=bus, 3=air, 4=rail.
- cost_air
Numeric. Cost (in GBP) of mode air.
- cost_bus
Numeric. Cost (in GBP) of mode bus.
- cost_car
Numeric. Cost (in GBP) of mode car.
- cost_rail
Numeric. Cost (in GBP) of mode rail.
- female
Numeric. Sex of individual. 1 for female, 0 for male.
- income
Numeric. Income (in GBP per annum) of the individual.
- service_air
Numeric. Additional services for the air alternative. 1 for no-frills, 2 for wifi, 3 for food. This is not used in the RP data, where it is set to 0.
- service_rail
Numeric. Additional services for the rail alternative. 1 for no-frills, 2 for wifi, 3 for food. This is not used in the RP data, where it is set to 0.
- time_air
Numeric. Travel time (in minutes) of mode air.
- time_bus
Numeric. Travel time (in minutes) of mode bus.
- time_car
Numeric. Travel time (in minutes) of mode car.
- time_rail
Numeric. Travel time (in minutes) of mode rail.
Details
This dataset is to be used for discrete choice modelling. Data comes from 500 individuals, each with two revealed preferences (RP) observation, and 14 stated stated (SC) observations. There are 8,000 choices in total. Data is simulated. Each observation contains attributes for the alternatives, availability of alternatives, and characteristics of the individuals.
Source
http://www.apollochoicemodelling.com/
Prints estimation results to console
Description
Prints estimation results to console. Amount of information presented can be adjusted through arguments.
Usage
apollo_modelOutput(model, modelOutput_settings = NA)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
modelOutput_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
Prints to screen the output of a model previously estimated by apollo_estimate()
Value
A matrix of coefficients, s.d. and t-tests (invisible)
Checks and modifies Apollo user-defined functions
Description
Checks and enhances user defined functions apollo_probabilities, apollo_randCoeff and apollo_lcPars.
Usage
apollo_modifyUserDefFunc(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
validate = TRUE,
noModification = FALSE
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names of parameters inside apollo_beta whose values should be kept constant throughout estimation. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
validate |
Logical. If TRUE, the original and modified
|
noModification |
Logical. If TRUE, loop expansion etc are skipped. |
Details
Internal use only. Called by apollo_estimate
before estimation.
Checks include: no re-definition of variables, no (direct) calls to database,
calling of apollo_weighting if weights are defined.
Value
List with four elements: apollo_probabilities, apollo_randCoeff, apollo_lcPars and a dummy called success (TRUE if modification was successful, FALSE if not. FALSE will be only be returnes if the modifications are validated).
Calculates Nested Logit probabilities
Description
Calculates the probabilities of a Nested Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_nl(nl_settings, functionality)
Arguments
nl_settings |
List of inputs of the NL model. It should contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
In this implementation of the Nested Logit model, each nest must have a lambda parameter associated to it.
For the model to be consistent with utility maximisation, the estimated value of the Lambda parameter of all nests
should be between 0 and 1. Lambda parameters are inversely proportional to the correlation between the error terms of
alternatives in a nest. If lambda=1, then there is no relevant correlation between the unobserved
utility of alternatives in that nest.
The tree must contain an upper nest called "root"
. The lambda parameter of the root is automatically
set to 1 if not specified in nlNests
, but can be changed by the user if desired (though not advised).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: Not implemented. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onnl_settings
. -
"raw"
: Same as"prediction"
-
"report"
: List with tree structure and choice overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Calculates density for a Normal distribution
Description
Calculates density for a Normal distribution at a specific value with a specified mean and standard deviation and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_normalDensity(normalDensity_settings, functionality)
Arguments
normalDensity_settings |
List of arguments to the functions. It must contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function calculates the probability of the linear model outcomeNormal = mu + xNormal + epsilon, where epsilon is a random error distributed Normal(0,sigma).
If using this function in the context of an Integrated Choice and Latent Variable (ICLV) model with continuous
indicators, then outcomeNormal
would be the value of the indicator, xNormal
would be the value of the latent variable (possibly
multiplied by a parameter to measure its correlation with the indicator, e.g. xNormal=lambda*LV), and mu
would be
an additional parameter to be estimated (the mean of the indicator, which should be fixed to zero if the indicator is
centered around its mean beforehand).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the likelihood for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: Predicted value at the observation level. -
"preprocess"
: Returns a list with pre-processed inputs, based onnormalDensity_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Dependent variable overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Calculates Ordered Logit probabilities
Description
Calculates the probabilities of an Ordered Logit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_ol(ol_settings, functionality)
Arguments
ol_settings |
List of settings for the OL model. It should include the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function estimates an Ordered Logit model of the type:
y* = V + epsilon
outcomeOrdered = 1 if -Inf < y* < tau[1]
2 if tau[1] < y* < tau[2]
...
maxLvl if tau[length(tau)] < y* < +Inf
Where epsilon is distributed standard logistic, and the values 1, 2, ..., maxLvl can be
replaces by coding[1], coding[2], ..., coding[maxLvl].
The behaviour of the function changes depending on the value of the functionality
argument.
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all possible levels, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onol_settings
. -
"raw"
: Same as"prediction"
-
"report"
: Dependent variable overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Calculates Ordered Probit probabilities
Description
Calculates the probabilities of an Ordered Probit model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_op(op_settings, functionality)
Arguments
op_settings |
List of settings for the OP model. It should include the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function estimates an ordered probit model of the type:
y^{*} = V + \epsilon \\
y = 1 if -\infty < y^{*} < \tau_1,
2 if \tau_1 < y^{*} < \tau_2,
...,
max(y) if \tau_{max(y)-1} < y^{*} < \infty
Where \epsilon
is distributed standard normal, and the values 1, 2, ..., max(y)
can be
replaced by coding[1], coding[2], ..., coding[maxLvl]
.
The behaviour of the function changes depending on the value of the functionality
argument.
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all possible levels, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onop_settings
. -
"raw"
: Same as"prediction"
-
"report"
: Dependent variable overview. -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Cross-validation of fit (LL)
Description
Randomly generates estimation and validation samples, estimates the model on the first and calculates the likelihood for the second, then repeats.
Usage
apollo_outOfSample(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
estimate_settings = list(estimationRoutine = "bgw", maxIterations = 200, writeIter =
FALSE, hessianRoutine = "none", printLevel = 3L, silent = TRUE),
outOfSample_settings = list(nRep = 10, validationSize = 0.1, samples = NA, rmse = NULL)
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in
|
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
estimate_settings |
List. Options controlling the estimation process. See apollo_estimate. |
outOfSample_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
A common way to test for overfitting of a model is to measure its fit on a sample not used during estimation that is, measuring its out-of-sample fit. A simple way to do this is splitting the complete available dataset in two parts: an estimation sample, and a validation sample. The model of interest is estimated using only the estimation sample, and then those estimated parameters are used to measure the fit of the model (e.g. the log-likelihood of the model) on the validation sample. Doing this with only one validation sample, however, may lead to biased results, as a particular validation sample need not be representative of the population. One way to minimise this issue is to randomly draw several pairs of estimation and validation samples from the complete dataset, and apply the procedure to each pair.
The splitting of the database into estimation and validation samples is done
at the individual level, not at the observation level. If the sampling wants
to be done at the individual level (not recommended on panel data), then the
optional outOfSample_settings$samples
argument should be provided.
This function writes two different files to the working/output directory:
-
modelName_outOfSample_params.csv
: Records the estimated parameters, final log-likelihood, and number of observations on each repetition. -
modelName_outOfSample_samples.csv
: Records the sample composition of each repetition.
The first two files are updated throughout the run of this function, while the last one is only written once the function finishes.
When run, this function will look for the two files above in the working/output directory. If they are found, the function will attempt to pick up re-sampling from where those files left off. This is useful in cases where the original bootstrapping was interrupted, or when additional re-sampling wants to be performed.
Value
A matrix with the average log-likelihood per observation for both the estimation and validation samples, for each repetition. Two additional files with further details are written to the working/output directory.
Calculates own model probabilities
Description
Receives functions or expressions for each functionality so that a user-defined model can interface with Apollo.
Usage
apollo_ownModel(ownModel_settings, functionality)
Arguments
ownModel_settings |
List of arguments. Only likelihood is mandatory.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onmnl_settings
. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"raw"
: Same as"prediction"
-
"report"
: Choice overview -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
-
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Calculates product across observations from same individual.
Description
Multiplies likelihood of observations from the same individual, or adds the log of them.
Usage
apollo_panelProd(P, apollo_inputs, functionality)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function should be called inside apollo_probabilities only if the data has a panel structure. It should be called after apollo_avgIntraDraws if intra-individual draws are used.
Value
Argument P
with (for most functionalities) the original contents after multiplying across observations at the individual level. Shape depends on argument functionality
.
-
"components"
: ReturnsP
without changes. -
"conditionals"
: ReturnsP
without averaging across draws. Drops all components except"model"
. -
"estimate"
: ReturnsP
containing the likelihood of the model after multiplying observations at the individual level. Drops all components except"model"
. -
"gradient"
: ReturnsP
containing the gradient of the likelihood after applying the product rule across observations for the same individual. -
"output"
: ReturnsP
containing the likelihood of the model after multiplying observations at the individual level. -
"prediction"
: ReturnsP
containing the probabilities/likelihoods of all alternatives for all model components averaged across inter-individual draws. -
"preprocess"
: ReturnsP
without changes. -
"raw"
: ReturnsP
without changes. -
"report"
: ReturnsP
without changes. -
"shares_LL"
: ReturnsP
containing the likelihood of the model after multiplying observations at the individual level. -
"validate"
: ReturnsP
containing the likelihood of the model averaged across inter-individual draws. Drops all components except"model"
. -
"zero_LL"
: ReturnsP
containing the likelihood of the model after multiplying observations at the individual level.
Predicts using an estimated model
Description
Calculates apollo_probabilities
with functionality="prediction".
Usage
apollo_prediction(
model,
apollo_probabilities,
apollo_inputs,
prediction_settings = list(),
modelComponent = NA
)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
prediction_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
modelComponent |
Deprecated. Same as |
Details
Structure of predictions are simplified before returning, e.g. list of vectors are turned into a matrix.
Value
A list containing predictions for component modelComponent
of the model described in apollo_probabilities
.
The particular shape of the prediction will depend on the model component.
Checks likelihood function
Description
Checks that the likelihood function for the mode is in the appropriate format to be returned.
Usage
apollo_prepareProb(P, apollo_inputs, functionality)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function should be called inside apollo_probabilities
, near the end of it, just before return(P)
.
This function only performs checks on the shape of P, but does not change its values.
Value
Argument P
with (for most functionalities) the original contents. Output depends on argument functionality
.
-
"components"
: ReturnsP
without changes. -
"conditionals"
: Returns only the"model"
component of argumentP
. -
"estimate"
: Returns only the"model"
component of argumentP
. -
"gradient"
: Returns only the"model"
component of argumentP
. -
"output"
: Returns argumentP
without any changes to its content, but gives names to unnamed elements. -
"prediction"
: Returns argumentP
without any changes. -
"preprocess"
: Returns argumentP
without any changes to its content, but gives names to elements corresponding to componentNames. -
"raw"
: Returns argumentP
without any changes. -
"report"
: ReturnsP
without changes. -
"shares_LL"
: Returns argumentP
without any changes to its content, but gives names to unnamed elements. -
"validate"
: Returns argumentP
without any changes. -
"zero_LL"
: Returns argumentP
without any changes to its content, but gives names to unnamed elements.
Pre-process input for multiple models return
Description
Pre-process input for multiple models return
Usage
apollo_preprocess(inputs, modelType, functionality, apollo_inputs)
Arguments
inputs |
List of settings |
modelType |
Character. Type of model, e.g. "mnl", "nl", "cnl", etc. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Value
The returned object is a pre-processed version of the model settings. This is independent of functionality
, but the function is only called during preprocessing.
Prints message to terminal
Description
Prints message to terminal if apollo_inputs$silent
is FALSE
Usage
apollo_print(txt, nSignifD = 4, widthLim = 11, pause = 0, type = "t")
Arguments
txt |
Character, what to print. |
nSignifD |
Optional numeric integer. Minimum number of significant digits when printing numeric matrices. Default is 4. |
widthLim |
Optional numeric integer. Minimum width (in characters) of each column when printing numeric matrices. Default is 11 |
pause |
Scalar integer. Number of seconds the execution will pause after printing the message. Default is 0. |
type |
Character. "t" for regular text (default), "w" for warning, "i" for information. |
Value
Nothing
Reads parameters from file
Description
Reads in parameters from a previously estimated model and copies the values to the given apollo_beta
vector, only for those parameters whose name matches.
Usage
apollo_readBeta(
apollo_beta,
apollo_fixed,
inputModelName,
overwriteFixed = FALSE
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
inputModelName |
Character. modelName for model from which results are used as starting values. |
overwriteFixed |
Boolean. TRUE if starting values for fixed parameters should also be updated from input file. |
Details
This function will update the values of the parameters in its argument apollo_beta
with the matching values in the file
(inputModelName)_estimates.csv
. If there is no match for a given parameter in apollo_beta
, its value will not be updated.
Value
Named numeric vector. Names and updated starting values for parameters.
Calculates Random Regret Minimisation model probabilities
Description
Calculates the probabilities of a Random Regret Minimisation model and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_rrm(rrm_settings, functionality)
Arguments
rrm_settings |
List of inputs of the RRM model. It should contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the probabilities for the chosen alternative for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: List of vectors/matrices/arrays. Returns a list with the probabilities for all alternatives, with an extra element for the probability of the chosen alternative. -
"preprocess"
: Returns a list with pre-processed inputs, based onrrm_settings
. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"raw"
: Same as"prediction"
-
"report"
: Choice overview -
"shares_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when only constants are estimated. -
"validate"
: Same as"estimate"
-
"zero_LL"
: vector/matrix/array. Returns the probability of the chosen alternative when all parameters are zero.
Saves estimation results to files.
Description
Writes files in the working/output directory with the estimation results.
Usage
apollo_saveOutput(model, saveOutput_settings = NA)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
saveOutput_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
Estimation results are saved different files in the working/output directory:
-
(modelName)_corr.csv
CSV file with the estimated classical correlation matrix. Only when bayesian estimation was not used. -
(modelName)_covar.csv
CSV file with the estimated classical covariance matrix. Only when bayesian estimation was not used. -
(modelName)_estimates.csv
CSV file with the estimated parameter values, their standars errors, and t-ratios. -
(modelName).F12
F12 file with model results. Compatible with ALOGIT. -
(modelName)_output.txt
Text file with the output produced by functionapollo_modelOutput
. -
(modelName)_robcorr.csv
CSV file with the estimated robust correlation matrix. Only when bayesian estimation was not used. -
(modelName)_robcovar.csv
CSV file with the estimated robust covariance matrix. Only when bayesian estimation was not used.
Value
nothing
Searches for better starting values.
Description
Given a set of starting values and a range for them, searches for points with a better likelihood and steeper gradients.
Usage
apollo_searchStart(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
searchStart_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
searchStart_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
This function implements a simplified version of the algorithm proposed by Bierlaire, M., Themans, M. & Zufferey, N. (2010), A Heuristic for Nonlinear Global Optimization, INFORMS Journal on Computing, 22(1), pp.59-70. The main difference lies in it implementing only two out of three tests on the candidates described by the authors. The implemented algorithm has the following steps.
Randomly draw
nCandidates
candidates from an interval given by the user.Label all candidates with a valid log-likelihood (LL) as active.
Apply
bfgsIter
iterations of the BFGS algorithm to each active candidate.Apply the following tests to each active candidate:
Has the BGFS search converged?
Are the candidate parameters after BFGS closer than
dTest
from any other candidate with higher LL?Is the LL of the candidate after BFGS further than
distLL
from a candidate with better LL, and its gradient smaller thangTest
?
Mark any candidates for which at least one test results in yes as inactive.
Go back to step 3, unless only one candidate is active, or the maximum number of iterations (
maxStages
) has been reached.
This function will write a CSV file to the working/output directory summarising progress. This file is called modelName
_searchStart.csv .
Value
named vector of model parameters. These are the best values found.
Sets specified rows to a given value
Description
Given a numeric object (scalar, vector, matrix or 3-dim array) sets a subset of rows to a given value.
Usage
apollo_setRows(v, r, val)
Arguments
v |
Numeric scalar, vector, matrix or 3-dim array. Rows of this object will be replaced by |
r |
Boolean vector. As many elements as rows in |
val |
Numeric scalar. Value to which the specified rows must be set to. |
Value
The same argument utilities
but with the rows where r==TRUE
set to val
.
Automatically sets working directory to active file directory
Description
This function only works in Rstudio. If called outside RStudio will just print a message to screen saying it could not set the working directory.
Usage
apollo_setWorkDir()
Value
(invisibly) TRUE if it manages to set the working directory, FALSE if not.
Compares predicted and observed shares
Description
Comparing the shares predicted by the model with the shares observed in the data, and conducts statistical tests.
Usage
apollo_sharesTest(
model,
apollo_probabilities,
apollo_inputs,
sharesTest_settings
)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
sharesTest_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
This is an auxiliary function to help guide the definition of utility functions in a choice model. By comparing the predicted and observed shares of alternatives for different categories of the data, it is possible to identify what additional explanatory variables could improve the fit of the model.
Value
Nothing
Starts or stops writing output to a text file.
Description
Starts or stops writing the output shown in the console to a file named "modelName_additional_output.txt".
Usage
apollo_sink(apollo_inputs = NULL)
Arguments
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. If not provided, it will be looked for in the global environment. |
Details
After the first time this function is called, all output shown in the console will also be written to a text file called "modelName_additional_output.txt", where "modelName" is the modelName set inside apollo_control. The second time this function is called, it stops writing the console output to the file. The user should always call this function an even number of times to close the output file and prevents data loss.
Value
Nothing.
Measures evaluation time of a model
Description
Measures the evaluation time of a model for different number of cores and draws.
Usage
apollo_speedTest(
apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs,
speedTest_settings = NA
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
speedTest_settings |
List. Contains settings for this function. User input is required for all settings except those with a default or marked as optional.
|
Details
This function evaluates the function apollo_probabilities
several times using different number of threads (a.k.a. processor cores),
and draws (if the model uses mixing). It then plots the estimation time for each combination.
Estimation time grows at least linearly with number of draws, while time savings decrease with the number of threads.
This function can help decide what number of draws and cores to use for estimation, though a high number of draws is always
recommended. If the computer will be used for additional activities during estimation, no more than (machine number of cores - 1) should be used.
Using more threads than cores available in the machine will lead to reduce dperformance.
The use of additional cores come at the expense of additional memory usage. If R uses more memory than the physical RAM available,
then significant slow-downs in processing time can be expected. This function can help avoiding such pitfalls.
Value
A matrix with the average time per evaluation for each number of threads and draws combination. A graph is also plotted.
Dataset of route choice.
Description
A Stated Preference dataset containing 3,492 route choices among two alternatives.
Usage
apollo_swissRouteChoiceData
Format
A data frame with 3,492 rows and 16 variables:
- ID
Numeric. Identification number of the individual.
- choice
Numeric. Choice indicator, 1 for alternative 1, and 2 for alternative 2.
- tt1
Numeric. Travel time (in minutes) for alternative 1.
- tc1
Numeric. Travel cost (in CHF) for alternative 1.
- hw1
Numeric. Headway time (in minutes) for alternative 1.
- ch1
Numeric. Number of interchanges for alternative 1.
- tt2
Numeric. Travel time (in minutes) for alternative 2.
- tc2
Numeric. Travel cost (in CHF) for alternative 2.
- hw2
Numeric. Headway time (in minutes) for alternative 2.
- ch2
Numeric. Number of interchanges for alternative 2.
- hh_inc_abs
Numeric. Household income (in CHF per annum).
- car_availability
Numeric. 1 if respondent has a car available, 0 otherwise.
- commute
Numeric. 1 if the purpose of the trip is commuting. 0 otherwise.
- shopping
Numeric. 1 if the purpose of the trip is shopping. 0 otherwise.
- business
Numeric. 1 if the purpose of the trip is business. 0 otherwise.
- leisure
Numeric. 1 if the purpose of the trip is leisure. 0 otherwise.
Details
This dataset is to be used for discrete choice modelling. Data comes from 388 individuals who participated in a Stated Choice (SC) survey, providing a total of 3,492 observations. Each choice scenario includes two alternatives described in terms of travel time, cost, headway and interchanges. Additional information on respondents is available. This dataset comes from the following publication. Vrtic, M. & Axhausen, K.W. (2003), The impact of tilting trains in Switzerland: A route choice model of regional and long distance public transport trips. 82nd annual meeting of the transportation research board, Washington, DC.
Source
http://www.apollochoicemodelling.com/
Dataset of time use.
Description
A Revealed Preference dataset containing 2,826 full-day observations.
Usage
apollo_timeUseData
Format
An object of class data.frame
with 2826 rows and 20 columns.
Details
This dataset is to be used for Multiple Discrete Continuous (MDC) modelling. Data comes from 447 individuals who provided activitry diaries for a total of 2,826 days. Each observation summarizes the amount of time spent in each of twelve different activities. The dataset also incluides characteristics of the participants. This dataset comes from the following publication. Calastri, C., Crastes dit Sourd, R. and Hess, S. (2020) We want it all: experiences from a survey seeking to capture social network structures, lifetime events and short-term travel and activity planning. Transportation, 47(1), pp. 175-201.
- indivID
Numeric. Identification number of the individual.
- day
Numeric. Index of the day for each observation (day 1 was excluded).
- date
Numeric. Date in format yyyymmdd.
- budget
Numeric. Total amount of time registered during the day (in minutes).
- t_a01
Numeric. Time spent dropping-of or picking up other people (in minutes).
- t_a02
Numeric. Time spent working (in minutes).
- t_a03
Numeric. Time spent on educational activities (in minutes).
- t_a04
Numeric. Time spent shopping (in minutes).
- t_a05
Numeric. Time spent on private business (in minutes).
- t_a06
Numeric. Time spent getting petrol (in minutes).
- t_a07
Numeric. Time spent on social or leasure activities (in minutes).
- t_a08
Numeric. Time spent on vacation or long (inter-city) travel (in minutes).
- t_a09
Numeric. Time spent doing exercise (in minutes).
- t_a10
Numeric. Time spent at home (in minutes).
- t_a11
Numeric. Time spent travelling (everyday travelling) (in minutes).
- t_a12
Numeric. Non-allocated time (in minutes).
- female
Numeric. 1 if respondent is female. 0 otherwise.
- age
Numeric. Age of respondent (in years, approximate).
- occ_full_time
Numeric. 1 if the respondent works full time.
- weekend
Numeric. 1 if the current date is a weekend.
Source
http://www.apollochoicemodelling.com/
Calculates density for a Tobit model (censored Normal)
Description
Calculates density for a censored Normal distribution at a specific value with a specified mean and standard deviation and user provided bounds, and can also perform other operations based on the value of the functionality
argument.
Usage
apollo_tobit(tobit_settings, functionality)
Arguments
tobit_settings |
List of arguments to the functions. It must contain the following.
|
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Details
This function calculates the probability of the linear model outcomeTobit = mu + xTobit + epsilon, where epsilon is a random error distributed Normal(0,sigma), but with optional lower and upper bounds imposed by the user (outside of which the density would be 0).
Value
The returned object depends on the value of argument functionality
as follows.
-
"components"
: Same as"estimate"
-
"conditionals"
: Same as"estimate"
-
"estimate"
: vector/matrix/array. Returns the likelihood for each observation. -
"gradient"
: List containing the likelihood and gradient of the model component. -
"output"
: Same as"estimate"
but also writes summary of input data to internal Apollo log. -
"prediction"
: Predicted value at the observation level. -
"preprocess"
: Returns a list with pre-processed inputs, based ontobit_settings
. -
"raw"
: Same as"estimate"
-
"report"
: Dependent variable overview. -
"shares_LL"
: Not implemented. Returns a vector of NA with as many elements as observations. -
"validate"
: Same as"estimate"
, but it also runs a set of tests to validate the function inputs. -
"zero_LL"
: Not implemented. Returns a vector of NA with as many elements as observations.
Returns unconditionals for models with random heterogeneity
Description
Returns unconditionals for random parameters in model, both for continuous mixtures and latent class.
Usage
apollo_unconditionals(model, apollo_probabilities, apollo_inputs)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
apollo_probabilities |
Function. Returns probabilities of the model to be estimated. Must receive three arguments:
|
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
This functions is only meant for use with models using continuous distributions or latent classes, or both at the same time.
Value
Depends on whether the model uses continuous mixtures or latent class.
If the model contains a continuous mixture, it returns a list with one object per random coefficient. When using inter-individual draws only, each element will be a matrix with one row per individual, and one column per draw. When using intra- individual draws, each element will be a three-dimensional array, with one row per observation, inter-individual draws in the second dimension, and intra- individual draws in the third dimension.
If the model contains latent classes, it returns a list with as many elements as random coefficients in the model, plus one additional element containing the class allocation probabilities.
If the model contains both continuous mixing and latent classes, a list with the two elements described above will be returned.
Pre-process input for common models return
Description
Pre-process input for common models return
Usage
apollo_validate(inputs, modelType, functionality, apollo_inputs)
Arguments
inputs |
List of settings |
modelType |
Character. Type of model, e.g. "mnl", "nl", "cnl", etc. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
apollo_inputs |
List of main inputs to the model estimation process. See apollo_validateInputs. |
Value
The returned object depends on the value of argument operation
Validates apollo_control
Description
Validates the options controlling the running of the code apollo_control
and sets default values for the omitted ones.
Usage
apollo_validateControl(database, apollo_control, silent = FALSE)
Arguments
database |
data.frame. Data used by model. |
apollo_control |
List. Options controlling the running of the code. User input is required for all settings except those with a default or marked as optional.
|
silent |
Boolean. If TRUE, no messages are printed to screen. |
Details
This function should be run before running apollo_validateData
.
Value
Validated version of apollo_control
, with additional element called panelData
set to TRUE for repeated choice data.
Validates data
Description
Checks consistency of the database with apollo_control
, sorts it by
indivID, and adds an internal ID variable (apollo_sequence
)
Usage
apollo_validateData(database, apollo_control, silent)
Arguments
database |
data.frame. Data used by model. |
apollo_control |
List. Options controlling the running of the code. See apollo_validateInputs. |
silent |
Boolean. TRUE to prevent the function from printing to the console. Default is FALSE. |
Details
This function should be called after calling apollo_validateControl.
Observations are sorted only if apollo_control$panelData=TRUE
.
Value
Data.frame. Validated version of database.
Validates the apollo_HB
list of parameters
Description
Validates the apollo_HB
list of parameters and sets default values for the omitted ones.
Usage
apollo_validateHBControl(
apollo_HB,
apollo_beta,
apollo_fixed,
apollo_control,
silent = FALSE
)
Arguments
apollo_HB |
List. Contains options for Bayesian estimation. See
|
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
apollo_control |
List. Options controlling the running of the code. See apollo_validateInputs. |
silent |
Boolean. TRUE to keep the function from printing to the console. Default is FALSE. |
Details
This function is only necessary when using bayesian estimation.
Value
Validated apollo_HB
Prepares input for apollo_estimate
Description
Searches the user work space (.GlobalEnv) for all necessary input to run apollo_estimate
, and packs it in a single list.
Usage
apollo_validateInputs(
apollo_beta = NA,
apollo_fixed = NA,
database = NA,
apollo_control = NA,
apollo_HB = NA,
apollo_draws = NA,
apollo_randCoeff = NA,
apollo_lcPars = NA,
recycle = FALSE,
silent = FALSE
)
Arguments
apollo_beta |
Named numeric vector. Names and values for parameters. |
apollo_fixed |
Character vector. Names (as defined in |
database |
data.frame. Data used by model. |
apollo_control |
List. Options controlling the running of the code. User input is required for all settings except those with a default or marked as optional.
|
apollo_HB |
List. Contains options for Bayesian estimation. See
|
apollo_draws |
List of arguments describing the inter and intra individual draws. Required only if
|
apollo_randCoeff |
Function. Used with mixing models. Constructs the random parameters of a mixing model. Receives two arguments:
|
apollo_lcPars |
Function. Used with latent class models. Constructs a list of parameters for each latent class. Receives two arguments:
|
recycle |
Logical. If TRUE, an older version of |
silent |
Logical. TRUE to keep the function from printing to the console. Default is FALSE. |
Details
All arguments to this function are optional. If the function is called without arguments, then it it will look in the user workspace (i.e. the global environment) for variables with the same name as its omitted arguments. We strongly recommend users to visit http://www.apollochoicemodelling.com/ for examples on how to use Apollo. In the website, users will also find a detailed manual and a user-group for help and further reference.
Value
List grouping several required input for model estimation.
Lists variable names and definitions used inside a function
Description
Returns a list containing the names and definitions of variables in f, apollo_randCoeff
and apollo_lcPars
Usage
apollo_varList(f, apollo_inputs)
Arguments
f |
A function, usually |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Details
It looks for variable definitions inside f, apollo_randCoeff
, and apollo_lcPars
. It returns
them in a list.
Value
A list of expressions containing all definitions in f, apollo_randCoeff
and apollo_probabilities
Calculates variance-covariance matrix of an Apollo model
Description
Calculates the Hessian, variance-covariance matrix and standard errors of an Apollo model as defined by its likelihood function
and apollo_inputs
list of settings. Performs automatic scaling for increased numeric stability.
Usage
apollo_varcov(apollo_beta, apollo_fixed, varcov_settings)
Arguments
apollo_beta |
Named numeric vector. Names and values of parameters at which to calculate the covariance matrix. Values must not be scaled, and they must include any fixed parameter. |
apollo_fixed |
Character vector. Names of fixed parameters. |
varcov_settings |
List of settings defining the behaviour of this function. It must contain at least one of
the following:
|
Details
It calculates the Hessian, variance-covariance, and standard errors at apollo_beta
values of an
estimated model. At least one of the following settings must be provided (ordered by speed of computation): apollo_grad
,
apollo_logLike
, or (apollo_probabilities
and apollo_inputs
). If more than one is provided,
then the priority is: apollo_grad
, apollo_logLike
, (apollo_probabilities
and apollo_inputs
).
Value
List with the following elements
-
apollo_beta
: Named numerical vector. Parameter estimates (model$estimate
, not scaled). -
corrmat
: Numerical matrix. Correlation between parameter estimates. -
hessian
: Numerical matrix. Hessian of the model at parameter estimates (model$estimate
). -
hessianScaling
: Named numeric vector. Scales used on the paramaters to calculate the Hessian (non-fixed only). -
methodsAttempted
: Character vector. Name of methods attempted to calculate the Hessian. -
methodUsed
: Character. Name of method used to calculate the Hessian. -
robcorrmat
: Numerical matrix. Robust correlation between parameter estimates. -
robse
: Named numerical vector. Robust standard errors of parameter estimates. -
robvarcov
: Numerical matrix. Robust variance-covariance matrix. -
se
: Named numerical vector. Standard errors of parameter estimates. -
varcov
: Numerical matrix. Variance-covariance matrix.
Applies weights
Description
Applies weights to individual observations in likelihood function.
Usage
apollo_weighting(P, apollo_inputs, functionality)
Arguments
P |
List of vectors, matrices or 3-dim arrays. Likelihood of the model components. |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
functionality |
Character. Setting instructing Apollo what processing to apply to the likelihood function. This is in general controlled by the functions that call
|
Value
The likelihood (i.e. probability in the case of choice models) of the model in the appropriate form for the given functionality, multiplied by individual-specific weights.
Writes an F12 file
Description
Writes an F12 file (ALogit format) with the results of a model estimation.
Usage
apollo_writeF12(model, truncateCoeffNames = TRUE)
Arguments
model |
Model object. Estimated model object as returned by function apollo_estimate. |
truncateCoeffNames |
Boolean. TRUE to truncate parameter names to 10 characters. TRUE by default. |
Value
Nothing.
Writes the vector [beta,ll] to a file called modelname_iterations.csv
Description
Writes the vector [beta,ll] to a file called modelname_iterations.csv
Usage
apollo_writeTheta(
beta,
ll,
modelName,
scaling = NULL,
outDir = NULL,
apollo_beta = NULL
)
Arguments
beta |
vector of parameters to be written (including fixed ones). |
ll |
scalar representing the log-likelihood of the whole model. |
modelName |
Character. Name of the model. |
scaling |
Numeric vector of scales applied to beta |
outDir |
Scalar character. Name of output directory |
apollo_beta |
Named numeric vector of starting values. |
Value
Nothing.
Validates and expands rows if necessary.
Description
Validates and expands rows if necessary.
Usage
aux_validateRows(rows, componentName = NULL, apollo_inputs = NULL)
Arguments
rows |
Boolean vector. Consideration of which rows to include. Length equal to the number of observations (nObs), with entries equal to TRUE for rows to include, and FALSE for rows to exclude. Default is |
componentName |
Character. Name given to model component. If not provided by the user, Apollo will set the name automatically according to the element in |
apollo_inputs |
List grouping most common inputs. Created by function apollo_validateInputs. |
Prints brief summary of Apollo model
Description
Receives an estimated model object and prints a brief summary using the generic print function.
Usage
## S3 method for class 'apollo'
print(x, ...)
Arguments
x |
Model object. Estimated model object as returned by function apollo_estimate. |
... |
further arguments passed to or from other methods. |
Value
nothing.
Prints summary of Apollo model
Description
Receives an estimated model object and prints a summary using the generic summary function.
Usage
## S3 method for class 'apollo'
summary(object, ..., pTwoSided = FALSE)
Arguments
object |
Model object. Estimated model object as returned by function apollo_estimate. |
... |
further arguments passed to or from other methods. |
pTwoSided |
Logical. Should two-sided p-values be printed instead of one-sided p-values. FALSE by default. #' @return nothing. |