# A classic bootstrap example


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

Draw bootstrap samples from mouse survival data to compare actual mean
difference to those observed in the null- (bootstrap) world.

```{r}
# Treatment group.
treat <- c(94, 38, 23, 197, 99, 16, 141)
# control group
control <- c(52, 10, 40, 104, 51, 27, 146, 30, 46)

# Observed difference in real world.
actual_diff <- mean(treat) - mean(control)

# Set the number of trials.
n_trials <- 10000

# An empty array to store the trials.
results <- numeric(n_trials)

# U is our universe (Step 2 above)
u <- c(treat, control)

# step 5 above.
for (i in 1:n_trials) {
    # Step 3 above.
    fake_treat <- sample(u, size=7, replace=TRUE)
    # Step 3.
    fake_control <- sample(u, size=9, replace=TRUE)
    # Step 4.
    mt <- mean(fake_treat)
    # Step 4.
    mc <- mean(fake_control)
    # Step 4.
    diff <- mt - mc
    # Step 4.
    results[i] <- diff
}

# Step 6
hist(results, breaks=25,
    main='Bootstrap distribution of mean differences in survival',
    xlab='Bootstrap mean treatment - mean control')

# Step 6
k <- sum(results >= actual_diff)
kk <- k / n_trials
message('Proportion bootstrap mean difference >= actual difference: ', kk)
```
