generate | R Documentation |
Generation creates a simulated distribution from specify()
.
In the context of confidence intervals, this is a bootstrap distribution
based on the result of specify()
. In the context of hypothesis testing,
this is a null distribution based on the result of specify()
and
hypothesize().
Learn more in vignette("infer")
.
generate(x, reps = 1, type = NULL, variables = !!response_expr(x), ...)
x |
A data frame that can be coerced into a tibble. |
reps |
The number of resamples to generate. |
type |
The method used to generate resamples of the observed
data reflecting the null hypothesis. Currently one of
|
variables |
If |
... |
Currently ignored. |
A tibble containing reps
generated datasets, indicated by the
replicate
column.
The type
argument determines the method used to create the null
distribution.
bootstrap
: A bootstrap sample will be drawn for each replicate,
where a sample of size equal to the input sample size is drawn (with
replacement) from the input sample data.
permute
: For each replicate, each input value will be randomly
reassigned (without replacement) to a new output value in the sample.
draw
: A value will be sampled from a theoretical distribution
with parameter p
specified in hypothesize()
for each replicate. This
option is currently only applicable for testing on one proportion. This
generation type was previously called "simulate"
, which has been
superseded.
When using the infer package for research, or in other cases when exact
reproducibility is a priority, be sure the set the seed for R’s random
number generator. infer will respect the random seed specified in the
set.seed()
function, returning the same result when generate()
ing
data given an identical seed. For instance, we can calculate the
difference in mean age
by college
degree status using the gss
dataset from 10 versions of the gss
resampled with permutation using
the following code.
set.seed(1) gss %>% specify(age ~ college) %>% hypothesize(null = "independence") %>% generate(reps = 5, type = "permute") %>% calculate("diff in means", order = c("degree", "no degree"))
## Response: age (numeric) ## Explanatory: college (factor) ## Null Hypothesis: independence ## # A tibble: 5 x 2 ## replicate stat ## <int> <dbl> ## 1 1 -0.531 ## 2 2 -2.35 ## 3 3 0.764 ## 4 4 0.280 ## 5 5 0.350
Setting the seed to the same value again and rerunning the same code will produce the same result.
# set the seed set.seed(1) gss %>% specify(age ~ college) %>% hypothesize(null = "independence") %>% generate(reps = 5, type = "permute") %>% calculate("diff in means", order = c("degree", "no degree"))
## Response: age (numeric) ## Explanatory: college (factor) ## Null Hypothesis: independence ## # A tibble: 5 x 2 ## replicate stat ## <int> <dbl> ## 1 1 -0.531 ## 2 2 -2.35 ## 3 3 0.764 ## 4 4 0.280 ## 5 5 0.350
Please keep this in mind when writing infer code that utilizes
resampling with generate()
.
Other core functions:
calculate()
,
hypothesize()
,
specify()
# generate a null distribution by taking 200 bootstrap samples
gss %>%
specify(response = hours) %>%
hypothesize(null = "point", mu = 40) %>%
generate(reps = 200, type = "bootstrap")
# generate a null distribution for the independence of
# two variables by permuting their values 200 times
gss %>%
specify(partyid ~ age) %>%
hypothesize(null = "independence") %>%
generate(reps = 200, type = "permute")
# generate a null distribution via sampling from a
# binomial distribution 200 times
gss %>%
specify(response = sex, success = "female") %>%
hypothesize(null = "point", p = .5) %>%
generate(reps = 200, type = "draw") %>%
calculate(stat = "z")
# more in-depth explanation of how to use the infer package
## Not run:
vignette("infer")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.