Skip to content

Commit f01d2af

Browse files
committed
Clarification in MLE and LOO doc
closes #1052 closes #1051
1 parent 3ae9080 commit f01d2af

6 files changed

Lines changed: 60 additions & 27 deletions

File tree

R/fit.R

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,10 +1497,10 @@ CmdStanMCMC <- R6::R6Class(
14971497
#' and the \pkg{loo} package [vignettes](https://mc-stan.org/loo/articles/)
14981498
#' for details.
14991499
#'
1500-
#' @param variables (character vector) The name(s) of the variable(s) in the
1501-
#' Stan program containing the pointwise log-likelihood. The default is to
1502-
#' look for `"log_lik"`. This argument is passed to the
1503-
#' [`$draws()`][fit-method-draws] method.
1500+
#' @param variables (string) The name of the variable in the Stan program
1501+
#' containing the pointwise log-likelihood. The default is to look for
1502+
#' `"log_lik"`. This argument is passed to the [`$draws()`][fit-method-draws]
1503+
#' method.
15041504
#' @param r_eff (multiple options) How to handle the `r_eff` argument for `loo()`:
15051505
#' * `TRUE` (the default) will automatically call [loo::relative_eff.array()]
15061506
#' to compute the `r_eff` argument to pass to [loo::loo.array()].
@@ -1539,6 +1539,9 @@ CmdStanMCMC <- R6::R6Class(
15391539
#'
15401540
loo <- function(variables = "log_lik", r_eff = TRUE, moment_match = FALSE, ...) {
15411541
require_suggested_package("loo")
1542+
if (length(variables) != 1) {
1543+
stop("Only a single variable name is allowed for the 'variables' argument.", call. = FALSE)
1544+
}
15421545
LLarray <- self$draws(variables, format = "draws_array")
15431546
if (is.logical(r_eff)) {
15441547
if (isTRUE(r_eff)) {
@@ -1805,6 +1808,12 @@ CmdStanMCMC$set("public", name = "num_chains", value = num_chains)
18051808
#'
18061809
#' @description A `CmdStanMLE` object is the fitted model object returned by the
18071810
#' [`$optimize()`][model-method-optimize] method of a [`CmdStanModel`] object.
1811+
#' The name "MLE" may be somewhat misleading because the `$optimize()` method
1812+
#' can compute either a penalized maximum likelihood estimate or a maximum a
1813+
#' posteriori estimate, depending on the value of the `jacobian` argument when
1814+
#' the model is fit. Additionally, for models without constrained parameters,
1815+
#' the penalized MLE and the posterior mode are equivalent, as the Jacobian
1816+
#' adjustment has no effect.
18081817
#'
18091818
#' @section Methods: `CmdStanMLE` objects have the following associated methods,
18101819
#' all of which have their own (linked) documentation pages.
@@ -1814,7 +1823,7 @@ CmdStanMCMC$set("public", name = "num_chains", value = num_chains)
18141823
#' |**Method**|**Description**|
18151824
#' |:----------|:---------------|
18161825
#' [`draws()`][fit-method-draws] | Return the point estimate as a 1-row [`draws_matrix`][posterior::draws_matrix]. |
1817-
#' [`$mle()`][fit-method-mle] | Return the point estimate as a numeric vector. |
1826+
#' [`$mode()`][fit-method-mode] | Return the point estimate as a numeric vector. |
18181827
#' [`$lp()`][fit-method-lp] | Return the total log probability density (`target`). |
18191828
#' [`$init()`][fit-method-init] | Return user-specified initial values. |
18201829
#' [`$metadata()`][fit-method-metadata] | Return a list of metadata gathered from the CmdStan CSV files. |
@@ -1874,17 +1883,23 @@ CmdStanMLE <- R6::R6Class(
18741883
)
18751884
)
18761885

1877-
#' Extract (penalized) maximum likelihood estimate after optimization
1886+
#' Extract point estimate after optimization
18781887
#'
18791888
#' @name fit-method-mle
18801889
#' @aliases mle
1881-
#' @description The `$mle()` method is only available for [`CmdStanMLE`] objects.
1882-
#' It returns the penalized maximum likelihood estimate (posterior mode) as a
1883-
#' numeric vector with one element per variable. The returned vector does *not*
1884-
#' include `lp__`, the total log probability (`target`) accumulated in the
1885-
#' model block of the Stan program, which is available via the
1886-
#' [`$lp()`][fit-method-lp] method and also included in the
1887-
#' [`$draws()`][fit-method-draws] method.
1890+
#' @description The `$mle()` method is only available for [`CmdStanMLE`]
1891+
#' objects. It returns the point estimate as a numeric vector with one element
1892+
#' per variable. The returned vector does *not* include `lp__`, the total log
1893+
#' probability (`target`) accumulated in the model block of the Stan program,
1894+
#' which is available via the [`$lp()`][fit-method-lp] method and also
1895+
#' included in the [`$draws()`][fit-method-draws] method.
1896+
#'
1897+
#' The name `mle` may be somewhat misleading because the `$optimize()` method
1898+
#' can compute either a penalized maximum likelihood estimate or a maximum a
1899+
#' posteriori estimate, depending on the value of the `jacobian` argument when
1900+
#' the model is fit. Additionally, for models without constrained parameters,
1901+
#' the penalized MLE and the posterior mode are equivalent, as the Jacobian
1902+
#' adjustment has no effect.
18881903
#'
18891904
#' @param variables (character vector) The variables (parameters, transformed
18901905
#' parameters, and generated quantities) to include. If NULL (the default)
@@ -1909,6 +1924,7 @@ mle <- function(variables = NULL) {
19091924
}
19101925
CmdStanMLE$set("public", name = "mle", value = mle)
19111926

1927+
19121928
# CmdStanLaplace ---------------------------------------------------------------
19131929
#' CmdStanLaplace objects
19141930
#'

man/CmdStanMLE.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-method-loo.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-method-mle.Rd

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-method-save_object.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-fit-mcmc.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ test_that("loo method works if log_lik is available", {
276276
fit_bernoulli <- testing_fit("bernoulli_log_lik")
277277
expect_s3_class(suppressWarnings(fit_bernoulli$loo(cores = 1, save_psis = TRUE)), "loo")
278278
expect_s3_class(suppressWarnings(fit_bernoulli$loo(r_eff = FALSE)), "loo")
279+
280+
expect_error(
281+
fit_bernoulli$loo(variables = c("log_lik", "beta")),
282+
"Only a single variable name is allowed"
283+
)
279284
})
280285

281286
test_that("loo method works with moment-matching", {

0 commit comments

Comments
 (0)