Rows: 1,000
Columns: 13
$ fage <int> 34, 36, 37, NA, 32, 32, 37, 29, 30, 29, 30, 34, 28, 28,…
$ mage <dbl> 34, 31, 36, 16, 31, 26, 36, 24, 32, 26, 34, 27, 22, 31,…
$ mature <chr> "younger mom", "younger mom", "mature mom", "younger mo…
$ weeks <dbl> 37, 41, 37, 38, 36, 39, 36, 40, 39, 39, 42, 40, 40, 39,…
$ premie <chr> "full term", "full term", "full term", "full term", "pr…
$ visits <dbl> 14, 12, 10, NA, 12, 14, 10, 13, 15, 11, 14, 16, 20, 15,…
$ gained <dbl> 28, 41, 28, 29, 48, 45, 20, 65, 25, 22, 40, 30, 31, NA,…
$ weight <dbl> 6.96, 8.86, 7.51, 6.19, 6.75, 6.69, 6.13, 6.74, 8.94, 9…
$ lowbirthweight <chr> "not low", "not low", "not low", "not low", "not low", …
$ sex <chr> "male", "female", "female", "male", "female", "female",…
$ habit <chr> "nonsmoker", "nonsmoker", "nonsmoker", "nonsmoker", "no…
$ marital <chr> "married", "married", "married", "not married", "marrie…
$ whitemom <chr> "white", "white", "not white", "white", "white", "white…
AE 15: births
A statistical problem consists of four things:
- (data structure) in this class our data always come as a data frame, so data structures differ according to how many columns they have and what the variable types are;
- (target) what unknown quantity are we trying to use the data to learn about?
- (estimator) how are we going to convert our data into a best guess (aka estimate) of the unknown target?
- (sampling distribution) what strategy are we going to use to approximate the sampling distribution of the estimate? We’ve seen two options, both of which are approximations that make assumptions and work better sometimes than others:
- simulation (histogram)
- mathematical (smooth curve)
In general, the simulation-based approach may be more versatile across different kinds of statistical problems.
To illustrate all of the options we have encountered, we will use different subsets of columns in the births14 dataset in the openintro package:
One binary variable (estimating a proportion)
What fraction of mothers smoke?
We have data on this!
births14 |> select(habit)# A tibble: 1,000 × 1
habit
<chr>
1 nonsmoker
2 nonsmoker
3 nonsmoker
4 nonsmoker
5 nonsmoker
6 nonsmoker
7 nonsmoker
8 nonsmoker
9 nonsmoker
10 nonsmoker
# ℹ 990 more rows
Point estimation
obs_prop <- births14 |>
drop_na(habit) |>
specify(response = habit, success = "smoker") |>
calculate(stat = "prop")
obs_propResponse: habit (factor)
# A tibble: 1 × 1
stat
<dbl>
1 0.116
Simulation-based approximation
set.seed(4567)
boot_dist_one_prop <- births14 |>
drop_na(habit) |>
specify(response = habit, success = "smoker") |>
generate(reps = 250, type = "bootstrap") |>
calculate(stat = "prop")
boot_ci_prop <- boot_dist_one_prop |>
get_confidence_interval(point_estimate = obs_prop, level = 0.9, type = "percentile")
visualize(boot_dist_one_prop) +
shade_confidence_interval(boot_ci_prop)
boot_ci_prop# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 0.0989 0.134
Mathematical approximation
The central limit theorem (and other results) promises that for large enough sample size (number of rows \(n\)), the sampling distribution of the sample proportion will be approximately:
\[ N\left(\widehat{prop},\,SE = \sqrt{\frac{\widehat{prop}(1-\widehat{prop})}{n}}\right). \]
norm_dist_one_prop <- births14 |>
drop_na(habit) |>
specify(response = habit, success = "smoker") |>
assume(distribution = "z")
norm_ci_prop <- norm_dist_one_prop |>
get_confidence_interval(point_estimate = obs_prop, level = 0.9, type = "se")
visualize(norm_dist_one_prop) +
shade_confidence_interval(norm_ci_prop)
norm_ci_prop# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 0.0994 0.133
Two binary variables (estimating a difference in proportions)
How is smoking (
habit) related to whether or not the birth is classified as premature (premie)?
We have data on this!
births14 |> select(habit, premie)# A tibble: 1,000 × 2
habit premie
<chr> <chr>
1 nonsmoker full term
2 nonsmoker full term
3 nonsmoker full term
4 nonsmoker full term
5 nonsmoker premie
6 nonsmoker full term
7 nonsmoker premie
8 nonsmoker full term
9 nonsmoker full term
10 nonsmoker full term
# ℹ 990 more rows
Point estimation
Old way:
births14 |>
drop_na(habit) |>
count(habit, premie) |>
group_by(habit) |>
mutate(
prop = n / sum(n)
)# A tibble: 4 × 4
# Groups: habit [2]
habit premie n prop
<chr> <chr> <int> <dbl>
1 nonsmoker full term 771 0.889
2 nonsmoker premie 96 0.111
3 smoker full term 89 0.781
4 smoker premie 25 0.219
New way:
Simulation-based approximation
set.seed(4567)
boot_dist_two_prop <- births14 |>
drop_na(habit) |>
specify(response = premie, explanatory = habit, success = "premie") |>
generate(reps = 250, type = "bootstrap") |>
calculate(stat = "diff in props", order = c("smoker", "nonsmoker"))
boot_ci_two_prop <- boot_dist_two_prop |>
get_confidence_interval(point_estimate = obs_prop_diff, level = 0.9, type = "percentile")
visualize(boot_dist_two_prop) +
shade_confidence_interval(boot_ci_two_prop)
boot_ci_prop# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 0.0989 0.134
Mathematical approximation
The central limit theorem (and other results) promises that for large enough sample size (number of rows \(n=n_1+n_2\)), the sampling distribution of the difference in proportions will be approximately:
\[ N\left(\hat{p}_1-\hat{p}_2,\,SE = \sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{\sqrt{n_1}}+\frac{\hat{p}_2(1-\hat{p}_2)}{\sqrt{n_2}}}\right). \]
norm_dist_two_prop <- births14 |>
drop_na(habit) |>
specify(response = premie, explanatory = habit, success = "premie") |>
assume(distribution = "z")
norm_ci_two_prop <- norm_dist_two_prop |>
get_confidence_interval(point_estimate = obs_prop_diff, level = 0.9, type = "se")
visualize(norm_dist_two_prop) +
shade_confidence_interval(norm_ci_two_prop)
norm_ci_two_prop# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 0.0425 0.175
One numerical variable (estimating a mean)
How heavy are newborns, typically?
We have data on this!
births14 |> select(weight)# A tibble: 1,000 × 1
weight
<dbl>
1 6.96
2 8.86
3 7.51
4 6.19
5 6.75
6 6.69
7 6.13
8 6.74
9 8.94
10 9.12
# ℹ 990 more rows
Point estimation
obs_mean <- births14 |>
specify(response = weight) |>
calculate(stat = "mean")
obs_meanResponse: weight (numeric)
# A tibble: 1 × 1
stat
<dbl>
1 7.20
Simulation-based approximation
set.seed(4567)
boot_dist_one_mean <- births14 |>
specify(response = weight) |>
generate(reps = 250, type = "bootstrap") |>
calculate(stat = "mean")
boot_ci_mean <- boot_dist_one_mean |>
get_confidence_interval(point_estimate = obs_mean, level = 0.9, type = "percentile")
visualize(boot_dist_one_mean) +
shade_confidence_interval(boot_ci_mean)
boot_ci_mean# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 7.13 7.26
Mathematical approximation
The central limit theorem (and other results) promises that for large enough sample size (number of rows \(n\)), the sampling distribution of the sample average will be approximately:
\[ N\left(\bar{x},\,SE = \frac{s}{\sqrt{n}}\right). \]
norm_dist_one_mean <- births14 |>
specify(response = weight) |>
assume(distribution = "t")
norm_ci_mean <- norm_dist_one_mean |>
get_confidence_interval(point_estimate = obs_mean, level = 0.9, type = "se")
visualize(norm_dist_one_mean) +
shade_confidence_interval(norm_ci_mean)
norm_ci_mean# A tibble: 1 × 2
lower_ci upper_ci
<dbl> <dbl>
1 7.13 7.27
One numerical variable and one binary variable (estimating a difference in means)
Do newborn weights differ depending on whether or not the mother smokes?
We have data on this!
births14 |> select(habit, weight)# A tibble: 1,000 × 2
habit weight
<chr> <dbl>
1 nonsmoker 6.96
2 nonsmoker 8.86
3 nonsmoker 7.51
4 nonsmoker 6.19
5 nonsmoker 6.75
6 nonsmoker 6.69
7 nonsmoker 6.13
8 nonsmoker 6.74
9 nonsmoker 8.94
10 nonsmoker 9.12
# ℹ 990 more rows
Point estimation
Simulation-based approximation
Mathematical approximation
Two numerical variables (estimating a regression slope)
Is the weight of the baby related to the length of the pregnancy?
We have data on this!
births14 |> select(weeks, weight)# A tibble: 1,000 × 2
weeks weight
<dbl> <dbl>
1 37 6.96
2 41 8.86
3 37 7.51
4 38 6.19
5 36 6.75
6 39 6.69
7 36 6.13
8 40 6.74
9 39 8.94
10 39 9.12
# ℹ 990 more rows
Point estimation
obs_fit <- births14 |>
specify(weight ~ weeks) |>
fit()
obs_fit# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 intercept -3.60
2 weeks 0.279
Simulation-based approximation
set.seed(4567)
boot_dist_fit <- births14 |>
specify(weight ~ weeks) |>
generate(reps = 250, type = "bootstrap") |>
fit()
boot_ci_fit <- boot_dist_fit |>
get_confidence_interval(point_estimate = obs_fit, level = 0.9, type = "percentile")
visualize(boot_dist_fit) +
shade_confidence_interval(boot_ci_fit)
boot_ci_fit# A tibble: 2 × 3
term lower_ci upper_ci
<chr> <dbl> <dbl>
1 intercept -4.92 -2.21
2 weeks 0.244 0.313
Mathematical approximation
Apparently, the infer package doesn’t actually implement this, but that’s okay.
