Learn R Programming

posterior (version 1.6.0)

resample_draws: Resample draws objects

Description

Resample draws objects according to provided weights, for example weights obtained through importance sampling.

Usage

resample_draws(x, ...)

# S3 method for draws resample_draws(x, weights = NULL, method = "stratified", ndraws = NULL, ...)

# S3 method for rvar resample_draws(x, ...)

Value

A draws object of the same class as x.

Arguments

x

(draws) A draws object or another R object for which the method is defined.

...

Arguments passed to individual methods (if applicable).

weights

(numeric vector) A vector of positive weights of length ndraws(x). The weights will be internally normalized. If weights is not specified, an attempt will be made to extract any weights already stored in the draws object (via weight_draws()). If no weights are stored in the draws object, equal weight is supplied to each draw. How exactly the weights influence the resampling depends on the method argument.

method

(string) The resampling method to use:

  • "simple": simple random resampling with replacement

  • "simple_no_replace": simple random resampling without replacement

  • "stratified": stratified resampling with replacement

  • "deterministic": deterministic resampling with replacement

Currently, "stratified" is the default as it has comparably low variance and bias with respect to ideal resampling. The latter would sample perfectly proportional to the weights, but this is not possible in practice due to the finite number of draws available. For more details about resampling methods, see Kitagawa (1996).

ndraws

(positive integer) The number of draws to be returned. By default ndraws is set internally to the total number of draws in x if sensible.

Details

Upon usage of resample_draws(), chains will automatically be merged due to subsetting of individual draws (see subset_draws for details). Also, weights stored in the draws object will be removed in the process, as resampling invalidates existing weights.

References

Kitagawa, G., Monte Carlo Filter and Smoother for Non-Gaussian Nonlinear ' State Space Models, Journal of Computational and Graphical Statistics, 5(1):1-25, 1996.

See Also

resample_draws()

Examples

Run this code
x <- as_draws_df(example_draws())

# random weights for justr for demonstration
w <- runif(ndraws(x), 0, 10)

# use default stratified sampling
x_rs <- resample_draws(x, weights = w)
summarise_draws(x_rs, default_summary_measures())

# use simple random sampling
x_rs <- resample_draws(x, weights = w, method = "simple")
summarise_draws(x_rs, default_summary_measures())

Run the code above in your browser using DataLab