Making decisions
Lecture 24
Warm-up
It’s (almost) Giving Tuesday!
Give feedback!
Take 2 minutes to fill out the TA evaluation form – link in your email! Due Monday, December 8th.
Nominate a TA for the StatSci TA of the Year award by sending an email to dus@stat.duke.edu with a brief narrative for your nomination.
Please also fill out the course evaluation (on DukeHub) as well, I’d love to your feedback!
![]() Allison Yang |
![]() Anric Ngan |
![]() Carl Emerson |
![]() Chelsea Nguyen |
![]() Ethan Cho |
![]() Helen Chen |
![]() Hellen Han |
![]() Hyunjin Lee |
![]() Josh Lim |
![]() Juan Pablo L. Escamilla |
![]() Kenna Roberts |
![]() Max Niu |
![]() Oliver Gao |
![]() Robbie Hao |
![]() Sophie Schwartz |
![]() Tally Coulter |
![]() Yasmine Abdel-Rahman |
Announcements
- HW:
- HW 7 due at 11:59 pm on Wednesday, Dec 2
- Final exam:
- Classroom: 2/3 of you will take it in this room (Bio Sci 111) the other 1/3 in Old Chem 116, check your email for your classroom assignment
- Review: During reading period, Monday, Dec 7, 10 am - 11:15 am
- Office hours:
- This week until the end of Friday – as usual
- Starting Saturday till the final exam – to be posted on Wednesday
From last time
Participate 📱💻
Which of the following is true about confidence intervals?
- They’re for a sample statistic.
- They’re for a population parameter.
- They can be either for both a sample statistic or a population parameter.
- They’re neither for a sample statistic nor a population parameter.
Go to wooclap.com and use the code ZCUJHJN.
Why do we construct confidence intervals?
To estimate plausible values of a parameter of interest, e.g.,
- a slope (\(\beta_1\))
- a mean (\(\mu\))
- a proportion (\(p\))
- etc.
What is bootstrapping?
Bootstrapping is a statistical procedure that resamples(with replacement) a single data set to create many simulated samples.
We then use these simulated samples to quantify the uncertainty around the sample statistic we’re interested in, e.g., a slope (\(b_1\)), a mean (\(\bar{x}\)), a proportion (\(\hat{p}\)).
Setup
Computing the CI for the slope I
Calculate the observed slope:
observed_fit <- duke_forest |>
specify(price ~ area) |>
fit()
observed_fit# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 intercept 116652.
2 area 159.
Computing the CI for the slope II
Take 1000 bootstrap samples and fit models to each one:
# A tibble: 2,000 × 3
# Groups: replicate [1,000]
replicate term estimate
<int> <chr> <dbl>
1 1 intercept 73046.
2 1 area 177.
3 2 intercept 7730.
4 2 area 205.
5 3 intercept 164620.
6 3 area 135.
7 4 intercept 90690.
8 4 area 170.
9 5 intercept 270437.
10 5 area 100.
# ℹ 1,990 more rows
What does each observation on the plot represent?
Computing the CI for the slope III
Percentile method: Compute the 95% CI as the middle 95% of the bootstrap distribution:
Participate 📱💻
If you want to be very certain (i.e., more confident) that you capture the population parameter, should we use a wider or a narrower interval?
- Wider
- Narrower
- Depends on the situation
Go to wooclap.com and use the code ZCUJHJN.
Precision vs. accuracy
What drawbacks are associated with using a wider interval?
. . .
Precision vs. accuracy
How can we get best of both worlds – high precision and high accuracy?
Recap
Population: Complete set of observations of whatever we are studying, e.g., people, tweets, photographs, etc. – population size = \(N\)
Sample: Subset of the population, ideally random and representative – sample size = \(n\)
Sample statistic \(\ne\) population parameter, but if the sample is good, it can be a good estimate
Statistical inference: Discipline that concerns itself with the development of procedures, methods, and theorems that allow us to extract meaning and information from data that has been generated by stochastic (random) process
We report the estimate with a confidence interval, and the width of this interval depends on the variability of sample statistics from different samples from the population
Since we can’t continue sampling from the population, we bootstrap from the one sample we have to estimate sampling variability
An alternative approach
Standard error method: Compute the 95% CI as the observed slope plus/minus ~2 * standard error (the standard deviation of the bootstrap distribution):
Margin of error
That quantity (~2 * standard error) is called the margin of error, e.g.,
On the horizon…
In this class you learned how to construct a confidence interval (i.e., calculate the margin of error) using a computational method called bootstrapping.
The bootstrap distributions you constructed (given enough
reps– repeated samples) were unimodal and symmetric around the observed statistic.This is not a happenstance! And there is theory behind it… It’s called the Central Limit Theorem!
You can learn about the Central Limit Theorem and theory-based methods for constructing confidence intervals (and other inference procedures) in future stats courses.
Syntax notes
-
Bootstrapping for categorical data
specify(response = x, success = "success level")calculate(stat = "prop")
. . .
-
Bootstrapping for other
statscalculate()documentation: infer.tidymodels.org/reference/calculate.htmlinfer pipelines: infer.tidymodels.org/articles/observed_stat_examples.html
Hypothesis testing
Hypothesis testing
A hypothesis test is a statistical technique used to evaluate competing claims using data
Null hypothesis, \(H_0\): An assumption about the population. “There is nothing going on.”
Alternative hypothesis, \(H_A\): A research question about the population. “There is something going on”.
. . .
Hypotheses are always at the population level!
Hypothesis testing as a court trial
Null hypothesis, \(H_0\): Defendant is innocent
Alternative hypothesis, \(H_A\): Defendant is guilty
. . .
- Present the evidence: Collect data
. . .
-
Judge the evidence: “Could these data plausibly have happened by chance if the null hypothesis were true?”
- Yes: Fail to reject \(H_0\)
- No: Reject \(H_0\)
Hypothesis testing framework
Start with a null hypothesis, \(H_0\), that represents the status quo
Set an alternative hypothesis, \(H_A\), that represents the research question, i.e. what we’re testing for
-
Conduct a hypothesis test under the assumption that the null hypothesis is true and calculate a p-value (probability of observed or more extreme outcome given that the null hypothesis is true)
- if the test results suggest that the data do not provide convincing evidence for the alternative hypothesis, stick with the null hypothesis
- if they do, then reject the null hypothesis in favor of the alternative
Two examples
Predicting sleep duration from screen time.
Predicting the prices of houses in Duke Forest from their areas.
Example 1: Predicting sleep duration from screen time
Recap: The data
15 randomly sampled students’ responses to an online, anonymous survey:
Code
ggplot(screen_sleep, aes(x = screen, y = sleep)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE, color = "#8F2D56") +
labs(
x = "Screen time (hours per day)",
y = "Sleep (hours per night)",
title = "Screen time and sleep of STA 199 students"
)Setting hypotheses
Null hypothesis, \(H_0\): “There is nothing going on.” The slope of the model for predicting the sleep duration of Duke undergrads from their screen time is 0, \(\beta_1 = 0\).
Alternative hypothesis, \(H_A\): “There is something going on”. The slope of the model for predicting the sleep duration of Duke undergrads from their screen time is different than 0, \(\beta_1 \ne 0\).
Calculate observed slope
sleep_obs_fit <- screen_sleep |>
specify(sleep ~ screen) |>
fit()
sleep_obs_fit# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 intercept 6.12
2 screen 0.151
Hypothesis testing “mindset”
Assume you live in a world where null hypothesis is true: \(\beta_1 = 0\).
Ask yourself how likely you are to observe the sample statistic, or something even more extreme, in this world:
\[ P \big( b_1 \leq -0.15~or~ b_1 \geq 0.15 ~|~ \beta_1 = 0 \big) \]
A world where \(\beta_1 = 0\)
If there is no relationship between screen time and sleep, then any student’s sleep duration could just as easily have been paired with any other student’s screen time.
So, we can simulate a world where the null hypothesis is true by permuting (shuffling) the sleep durations across students, while keeping the screen times where they are.
Fitting a model to each shuffled dataset gives us a slope we might observe if the null hypothesis were true.
Doing this many times gives us the null distribution of the slope.
Permuting a single sample
Permuting many samples
Simulate null: specify()
Specify the response and explanatory variables:
screen_sleep |>
specify(sleep ~ screen)Response: sleep (numeric)
Explanatory: screen (numeric)
# A tibble: 15 × 2
sleep screen
<dbl> <dbl>
1 6 6
2 5 2.5
3 7.5 8.7
4 8.12 2.75
5 6.5 11.9
6 8.63 7.8
7 7 5.8
8 8.5 6
9 8.5 10.4
10 7 5.5
11 6 5.3
12 7 5.9
13 8.32 6.23
14 6.15 5.6
15 6 5
Simulate null: hypothesize()
Declare the null hypothesis – sleep and screen are independent:
Response: sleep (numeric)
Explanatory: screen (numeric)
Null Hypothesis: inde...
# A tibble: 15 × 2
sleep screen
<dbl> <dbl>
1 6 6
2 5 2.5
3 7.5 8.7
4 8.12 2.75
5 6.5 11.9
6 8.63 7.8
7 7 5.8
8 8.5 6
9 8.5 10.4
10 7 5.5
11 6 5.3
12 7 5.9
13 8.32 6.23
14 6.15 5.6
15 6 5
Simulate null: generate()
Generate 1000 permuted datasets by shuffling sleep across students:
Response: sleep (numeric)
Explanatory: screen (numeric)
Null Hypothesis: inde...
# A tibble: 15,000 × 3
# Groups: replicate [1,000]
sleep screen replicate
<dbl> <dbl> <int>
1 6.5 6 1
2 6 2.5 1
3 5 8.7 1
4 8.5 2.75 1
5 7 11.9 1
6 7 7.8 1
7 7.5 5.8 1
8 8.63 6 1
9 6 10.4 1
10 8.12 5.5 1
# ℹ 14,990 more rows
Simulate null: fit()
Fit a model to each permuted dataset, and save the result:
# A tibble: 2,000 × 3
# Groups: replicate [1,000]
replicate term estimate
<int> <chr> <dbl>
1 1 intercept 8.02
2 1 screen -0.147
3 2 intercept 6.84
4 2 screen 0.0380
5 3 intercept 6.66
6 3 screen 0.0666
7 4 intercept 6.91
8 4 screen 0.0262
9 5 intercept 6.01
10 5 screen 0.168
# ℹ 1,990 more rows
Visualize null distribution + p-value
Get p-value
sleep_null_dist |>
get_p_value(
obs_stat = sleep_obs_fit,
direction = "two-sided"
)# A tibble: 2 × 2
term p_value
<chr> <dbl>
1 intercept 0.206
2 screen 0.206
Make a decision
Based on the p-value calculated, what is the conclusion of the hypothesis test? Interpret it in the context of screen time and sleep.
Example 2: Predicting the prices of houses in Duke Forest from their areas
Participate 📱💻
Which pair of hypotheses is correct for testing whether area is a useful predictor of price for houses in Duke Forest?
- \(H_0: b_1 = 0\), \(H_A: b_1 \ne 0\)
- \(H_0: \beta_1 = 0\), \(H_A: \beta_1 \ne 0\)
- \(H_0: \beta_1 = 159\), \(H_A: \beta_1 \ne 159\)
- \(H_0: \beta_1 \ne 0\), \(H_A: \beta_1 = 0\)
Go to wooclap.com and use the code ZCUJHJN.
Setting hypotheses
Null hypothesis, \(H_0\): “There is nothing going on.” The slope of the model for predicting the prices of houses in Duke Forest from their areas is 0, \(\beta_1 = 0\).
Alternative hypothesis, \(H_A\): “There is something going on”. The slope of the model for predicting the prices of houses in Duke Forest from their areas is different than, \(\beta_1 \ne 0\).
Participate 📱💻
The p-value for this test is the probability of…
- observing a sample slope of 159 or more extreme, if \(\beta_1 = 0\)
- \(\beta_1\) being 0, given that we observed a sample slope of 159
- observing a sample slope of exactly 159, if \(\beta_1 = 0\)
- the alternative hypothesis being true, given the data
Go to wooclap.com and use the code ZCUJHJN.
Calculate observed slope
… which we have already done:
observed_fit <- duke_forest |>
specify(price ~ area) |>
fit()
observed_fit# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 intercept 116652.
2 area 159.
Simulate null distribution
View null distribution
null_dist# A tibble: 2,000 × 3
# Groups: replicate [1,000]
replicate term estimate
<int> <chr> <dbl>
1 1 intercept 568295.
2 1 area -3.02
3 2 intercept 597577.
4 2 area -13.6
5 3 intercept 560760.
6 3 area -0.310
7 4 intercept 556322.
8 4 area 1.29
9 5 intercept 588234.
10 5 area -10.2
# ℹ 1,990 more rows
Participate 📱💻
Before we plot it: where do you expect the null distribution of slopes to be centered?
- Around 0
- Around 159, the observed slope
- Around 1738, the seed
- Around the average house price
- It depends on the seed, so we can’t know ahead of time
Go to wooclap.com and use the code ZCUJHJN.
Visualize null distribution
Participate 📱💻
If we shade the area beyond \(\pm 159\) on the null distribution, what will the shaded area look like?
- Essentially nothing will be shaded, because 159 is far out in the tails
- About 5% of the distribution will be shaded
- About half of the distribution will be shaded
- All of it will be shaded, because 159 > 0
Go to wooclap.com and use the code ZCUJHJN.
Visualize null distribution + p-value
Get p-value
null_dist |>
get_p_value(obs_stat = observed_fit, direction = "two-sided")Warning: Please be cautious in reporting a p-value of 0. This result is an approximation
based on the number of `reps` chosen in the `generate()` step.
ℹ See `get_p_value()` (`?infer::get_p_value()`) for more information.
Please be cautious in reporting a p-value of 0. This result is an approximation
based on the number of `reps` chosen in the `generate()` step.
ℹ See `get_p_value()` (`?infer::get_p_value()`) for more information.
# A tibble: 2 × 2
term p_value
<chr> <dbl>
1 area 0
2 intercept 0
Participate 📱💻
R reports a p-value of 0. What’s the best interpretation?
- It is impossible to observe a slope of 159 if \(\beta_1 = 0\)
- None of the 1000 shuffled slopes were as or more extreme than 159, so the p-value is very small (less than 1/1000)
- We have proven that \(\beta_1 \ne 0\)
- Something went wrong with the simulation
Go to wooclap.com and use the code ZCUJHJN.
Participate 📱💻
Which conclusion is best supported by this hypothesis test?
- Reject \(H_0\): the data provide convincing evidence of a linear relationship between area and price for houses in Duke Forest
- Reject \(H_0\): we’ve shown that \(\beta_1 = 159\)
- Reject \(H_0\): larger area causes houses in Duke Forest to be more expensive
- Fail to reject \(H_0\): the p-value is 0, which is less than any reasonable cutoff
Go to wooclap.com and use the code ZCUJHJN.
Participate 📱💻
In Example 1 (screen time and sleep), the p-value was about 0.23, so we fail to reject \(H_0\). What does that mean?
- There is no relationship between screen time and sleep
- These data don’t provide convincing evidence of a relationship between screen time and sleep
- Screen time has no effect on sleep
- We’ve proven \(H_0\) is true
Go to wooclap.com and use the code ZCUJHJN.

























