R/thin_draws.R

Defines functions thin_draws.rvar thin_draws.draws thin_draws

Documented in thin_draws thin_draws.draws thin_draws.rvar

#' Thin `draws` objects
#'
#' Thin [`draws`] objects to reduce their size and autocorrelation in the chains.
#'
#' @aliases thin
#' @template args-methods-x
#' @param thin (positive integer) The period for selecting draws.
#' @template args-methods-dots
#' @template return-draws
#'
#' @examples
#' x <- example_draws()
#' niterations(x)
#'
#' x <- thin_draws(x, thin = 5)
#' niterations(x)
#'
#' @export
thin_draws <- function(x, thin, ...) {
  UseMethod("thin_draws")
}

#' @rdname thin_draws
#' @export
thin_draws.draws <- function(x, thin, ...) {
  thin <- as_one_integer(thin)
  if (thin == 1L) {
    # no thinning requested
    return(x)
  }
  if (thin <= 0L) {
    stop_no_call("'thin' must be a positive integer.")
  }
  niterations <- niterations(x)
  if (thin > niterations ) {
    stop_no_call("'thin' must be smaller than the total number of iterations.")
  }
  iteration_ids <- seq(1, niterations, by = thin)
  subset_draws(x, iteration = iteration_ids)
}

#' @rdname thin_draws
#' @export
thin_draws.rvar <- function(x, thin, ...) {
  thin_draws(draws_rvars(x = x), thin, ...)$x
}

Try the posterior package in your browser

Any scripts or data that you put into this service are public.

posterior documentation built on Nov. 2, 2023, 5:56 p.m.