b_bootstrap: b_bootstrap

View source: R/b_bootstrap.R

b_bootstrapR Documentation

b_bootstrap

Description

Performs a Bayesian bootstrap and returns a sample of size n1 representing the posterior distribution of the statistic. Returns a vector if the statistic is one-dimensional (like for mean(...)) or a data.frame if the statistic is multi-dimensional (like for the coefficients of lm).

Usage

b_bootstrap(
  data,
  statistic,
  n1 = 1000,
  n2 = 1000,
  use_weights = FALSE,
  weight_arg = NULL,
  ...
)

Arguments

data

The data as either a vector, matrix or data.frame.

statistic

A function that accepts data as its first argument and if use_weights is TRUE the weights as its second argument. Function should return a numeric vector.

n1

The size of the bootstrap sample (default = 1000).

n2

The sample size used to calculate the statistic each bootstrap draw (default = 1000).

use_weights

Whether the statistic function accepts a weight argument or should be calculated using resampled data (default = FALSE).

weight_arg

If the statistic function includes a named argument for the weights this could be specified here (default = NULL).

...

Further arguments passed on to the statistic function.

Value

A data frame containing bootstrap samples.

Author(s)

Rasmus Baath

References

https://www.sumsar.net/blog/2015/07/easy-bayesian-bootstrap-in-r/

Rubin, D. B. (1981). The Bayesian Bootstrap. The annals of statistics, 9(1), 130-134.

Examples


# linear function of seqence vs. response
lm_statistic <- function(data) {
  lm(sequence ~ response, data)$coef
}

# load data
data <- adaptation_level_small

# bootstrap
data_bootstrap <- b_bootstrap(data, lm_statistic, n1 = 1000, n2 = 1000)


bayes4psy documentation built on Sept. 29, 2023, 5:08 p.m.