Search⌘ K

Comparison with Confidence Intervals

Explore how to build and interpret confidence intervals alongside hypothesis tests using the infer package in R. This lesson teaches you to create bootstrap distributions, visualize intervals, and understand the relationship between confidence intervals and hypothesis testing through a unified framework.

We'll cover the following...

One of the great things about the infer package is that we can jump seamlessly between conducting hypothesis tests and constructing confidence intervals with minimal changes! Recall the code from the previous section that creates the null distribution, which in turn is needed to compute the pp-value:

R
null_distribution <- promotions %>%
specify(formula = decision ~ gender, success = "promoted") %>% hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "diff in props", order = c("male", "female"))

To create the corresponding bootstrap distribution needed to construct a 95% confidence interval 𝑝𝑚𝑝𝑓𝑝_𝑚 − 𝑝_𝑓, we only need to make two changes. First, we remove the hypothesize() step because we’re no longer assuming a null hypothesis H0H_0 is true. We can do this by deleting or commenting out the hypothesize() line of code. Second, we switch the type of resampling in the generate() step to bootstrap instead of permute.

R
bootstrap_distribution <- promotions %>%
specify(formula = decision ~ gender, success = "promoted") %>%
# Change 1 - Remove hypothesize():
# hypothesize(null = "independence") %>%
# Change 2 - Switch type from "permute" to "bootstrap":
generate(reps = 1000, type = "bootstrap") %>%
calculate(stat = "diff in props", order = c("male", "female"))

Using this bootstrap_distribution, let’s first compute the percentile-based confidence intervals:

R
percentile_ci <- bootstrap_distribution %>% get_confidence_interval(level = 0.95, type = "percentile")
percentile_ci

Using our shorthand interpretation for 95% confidence intervals, we’re 95% confident that the true difference in population proportions 𝑝𝑚𝑝𝑓𝑝_𝑚 − 𝑝_𝑓 ...