Skip to content

Commit e5086f7

Browse files
authored
Merge pull request #1060 from stan-dev/doc-clarifications
Doc clarifications
2 parents ce58981 + 75bc1c7 commit e5086f7

File tree

8 files changed

+71
-28
lines changed

8 files changed

+71
-28
lines changed

R/fit.R

Lines changed: 26 additions & 12 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)) {
@@ -1806,6 +1809,11 @@ CmdStanMCMC$set("public", name = "num_chains", value = num_chains)
18061809
#'
18071810
#' @description A `CmdStanMLE` object is the fitted model object returned by the
18081811
#' [`$optimize()`][model-method-optimize] method of a [`CmdStanModel`] object.
1812+
#' This object will either contain a penalized maximum likelihood estimate
1813+
#' (MLE) or a maximum a posteriori estimate (MAP), depending on the value of
1814+
#' the `jacobian` argument when the model is fit (and whether the model has
1815+
#' constrained parameters). See [`$optimize()`][model-method-optimize] and the
1816+
#' CmdStan User's Guide for more details.
18091817
#'
18101818
#' @section Methods: `CmdStanMLE` objects have the following associated methods,
18111819
#' all of which have their own (linked) documentation pages.
@@ -1875,17 +1883,22 @@ CmdStanMLE <- R6::R6Class(
18751883
)
18761884
)
18771885

1878-
#' Extract (penalized) maximum likelihood estimate after optimization
1886+
#' Extract point estimate after optimization
18791887
#'
18801888
#' @name fit-method-mle
18811889
#' @aliases mle
1882-
#' @description The `$mle()` method is only available for [`CmdStanMLE`] objects.
1883-
#' It returns the penalized maximum likelihood estimate (posterior mode) as a
1884-
#' numeric vector with one element per variable. The returned vector does *not*
1885-
#' include `lp__`, the total log probability (`target`) accumulated in the
1886-
#' model block of the Stan program, which is available via the
1887-
#' [`$lp()`][fit-method-lp] method and also included in the
1888-
#' [`$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+
#' For models with constrained parameters that are fit with `jacobian=TRUE`,
1898+
#' the `$mle()` method actually returns the maximum a posteriori (MAP)
1899+
#' estimate (posterior mode) rather than the MLE. See
1900+
#' [`$optimize()`][model-method-optimize] and the CmdStan User's Guide for
1901+
#' more details.
18891902
#'
18901903
#' @param variables (character vector) The variables (parameters, transformed
18911904
#' parameters, and generated quantities) to include. If NULL (the default)
@@ -1910,6 +1923,7 @@ mle <- function(variables = NULL) {
19101923
}
19111924
CmdStanMLE$set("public", name = "mle", value = mle)
19121925

1926+
19131927
# CmdStanLaplace ---------------------------------------------------------------
19141928
#' CmdStanLaplace objects
19151929
#'

R/model.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,14 @@ CmdStanModel$set("public", name = "sample_mpi", value = sample_mpi)
14441444
#' @family CmdStanModel methods
14451445
#'
14461446
#' @description The `$optimize()` method of a [`CmdStanModel`] object runs
1447-
#' Stan's optimizer to obtain a (penalized) maximum likelihood estimate or a
1448-
#' maximum a posteriori estimate (if `jacobian=TRUE`). See the
1447+
#' Stan's optimizer to obtain a (penalized) maximum likelihood estimate (MLE)
1448+
#' or a maximum a posteriori estimate (MAP), depending on the value of the
1449+
#' `jacobian` argument. For models with constrained parameters, when the
1450+
#' Jacobian adjustment is *not* applied, the point estimate corresponds to a
1451+
#' penalized MLE, and when the Jacobian adjustment is applied the point
1452+
#' estimate corresponds to the MAP (posterior mode) of the model we would fit
1453+
#' if we were instead doing MCMC sampling. The Jacobian adjustment has no
1454+
#' affect if the model has only unconstrained parameters. See the
14491455
#' [CmdStan User's Guide](https://mc-stan.org/docs/cmdstan-guide/index.html)
14501456
#' for more details.
14511457
#'
@@ -1455,6 +1461,7 @@ CmdStanModel$set("public", name = "sample_mpi", value = sample_mpi)
14551461
#' default arguments. The default values can also be obtained by checking the
14561462
#' metadata of an example model, e.g.,
14571463
#' `cmdstanr_example(method="optimize")$metadata()`.
1464+
#'
14581465
#' @template model-common-args
14591466
#' @param threads (positive integer) If the model was
14601467
#' [compiled][model-method-compile] with threading support, the number of

cmdstanr.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: b2cb035a-82d0-45fa-b984-ec75db1e1feb
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

man/CmdStanMLE.Rd

Lines changed: 5 additions & 0 deletions
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: 13 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-optimize.Rd

Lines changed: 8 additions & 2 deletions
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)