Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@statwonk
Last active February 22, 2020 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statwonk/40e96dff95700e0e43925bc1de1bcfc4 to your computer and use it in GitHub Desktop.
Save statwonk/40e96dff95700e0e43925bc1de1bcfc4 to your computer and use it in GitHub Desktop.
The essence of bootstrapping: resampling with replacement to estimate a sampling distribution and its statistics of interest.
library(tidyverse)
library(gganimate)
# sample of 100 data points
tibble(x = rexp(1e2)) -> d
# 20 bootstrap samples, you likely want several orders of magnitude more.
tibble(boot_samples = seq_len(2e1)) %>%
mutate(data = map(boot_samples, ~ d %>% sample_frac(1, replace = TRUE))) %>%
unnest(cols = c(data)) %>%
ggplot(aes(x = x, group = boot_samples)) +
stat_ecdf(size = 0.25) +
xlab("Time to event") +
ggtitle("Bootstrap samples from one sample of an\n
Exponential distribution r.v. with rate parameter 1") +
transition_reveal(boot_samples) -> anim
animate(anim, fps = 10, duration = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment