Course Outline

list High School / Advanced Statistics and Data Science I (ABC)

Book
  • High School / Advanced Statistics and Data Science I (ABC)
  • High School / Statistics and Data Science I (AB)
  • High School / Statistics and Data Science II (XCD)
  • High School / Algebra + Data Science (G)
  • College / Introductory Statistics with R (ABC)
  • College / Advanced Statistics with R (ABCD)
  • College / Accelerated Statistics with R (XCD)
  • CKHub: Jupyter made easy

12.5 Using the Bootstrapped Sampling Distribution to Find the Confidence Interval

Using resample() to Bootstrap a Sampling Distribution

Now that we have reviewed how the resample() function works, let’s use it to create a sampling distribution of 1000 \(b_1\)s.

Modify the code in the window below to create a sampling distribution of 1000 \(b_1\)s, each based on resampled data, and save it into a new data frame called sdob1_boot. Add some code to produce a histogram of the sampling distribution.

require(coursekata) # modify this to make 1000 bootstrapped b1s sdob1_boot <- do( ) * b1(Tip ~ Condition, data = resample(TipExperiment)) # visualize sdob1_boot with a histogram # modify this to make 1000 bootstrapped b1s sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) # visualize sdob1_boot with a histogram gf_histogram(~b1, data = sdob1_boot) ex() %>% { check_function(., "do") %>% check_arg("object") %>% check_equal() check_or(., check_function(., "gf_histogram") %>% { check_arg(., "object") %>% check_equal() check_arg(., "data") %>% check_equal(eval = FALSE) }, override_solution(., '{ sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) gf_histogram(sdob1_boot, ~b1) }') %>% check_function("gf_histogram") %>% { check_arg(., "object") %>% check_equal(eval = FALSE) check_arg(., "gformula") %>% check_equal() }, override_solution(., '{ sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) gf_histogram(~sdob1_boot$b1) }') %>% check_function("gf_histogram") %>% check_arg("object") %>% check_equal(eval = FALSE) ) }

A histogram of the sampling distribution of b1. It is normally distributed, and centered around 5 or 6. The distribution ranges along the x-axis from about negative 3 to about 16.

Use favstats() in the code window below to see what the average of the \(b_1\)s is in sdob1_boot.

require(coursekata) # we have created the sampling distribution for you sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) # run favstats to check out the mean of the sampling distribution # we have created the sampling distribution for you sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) # run favstats to check out the mean of the sampling distribution favstats(~b1, data = sdob1_boot) ex() %>% check_function("favstats") %>% check_result() %>% check_equal()
       min       Q1   median       Q3      max     mean       sd    n missing
 -3.219048 3.772727 5.921166 8.480083 15.96154 6.110566 3.381418 1000       0

The mean is fairly close to $6.05, the estimate of \(\beta_1\) from the tipping study. Because the resampled sampling distribution is roughly centered at the sample \(b_1\), it provides us what we were looking for in order to calculate the 95% confidence interval for \(\beta_1\): a sampling distribution centered at the sample \(b_1\).

The same three-layered diagram of beta-sub-1, the sampling distribution of b1, and the sample b1 that appears earlier on the page; however, there are three histograms of potential sampling distributions. They are slightly overlapping, and the one in the center is shaded in red, and the two to the sides of it are blue. The red histogram in the center represents a possible DGP where the sampling distribution is centered at 6.05, and the sample b1 of 6.05 falls right in the center as well. The histogram on the left represents a possible DGP where beta-sub-1 equals negative 0.67, so the sampling distribution is also centered at negative 0.67. The sample b1 of 6.05 falls right on the line for the boundary of the upper tail for this distribution. The histogram on the right represents a possible DGP where beta-sub-1 equals 12.76, so the sampling distribution is also centered at 12.76. The sample b1 of 6.05 falls right on the line for the boundary of the lower tail for this distribution. In the top line, the beta-sub-1 of negative 0.67 is labeled as the Lower Bound, and the beta-sub-1 of 12.76 is labeled as the Upper Bound. We can also see that the boundary for the lower tail of the red sampling distribution in the center is also aligned with the center of the distribution on the left at negative 0.67, while the boundary for the upper tail of the red sampling distribution is also aligned with the center of the distribution on the right at 12.76.

Using the Bootstrapped Sampling Distribution to Find the Confidence Interval

We have now succeeded in creating a bootstrapped sampling distribution of 1000 \(b_1\)s centered at the sample \(b_1\) (roughly 6.05) using the resample() function. To find the lower and upper bounds of the confidence interval, we will use our sampling distribution of \(b_1\)s as a probability distribution, interpreting proportions of \(b_1\)s falling in a certain range as a probability that future \(b_1\)s would fall into the same range.

We want to find the cutoffs that separate the middle .95 of the resampled sampling distribution from the lower and upper .025 tails because these cutoffs will correspond perfectly with the lower and upper bound of the confidence interval.

To do this, we start by putting the 1000 \(b_1\)s in order. Then we can find the cutoffs that separate the top 25 and the bottom 25 \(b_1\)s from the middle 950 \(b_1\)s.

We can visualize this task by shading in the middle .95 differently from the tails (.025 in each tail) as shown in the histogram below. (The histogram will show all the values of resampled \(b_1\) in order from smallest to largest on the x-axis.)

gf_histogram(~b1, data = sdob1_boot, fill = ~middle(b1, .95), bins = 80)

A histogram of the sampling distribution of b1. It is normally distributed, and centered around 5 or 6. The distribution ranges along the x-axis from about negative 3 to about 16. The upper and lower tails of the distribution have been shaded in red. The red area in the lower tail ranges from about negative 3 to zero, and the red area in the upper tail ranges from about 13 to 16.

As illustrated below, the cutoff for the lowest .025 of \(b_1\)s is at the 26th \(b_1\). The cutoff for the highest .025 of \(b_1\)s is at the 975th \(b_1\). These two cutoffs correspond to the lower and upper bound of the confidence interval.

A horizontal rectangle is split into three parts. The middle is the largest section and shaded in blue, and labeled as 26-975 inside of it. Underneath this section it is labeled as the “middle 950 b1s.” The left section of the rectangle is shaded in red and labeled as 1-25 inside of it. Underneath this section it is labeled as the “lowest 25 b1s.” The right section to the rectangle is shaded in red and labeled as 976-1000 inside of it. Underneath this section it is labeled as the “highest 25 b1s.”

Here’s some code that will arrange the \(b_1\)s in order (from lowest to highest) and save the re-arranged data back into sdob1_boot.

sdob1_boot <- arrange(sdob1_boot, b1)

To identify the 26th b1 in the arranged data frame (26th from the lowest), we can use these brackets (e.g., [26]).

sdob1_boot$b1[26]

Use the code block below to print out both the 26th and 975th b1s.

require(coursekata) sdob1_boot <- do(1000) * b1(Tip ~ Condition, data = resample(TipExperiment)) sdob1_boot <- arrange(sdob1_boot, b1) # we’ve written code to print the 26th b1 sdob1_boot$b1[26] # write code to print the 975th b1 # we’ve written code to print the 26th b1 sdob1_boot$b1[26] # write code to print the 975th b1 sdob1_boot$b1[975] ex() %>% { check_output_expr(., "sdob1_boot$b1[26]") check_output_expr(., "sdob1_boot$b1[975]") }
[1] -0.02484472
[1] 13.3

Based on our bootstrapped sampling distribution of \(b_1\), the 95% confidence interval runs from around 0 to 13 (give or take). Your numbers will be slightly different from ours, of course, because they are generated randomly. Based on this analysis, we can be 95% confident that the true value of \(\beta_1\) in the DGP lies in this range.

Responses