File: pointwise.R

package info (click to toggle)
r-cran-loo 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,836 kB
  • sloc: sh: 15; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#' Convenience function for extracting pointwise estimates
#'
#' @export
#' @param x A `loo` object, for example one returned by [loo()],
#'   [loo_subsample()], [loo_approximate_posterior()], [loo_moment_match()], etc.
#' @param estimate Which pointwise estimate to return. By default all are
#'   returned. The objects returned by the different functions ([loo()],
#'   [loo_subsample()], etc.) have slightly different estimates available.
#'   Typically at a minimum the estimates `elpd_loo`, `looic`, `mcse_elpd_loo`,
#'   `p_loo`, and `influence_pareto_k` will be available, but there may be
#'   others.
#' @param ... Currently ignored.
#' @return A vector of length equal to the number of observations.
#'
#' @examples
#' x <- loo(example_loglik_array())
#' pointwise(x, "elpd_loo")
#'
pointwise <- function(x, estimate, ...) {
  UseMethod("pointwise")
}

#' @rdname pointwise
#' @export
pointwise.loo <- function(x, estimate, ...) {
  stopifnot(is.character(estimate), length(estimate) == 1)
  pw <- x$pointwise
  if (is.null(pw)) {
    stop("No pointwise estimates found.", call. = FALSE)
  }
  estimates <- colnames(pw)
  if (!(estimate %in% estimates)) {
    stop(
      "'", estimate, "' not found.",
      " Available estimates are: \n",
      paste(shQuote(estimates), collapse=", ")
    )
  }
  pw[, estimate]
}