# Sampling error for pig ration weight gain via bootstrap


Find this notebook on the web at
<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/how_big_sample.html#nte-sampling_error_bootstrap">Note <span>30.2</span></a>.

Use the bootstrap (resampling with replacement) procedure to estimate
how much error to expect when taking samples of different sizes.

```{r}
sampsize <- 12

gains <- c(31, 34, 29, 26, 32, 35, 38, 34, 32, 31, 30, 29)

n_trials <- 10000
results <- numeric(n_trials)

for (i in 1:n_trials) {
    fake_gains <- sample(gains, size=sampsize, replace=TRUE)
    results[i] <- mean(fake_gains)
}

title <- paste('Distribution of mean of', sampsize,
               'weights from', n_trials, 'bootstrap samples')
hist(results, breaks=25,
     main=title,
     xlab='Mean weight')

# Find values such that to 5%, 95% of values are below given value.
# (These are the 5% and 95% percentile values).
hi_lo_range <- quantile(results, c(0.05, 0.95))
rounded =  round(hi_lo_range, 2)

message('5% and 95% percentiles of bootstrap means: ',
        rounded[1], ' ', rounded[2])
```
