\(~\)
This lab covers confidence interval estimation using the two approaches described in our most recent lecture:
These methods can be applied to any of the descriptive statistics we’ve seen throughout the semester, including the coefficients of regression models.
## Data that will be used in examples (claims against the TSA for laptops)
library(dplyr)
tsa <- read.csv("https://remiller1450.github.io/data/tsa.csv")
tsa_laptops = tsa %>% filter(Item == "Computer - Laptop", Claim_Amount < 1e6)
\(~\)
We’ve already seen the functions needed to create confidence interval estimates for many common scenarios. As review, these functions are summarized in the table below:
| Scenario | Function | Notes |
|---|---|---|
| Single Proportion | prop.test() or binom.test() |
prop.test() is valid when $n n (1 - ) $ |
| Difference in Proportions | prop.test() |
Valid when single proportion conditions are met for each group |
| Single Mean | t.test() |
Valid when \(n \geq 30\) or Normally distributed data |
| Difference in Means | t.test() |
Valid when \(n_1 \text{ and } n_2 \geq 30\) or Normally distributed data |
| Correlation | cor.test() |
Allows either Pearson’s or Spearman’s correlation |
| Regression Coefficients | confint() |
The fitted regression model must be given as the function’s input |
You should note that each function expects a different type of input. Below are a few quick examples so you don’t need to go back to our previous labs.
## For prop.test() "x" is the numerator(s) and "n" is the denominator(s) of the involved proportions
numerator_count = sum(tsa_laptops$Status == "Denied")
denominator_count = length(tsa_laptops$Status)
prop.test(x = numerator_count, n = denominator_count, correct = FALSE)
## For t.test() "x" is either the variable (one-sample) or a formula of the form `outcome ~ explanatory` and "data" is the data set.
t.test(x = tsa_laptops$Claim_Amount)
## For cor.test() "x" and "y" are each of the two involved variables
cor.test(x = tsa_laptops$Claim_Amount, y = tsa_laptops$Close_Amount)
## For confint() "object" is the fitted regression model
fitted_model = lm(Close_Amount ~ Claim_Amount, data = tsa_laptops)
confint(fitted_model)
With all of these functions, the confidence level can be
adjusted using the conf.level argument (except for
confint(), which uses the level argument).
## Example of a 99% confidence interval
cor.test(x = tsa_laptops$Claim_Amount, y = tsa_laptops$Close_Amount, conf.level = 0.99)
##
## Pearson's product-moment correlation
##
## data: tsa_laptops$Claim_Amount and tsa_laptops$Close_Amount
## t = 4.7439, df = 1593, p-value = 2.284e-06
## alternative hypothesis: true correlation is not equal to 0
## 99 percent confidence interval:
## 0.05396995 0.18111678
## sample estimates:
## cor
## 0.1180271
Question #1: Michael Jordan is widely regarded as the best professional basketball player of all time. At the end of his playing career after the 2002-03 NBA season he averaged 30.1 points per game in his regular season appearances. In this question you will consider three different random samples of varying sizes (\(n=5\), \(n=25\), and \(n=75\)) of regular season games played by Michael Jordan.
mj5 = read.csv("https://remiller1450.github.io/data/mj5.csv")
mj25 = read.csv("https://remiller1450.github.io/data/mj25.csv")
mj75 = read.csv("https://remiller1450.github.io/data/mj75.csv")
mj75 sample (\(n=75\) games) use the mean()
and sd() functions to find the sample mean and sample
standard deviation for the number of points per game scored by Michael
Jordan.mj5 and
mj25 samples. Without calculating anything, if you were to
compute the margin of error for a 95% confidence interval for the mean
number of points per game scored by Michael Jordan, how would you expect
the size of each margin of error to compare with the one you found in
Part C? Order the margins of error from smallest to largest according to
your expectation and briefly justify your ordering.\(~\)
Question #2: For this question you should continue
using the mj75 sample. This question will involve a variety
of different research questions and your task is to find the requested
confidence interval estimate for the relevant population parameter using
an appropriate R function.
tov) and the number of points he scores. Find a
90% confidence interval estimate for an appropriate descriptive
statistic. Be sure you explore the data to determine the appropriate
statistic.lm() function to fit a
simple linear regression model that uses the stl, the
number of steals, to predict pts. Then use the
confint() function to find a 95% confidence interval
estimate for the slope coefficient.\(~\)
RStatKey provides an intuitive UI to see the results of bootstrapping,
but in a real-world setting it is valuable to perform an analysis that
is entirely reproducible via R.
To use bootstrapping to construct a confidence interval, we first
need to obtain many bootstrap samples from the original dataset. We can
do this by using a for loop to repeatedly apply the
sample() function with the argument
replace = TRUE to sample our dataset with replacement.
The example below bootstraps the correlation between
AvgMovingSpeed and TotalTime in the “commute
tracker” dataset we saw in our lecture slides:
## Load the real sample data
commute_tracker = read.csv("https://remiller1450.github.io/data/CommuteTracker.csv")
my_dataset = commute_tracker
## Setup
n_boot = 1000 # number of bootstrap samples to draw
n = nrow(my_dataset) # size of the real sample
boot_stats = numeric(n_boot) # empty object to store bootstrapped statistics
## Draw each sample and store the bootstrapped statistic
for(i in 1:n_boot){
## Use the sample function to identify which cases are selected in the bootstrap sample
case_idx = sample(x = 1:n, size = n, replace = TRUE)
## Put the selected cases into a data frame
cur_boot_sample = my_dataset[case_idx, ]
## Calculate the descriptive statistic of interest (correlation) for the current bootstrap sample
boot_stats[i] = cor(x=cur_boot_sample$AvgMovingSpeed, y=cur_boot_sample$TotalTime)
}
The object boot_stats stores the statistic of interest
(sample correlation) for 1000 different bootstrap samples. We can plot
the bootstrap distribution to get a sense of how much this correlation
might vary from one sample to another:
library(ggplot2)
ggplot() + geom_histogram(aes(x = boot_stats), bins = 20)
To find a \(P\%\) percentile
bootstrap confidence interval we need to find the relevant
percentiles that define the middle \(P\%\) of the bootstrap distribution, which
we can do using the quantile() function:
quantile(boot_stats, probs = c(0.005, 0.995))
## 0.5% 99.5%
## -0.9349234 -0.7555941
The above code gives us a 99% confidence interval estimate because it excludes the most extreme 1% of bootstrapped results (0.5% on each end of the bootstrap distribution).
If we had instead wanted a 95% confidence interval we simply need to
modify the probs argument to reflect the middle 95% of
bootstrapped samples:
quantile(boot_stats, probs = c(0.025, 0.975))
## 2.5% 97.5%
## -0.9168570 -0.7890995
Question #3: For this question you will consider
only the mj25 sample of Michael Jordan’s games.
mj25 = read.csv("https://remiller1450.github.io/data/mj25.csv")
quantile() function with the
appropriate values for the probs argument.t.test() function to
compute a 95% confidence interval using the probability model approach
for the mean number of points per game scored by Michael Jordan. How do
the endpoints of this interval compare with those you found using
bootstrapping in Part B?\(~\)
Many scholars define a mass shooting as an incident where four or more people are shot and either injured or killed in a short period of time. The Gun Violence Archive maintains a database of mass shootings that have happened in the United States. The data below are the incidents from 2025 that are recorded in this database, which we will view as a representative sample of mass shootings more broadly (ie: recent years besides just 2025).
mass_shootings = read.csv("https://remiller1450.github.io/data/mass_shootings_2025.csv")
Question #4: In addition to scenarios where the assumptions of the probability model approach are questionable, bootstrapping is also useful for unusual descriptive statistics where there is no commonly known probability model. An example is the mean-to-median ratio, which we saw in our last lecture. For this question you will use bootstrapping to create a 95% confidence interval for the mean-to-median ratio of a sample from a skewed population.
quantile() function to report the endpoints of the
interval.