Inference for two means

IMS, Ch. 20

Smith College

Nov 14, 2022

Correction

\(t\)-distribution only has one parameter

  • I said three

  • \(t\)-distribution is like the Standard Normal

    • always has mean 0
    • standard deviation is “baked in”
  • In a hypothesis test, use t-score (analogous to z-score):

\[ T = \frac{\bar{x} - \mu_0}{SE_X} = \frac{\bar{x} - \mu_0}{s_X / \sqrt{n}} \]

Inference for a difference in means

Method null dist. sampling dist.
1: simulation randomization test double bootstrap
2: probability ?? ??
3: \(t\)-approx. \(t \left( d.f. \right)\) \(t \left( d.f. \right)\)
  • See IMS, Chapter 20

\(t\)-based approximation

  • Standard error:

\[ SE_{X_1, X_2} = \sqrt{SE_{X_1}^2 + SE_{X_2}^2} \] - d.f. = \(min(n_1 - 1, n_2 - 1)\)

  • test statistic:

\[ T = \frac{(\bar{x}_1 - \bar{x}_2) - 0}{SE_{X_1, X_2}} \]

Birthweights

Every year, the US releases to the public a large data set containing information on births recorded in the country. This data set has been of interest to medical researchers who are studying the relation between habits and practices of expectant mothers and the birth of their children. This is a random sample of 1,000 cases from the data set released in 2014.

Observed statistics

library(tidyverse)
library(openintro)
births <- births14 |>
  filter(!is.na(habit))
obs <- births |>
  group_by(habit) |>
  summarize(
    num_births = n(),
    mean_weight = mean(weight),
    sd_weight = sd(weight)
  )
obs
# A tibble: 2 × 4
  habit     num_births mean_weight sd_weight
  <chr>          <int>       <dbl>     <dbl>
1 nonsmoker        867        7.27      1.23
2 smoker           114        6.68      1.60
  • Is the observed difference meaningful?

Exploratory data analysis

ggplot(births, aes(x = habit, y = weight)) +
  geom_jitter(width = 0.1, alpha = 0.3)

Randomization test

  • Construct null distribution via randomization
library(infer)
null_dist <- births |>
  specify(weight ~ habit) |>
  hypothesize(null = "independence") |>
  generate(2000, type = "permute") |>
  calculate(stat = "diff in means", order = c("smoker", "nonsmoker"))
  • Draw null distribution
null_plot <- null_dist |>
  ggplot(aes(x = stat)) +
  geom_density(fill = "darkgray") +
  geom_vline(xintercept = 0, linetype = 3) +
  geom_vline(xintercept = diff(obs$mean_weight), linetype = 2)

Null distribution

null_plot

Two-sample t-test

  • Do it all in one step!
births |>
  t_test(weight ~ habit, order = c("smoker", "nonsmoker"))
# A tibble: 1 × 7
  statistic  t_df  p_value alternative estimate lower_ci upper_ci
      <dbl> <dbl>    <dbl> <chr>          <dbl>    <dbl>    <dbl>
1     -3.82  131. 0.000208 two.sided     -0.593   -0.900   -0.285
  • Classic version
t.test(weight ~ habit, data = births)

    Welch Two Sample t-test

data:  weight by habit
t = 3.8166, df = 131.31, p-value = 0.0002075
alternative hypothesis: true difference in means between group nonsmoker and group smoker is not equal to 0
95 percent confidence interval:
 0.2854852 0.8998751
sample estimates:
mean in group nonsmoker    mean in group smoker 
               7.269873                6.677193 

\(t\)-based approximation

obs_t <- obs |>
  mutate(se = sd_weight / sqrt(num_births)) |>
  summarize(
    SE = sqrt(sum(se^2)),
    T = diff(mean_weight) / SE
  )
obs_t
# A tibble: 1 × 2
     SE     T
  <dbl> <dbl>
1 0.155 -3.82
my_t <- function(x, df) { dt(x / obs_t$SE, df) / obs_t$SE}

Compare nulls

null_plot +
  stat_function(fun = my_t, args = list(df = 131), color = "red") + 
  stat_function(fun = dnorm, args = list(mean = 0, sd = obs_t$SE), color = "blue")

p-values

  • Randomization test
null_dist |>
  get_p_value(obs_stat = obs_t$T, direction = "two-sided")
# A tibble: 1 × 1
  p_value
    <dbl>
1       0
  • \(t\)-based approximation
pt(obs_t$T, df = 131)
[1] 0.0001038671

Conclusion

  • Statistical significance:

There is a statistically significant reduction in the average birthweight of babies born to mothers who smoke.

  • Practical significance:

On average, babies born to mothers who smoke weighed 0.59 pounds less than babies born to mothers who didn’t smoke.

Your turn

See handout