| Type: | Package |
| Title: | Bunch-Gay-Welsch Statistical Estimation |
| Version: | 0.1.5 |
| Maintainer: | David S. Bunch <dsbunch@ucdavis.edu> |
| Description: | Performs statistical estimation and inference-related computations by accessing and executing modified versions of 'Fortran' subroutines originally published in the Association for Computing Machinery (ACM) journal Transactions on Mathematical Software (TOMS) by Bunch, Gay and Welsch (1993) <doi:10.1145/151271.151279>. The acronym 'BGW' (from the authors' last names) will be used when making reference to technical content (e.g., algorithm, methodology) that originally appeared in ACM TOMS. A key feature of BGW is that it exploits the special structure of statistical estimation problems within a trust-region-based optimization approach to produce an estimation algorithm that is much more effective than the usual practice of using optimization methods and codes originally developed for general optimization. The 'bgw' package bundles 'R' wrapper (and related) functions with modified 'Fortran' source code so that it can be compiled and linked in the 'R' environment for fast execution. This version implements a function ('bgw_mle.R') that performs maximum likelihood estimation (MLE) for a user-provided model object that computes probabilities (a.k.a. probability densities). The original motivation for producing this package was to provide fast, efficient, and reliable MLE for discrete choice models that can be called from the 'Apollo' choice modelling 'R' package ( see https://www.apollochoicemodelling.com). Starting with the release of Apollo 0.3.0, BGW is the default estimation package. However, estimation can also be performed using BGW in a stand-alone fashion without using 'Apollo' (as shown in simple examples included in the package). Note also that BGW capabilities are not limited to MLE, and future extension to other estimators (e.g., nonlinear least squares, generalized method of moments, etc.) is possible. The 'Fortran' code included in 'bgw' was modified by one of the original BGW authors (Bunch) under his rights as confirmed by direct consultation with the ACM Intellectual Property and Rights Manager. See https://authors.acm.org/author-resources/author-rights. The main requirement is clear citation of the original publication (see above). |
| License: | GPL-3 |
| Encoding: | UTF-8 |
| NeedsCompilation: | yes |
| Depends: | R (≥ 4.0.0) |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0), |
| VignetteBuilder: | knitr |
| Author: | David S. Bunch |
| Config/testthat/edition: | 3 |
| Config/roxygen2/version: | 8.1.1 |
| Packaged: | 2026-11-10 21:24:21 UTC; dsbunch |
| Repository: | CRAN |
| Date/Publication: | 2026-11-10 22:20:17 UTC |
Internal BGW Functions
Description
Internal BGW functions
Details
These are not to be called by the user.
Value
These functions are wrappers for 'Fortran' routines, and are not meant to be called by the user.
bgw_mle
Description
Performs maximum likelihood estimation (MLE) for the user-provided model
defined in calcR. Version 0.1.5.
Usage
bgw_mle(calcR, betaStart, calcJ = NULL, bgw_settings = NULL, calcJ_type = NULL)
Arguments
calcR |
Function that computes an n-vector (R) of model residuals for a p-vector of (numeric) parameters beta (the first argument). (The beta vector can be named or unnamed.) Because bgw_mle performs maximum likelihood estimation (MLE), R_i is the likelihood (probability) of the ith observation, and the estimate is the value of beta that minimizes the negative log-likelihood NLL = -Sum_i [log(R_i)]. |
betaStart |
Vector of initial starting values for beta. Can be either a named or unnamed vector. |
calcJ |
Optional function that computes analytic derivative (Jacobian) information for the n-vector R, with beta as its first argument, as for calcR. If calcJ is NULL (the default), estimation uses finite-difference derivatives. Starting with version 0.1.5, the Jacobian can be supplied in several forms (see Details), so whenever calcJ is supplied, calcJ_type should be supplied as well. |
bgw_settings |
List. Contains control parameters for BGW estimation code. All parameters have default values, so user input is entirely optional.
|
calcJ_type |
Character. Should be supplied whenever calcJ is supplied: |
Details
This function has been written to provide an R-based interface to Fortran estimation software published in Bunch, Gay and Welsch (1993), "Algorithm 717-Subroutines for Maximum Likelihood and Quasi-Likelihood Estimation of Parameters in Nonlinear Regression Models," ACM Transactions on Mathematical Software, 19 (1), March 1993, 109-130. The letters BGW will be used in various ways to denote the source of the estimation functionality.
A primary motivation was to develop a more efficient maximum likelihood estimation function for use in the Apollo choice modelling package: see https://www.apollochoicemodelling.com/. However, we have adopted a design whereby the BGW package is wholly independent of Apollo, and can be used in a stand-alone fashion. Note also that the BGW Fortran subroutines are written to support general statistical estimation for an arbitrary objective/criterion function. So, although this version of the package is specifically written for MLE, the package may see future updates that expand the number of estimation options (for, e.g., nonlinear least squares, generalized method of moments, etc.).
Remark: Following the convention in the numerical optimization literature, BGW minimizes the objective function. That is, bgw_mle minimizes the negative-log-likelihood for the model calcR.
Forms of the Jacobian (calcJ)
calcJ must return a matrix (not a vector), in one of the following forms.
Derivatives of probabilities. Formally, the Jacobian (J) is an n x p matrix whose [i,k]
element is dR_i/dbeta_k. The equivalent information can be provided as a p x n matrix (J
transpose). Because these are derivatives of probabilities, if J is provided in either form, the
user should also set calcJ_type = "probability".
Derivatives of log-probabilities. Alternatively, J can be provided as an n x p matrix
whose [i,k] element is d[log(R_i)]/dbeta_k, or, equivalently, the p x n matrix that is its
transpose. In either case the user should set calcJ_type = "log_probability".
Orientation. bgw identifies from the dimensions whether J is n x p or p x n. In the rare case where n = p, the dimensions cannot show this and the matrix is read as n x p, so a square matrix must be supplied in the n x p form.
Version 0.1.4 and earlier. In earlier versions of bgw, the assumed form for J was the
n x p matrix with [i,k] element d[log(R_i)]/dbeta_k. To maintain backward compatibility, if
calcJ_type is missing, bgw assumes that J contains derivatives of log(R_i). Because this cannot
be verified from the matrix itself, a calcJ that returns derivatives of probabilities must be
accompanied by calcJ_type = "probability"; otherwise the results will be wrong.
Value
model object of class 'bgw_mle'. Output of a bgw maximum likelihood estimation procedure. A list with the following attributes:
-
betaStart: Vector of initial starting values. -
bgw_settings: List. The final settings used, including any defaults. -
hasAnalyticGrad: Logical. Indicates if an analytical gradient calculation was used. If the user has not provided a calcJ function (see input parameter), it is set to FALSE. -
numParams: Numeric. Number of model parameters used in calcR. -
numResids: Numeric. Number of independent observations (model residuals) in data set = dimension of calcR output. -
code: Numeric. Numeric return code from BGW. Code 20 means that a model object at betaStart was requested, and no estimation was performed. Codes above 62 report a failure: 63, 64 and 66 at the start, and 65 when the gradient could not be computed. If code 65 occurs after the start, the last iterate at which the gradient could be computed is returned, with its log-likelihood, gradient and scale vector. -
message: Character. Message statement characterizing termination of MLE search. -
betaStop: Vector. Value of parameter vector at conclusion of MLE search. See message to determine if beta is a valid estimate. -
finalLL: Numeric. Value of log-likelihood at betaStop. -
iterations: Numeric. Number of iterations used in MLE search. -
functionEvals: Numeric. Number of function evaluations used in MLE search. (This excludes function evaluations used by any finite-difference calculations for the gradient and/or the vcHessian.) -
gradient: Vector. The gradient of the negative log-likelihood, evaluated at betaStop. -
scaleVec: Vector. The scaling vector at the conclusion of the MLE search (see scalingMethod in bgw_settings). -
estimate: Vector. Parameter vector at the conclusion of the search (the same as betaStop). It is always returned; it is the MLE only if the search converged successfully (code 3, 4, 5 or 6; see code and message). -
maximum: Numeric. Log-likelihood value at estimate (the same as finalLL). It is always returned; it is the maximum of the log-likelihood only if the search converged successfully. -
hessianMethodAttemptedBGW: String. Requested method for computing vcHessian (from bgw_settings). -
hessianMethodUsedBGW: String. Method actually used for computing vcHessian. If none was requested, this is "None"; if the matrix was singular, it is "N/A - BHHH Hessian is singular". -
vcHessianRecipCondBGW: Numeric. Estimated upper bound on the reciprocal of the Euclidean condition number of vcHessian (if available); 0 corresponds to a singular matrix. Set to -1 if unavailable. For a model object at betaStart (see userRequestModelObject), this is the exact reciprocal condition number. -
vcHessianConditionNumber: Numeric. Deprecated; the same value as vcHessianRecipCondBGW (despite the name, a reciprocal). It will be removed in a future version. -
varcovBGW: Matrix. p-by-p matrix containing estimate of the variance-covariance matrix (if requested and available). -
vcVecBGW: Vector. Lower triangle of variance-covariance matrix stored in vector form (row-major order, if requested and available). -
seBGW: Vector. Estimated standard errors for parameter estimates (if requested and available). -
tstatBGW: Vector. Estimated t-statistics (versus 0, if requested and available). indLevelGradients: Matrix. n-by-p matrix whose row i is d log(R_i)/d beta, computed at the final value of beta (if requested via userRequestIndLevelGradients).