bootstrapify() creates a bootstrapped tibble with virtual groups.

bootstrapify(data, times, ..., key = ".bootstrap")

Arguments

data

A tbl.

times

A single integer specifying the number of resamples. If the tibble is grouped, this is the number of resamples per group.

...

Not used.

key

A single character specifying the name of the virtual group that is added.

Value

A resampled_df with an extra group specified by the key.

Details

The following functions have special / interesting behavior when used with a resampled_df:

See also

collect.resampled_df()

Other virtual samplers: samplify

Examples

library(dplyr)
#> #> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’: #> #> filter, lag
#> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union
library(broom) bootstrapify(iris, 5)
#> # A tibble: 150 x 5 #> # Groups: .bootstrap [5] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # … with 140 more rows
iris %>% bootstrapify(5) %>% summarise(per_strap_mean = mean(Petal.Width))
#> # A tibble: 5 x 2 #> .bootstrap per_strap_mean #> <int> <dbl> #> 1 1 1.18 #> 2 2 1.25 #> 3 3 1.20 #> 4 4 1.16 #> 5 5 1.29
iris %>% group_by(Species) %>% bootstrapify(5) %>% summarise(per_strap_species_mean = mean(Petal.Width))
#> # A tibble: 15 x 3 #> # Groups: Species [3] #> Species .bootstrap per_strap_species_mean #> <fct> <int> <dbl> #> 1 setosa 1 0.246 #> 2 setosa 2 0.274 #> 3 setosa 3 0.224 #> 4 setosa 4 0.242 #> 5 setosa 5 0.232 #> 6 versicolor 1 1.35 #> 7 versicolor 2 1.31 #> 8 versicolor 3 1.32 #> 9 versicolor 4 1.31 #> 10 versicolor 5 1.35 #> 11 virginica 1 2.06 #> 12 virginica 2 2.03 #> 13 virginica 3 2.05 #> 14 virginica 4 2.11 #> 15 virginica 5 2.07
iris %>% bootstrapify(5) %>% do(tidy(lm(Sepal.Width ~ Sepal.Length + Species, data = .)))
#> # A tibble: 20 x 6 #> # Groups: .bootstrap [5] #> .bootstrap term estimate std.error statistic p.value #> <int> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 1 (Intercept) 1.69 0.229 7.38 1.12e-11 #> 2 1 Sepal.Length 0.346 0.0444 7.79 1.12e-12 #> 3 1 Speciesversicolor -0.961 0.0693 -13.9 1.93e-28 #> 4 1 Speciesvirginica -1.04 0.0893 -11.7 1.13e-22 #> 5 2 (Intercept) 1.94 0.223 8.69 6.68e-15 #> 6 2 Sepal.Length 0.302 0.0428 7.05 6.64e-11 #> 7 2 Speciesversicolor -0.952 0.0643 -14.8 7.54e-31 #> 8 2 Speciesvirginica -0.898 0.0807 -11.1 3.34e-21 #> 9 3 (Intercept) 1.51 0.260 5.81 3.72e- 8 #> 10 3 Sepal.Length 0.394 0.0503 7.84 8.82e-13 #> 11 3 Speciesversicolor -1.07 0.0716 -15.0 2.07e-31 #> 12 3 Speciesvirginica -1.13 0.103 -11.0 9.46e-21 #> 13 4 (Intercept) 1.58 0.198 7.99 3.77e-13 #> 14 4 Sepal.Length 0.368 0.0392 9.39 1.10e-16 #> 15 4 Speciesversicolor -1.00 0.0660 -15.2 7.40e-32 #> 16 4 Speciesvirginica -0.983 0.0825 -11.9 2.79e-23 #> 17 5 (Intercept) 1.77 0.239 7.43 8.54e-12 #> 18 5 Sepal.Length 0.340 0.0474 7.17 3.41e-11 #> 19 5 Speciesversicolor -0.971 0.0722 -13.5 2.33e-27 #> 20 5 Speciesvirginica -1.10 0.0990 -11.1 4.62e-21
# Alternatively, use the newer group_modify() iris %>% bootstrapify(5) %>% group_modify(~tidy(lm(Sepal.Width ~ Sepal.Length + Species, data = .x)))
#> # A tibble: 20 x 6 #> # Groups: .bootstrap [5] #> .bootstrap term estimate std.error statistic p.value #> <int> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 1 (Intercept) 1.93 0.271 7.14 4.09e-11 #> 2 1 Sepal.Length 0.297 0.0533 5.57 1.20e- 7 #> 3 1 Speciesversicolor -0.931 0.0859 -10.8 1.83e-20 #> 4 1 Speciesvirginica -0.937 0.101 -9.32 1.71e-16 #> 5 2 (Intercept) 1.73 0.224 7.70 1.85e-12 #> 6 2 Sepal.Length 0.353 0.0437 8.08 2.18e-13 #> 7 2 Speciesversicolor -1.06 0.0691 -15.3 3.17e-32 #> 8 2 Speciesvirginica -1.18 0.0871 -13.5 1.67e-27 #> 9 3 (Intercept) 1.95 0.225 8.66 7.85e-15 #> 10 3 Sepal.Length 0.299 0.0436 6.87 1.72e-10 #> 11 3 Speciesversicolor -0.885 0.0686 -12.9 7.05e-26 #> 12 3 Speciesvirginica -0.970 0.0873 -11.1 3.41e-21 #> 13 4 (Intercept) 1.58 0.243 6.52 1.09e- 9 #> 14 4 Sepal.Length 0.359 0.0484 7.43 8.30e-12 #> 15 4 Speciesversicolor -0.960 0.0787 -12.2 4.69e-24 #> 16 4 Speciesvirginica -0.905 0.0929 -9.74 1.41e-17 #> 17 5 (Intercept) 1.43 0.238 6.00 1.49e- 8 #> 18 5 Sepal.Length 0.401 0.0468 8.56 1.47e-14 #> 19 5 Speciesversicolor -1.02 0.0692 -14.7 1.18e-30 #> 20 5 Speciesvirginica -1.07 0.0872 -12.3 2.70e-24
# Alter the name of the group with `key` # Materialize them with collect() straps <- bootstrapify(iris, 5, key = ".straps") collect(straps)
#> # A tibble: 750 x 6 #> # Groups: .straps [5] #> .straps Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <int> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 1 7 3.2 4.7 1.4 versicolor #> 2 1 5.1 2.5 3 1.1 versicolor #> 3 1 5.5 2.5 4 1.3 versicolor #> 4 1 7.4 2.8 6.1 1.9 virginica #> 5 1 6 2.2 4 1 versicolor #> 6 1 4.7 3.2 1.3 0.2 setosa #> 7 1 6.9 3.2 5.7 2.3 virginica #> 8 1 6.3 3.4 5.6 2.4 virginica #> 9 1 4.9 3.1 1.5 0.2 setosa #> 10 1 4.6 3.2 1.4 0.2 setosa #> # … with 740 more rows