bootstrapper: Create a bootstrapper function useful for repeated...

Description Usage Arguments See Also Examples

Description

Create a bootstrapper function useful for repeated reproducible bootstrapping

Usage

1
2
3
bootstrapper(times, group = NULL, seed = NULL, key = ".draw",
  row = ".row", id = ".id", original_id = ".original_id",
  copies = ".copies")

Arguments

times

Number of independent bootstrap draws to perform.

group

An optional expression setting up the grouping to use for bootstrapping. If not provided, any grouping present in the original dataset will be used.

seed

Random seed to use.

key

Name (as character) of the column that will hold an integer running from 1 to times indicating the bootstrap replicate.

row

Name (as character) of the column that will hold an integer counting rows in the final bootstrapped dataset. Useful for animations with gganimate.

id

Name (as character) of the column that will hold an integer running from 1 to n for each bootstrap, where n is the number of observations in each group.

original_id

Name (as character) of the column that indicates the row from which the bootstrapped row originates.

copies

Name (as character) of the column that reports how often a specific original row has been oversampled.

See Also

sampler()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
bs <- bootstrapper(3)
bs(data.frame(letter = letters[1:4]))

library(ggplot2)
library(dplyr)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  geom_smooth(data = bootstrapper(5, Species), aes(group = .draw), se = FALSE) +
  facet_wrap(~Species)

# it is important to set grouping correctly for bootstrapping
set.seed(1234)

df <- data.frame(
  type = c(rep("A", 100), rep("B", 10), rep("C", 3)),
  y = rnorm(113)
)

# incorrect: bootstrapping ungrouped dataset leads to missing category C
ggplot(df, aes(type, y)) +
  geom_pointrange(data = bootstrapper(6, seed = 562), stat = "summary") +
  facet_wrap(~.draw)

# correct: bootstrapping within groups
ggplot(df, aes(type, y)) +
  geom_pointrange(data = bootstrapper(6, type, seed = 562), stat = "summary") +
  facet_wrap(~.draw)

# also correct: use grouped data frame
ggplot(group_by(df, type), aes(type, y)) +
  geom_pointrange(data = bootstrapper(6, seed = 562), stat = "summary") +
  facet_wrap(~.draw)

## Not run: 
library(gganimate)

set.seed(69527)
x <- rnorm(15)
data <- data.frame(
  x,
  y = x + 0.5*rnorm(15)
)

bs <- bootstrapper(9)

p <- ggplot(data, aes(x, y)) +
  geom_point(shape = 21, size = 6, fill = "white") +
  geom_text(label = "0", hjust = 0.5, vjust = 0.5, size = 10/.pt) +
  geom_point(data = bs, aes(group = .row), shape = 21, size = 6, fill = "blue") +
  geom_text(
    data = bs, aes(label = .copies, group = .row),
    hjust = 0.5, vjust = 0.5, size = 10/.pt, color = "white"
  ) +
  geom_smooth(data = bs, method = "lm", se = FALSE) +
  ggtitle("Bootstrap demonstration") +
  theme_bw()

p + facet_wrap(~.draw)
p + transition_states(.draw, 1, 1) +
  enter_fade() + exit_fade()

## End(Not run)

clauswilke/ungeviz documentation built on May 16, 2019, 3:10 p.m.