R/sampling.R
Bootstrap resampling with replacement from a distribution of a random variable stored in an rvtable.
sample_rvtable(x, resample = FALSE, n = 10000, interp = TRUE, n.interp = 1e+05, decimals = NULL, density.args)
x | an |
---|---|
resample | if |
n | bootstrap sample size. |
interp | linearly interpolate between observed values of Val before sampling if Val is continuous. Ignored for discrete random variables. |
n.interp | length of sequence of interpolated sampling points if. Ignored for discrete random variables or |
decimals | number of decimal places for rounding samples. Ignored for discrete random variables. |
density.args | optional arguments passed to |
an rvtable
object where the Val column represents samples and the Prob column is dropped.
Resample an rvtable yielding a new rvtable.
This is used to convert from a distribution-based rvtable, which has Val and Prob columns representing a distribution to one with only a Val column representing samples from a distribution.
This is often used preceding plotting data in an rvtable so that samples may be passed to plot code rather than a representation of a distribution using values and associated probabilities.
This function can also take a sample-based rvtable, in which case it will resample it if resample=TRUE
.
If is sample-based rvtable is passed and the random variable is continuous, the generation of a new continuous density prior to resampling can be controlled via density.args
.
All rvtable objects are either distribution-based or sample-based.
#> # A tibble: 10 x 1 #> x #> <dbl> #> 1 0.0677 #> 2 0.887 #> 3 -0.0667 #> 4 0.272 #> 5 -1.13 #> 6 0.383 #> 7 1.05 #> 8 1.27 #> 9 0.607 #> 10 -1.26x <- rvtable(sample(1:100, 50), discrete=TRUE) y <- sample_rvtable(x, n=10) sample_rvtable(y, n=8, resample=TRUE)#> # A tibble: 8 x 1 #> x #> <dbl> #> 1 70.0 #> 2 6.00 #> 3 70.0 #> 4 70.0 #> 5 48.0 #> 6 81.0 #> 7 81.0 #> 8 70.0