2025-02-07

What is Hypothesis Testing?

Hypothesis testing is a statistical method used to make decisions about population parameters based on sample data. It involves:

  • Null Hypothesis (H₀): A statement of no effect (e.g., “The mean of Group A equals the mean of Group B”). \[ H_0: \mu_1 = \mu_2 \]

  • Alternative Hypothesis (H₁): A statement contradicting H₀ (e.g., “The means are different”). \[ H_1: \mu_1 \neq \mu_2 \]

Test Statistic and p-value

The test statistic quantifies the difference between the sample data and H₀. For a t-test: \[ t = \frac{\bar{X}_1 - \bar{X}_2}{s_p \sqrt{\frac{1}{n_1} + \frac{1}{n_2}}} \] Where: - \(\bar{X}_1, \bar{X}_2\): Sample means - \(s_p\): Pooled standard deviation - \(n_1, n_2\): Sample sizes

The p-value is the probability of observing the test statistic (or more extreme) if H₀ is true: \[ \text{p-value} = P(T \geq |t| \mid H_0) \]

Example: Comparing MPG by Transmission Type

We use the mtcars dataset to test if manual and automatic cars have different fuel efficiency (MPG).

data("mtcars")
head(mtcars)
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
  Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
  Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
  Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
  Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
  Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
  Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Density Plot

The Density plot shows how extreme the t-value is compared to H₀. Based off of the Density plot and t-value we can reject H₀.

Power Analysis

The larger the Cohen’s d (effect size) the higher the probability that we can reject H.

Results

result <- t.test(mpg ~ am, data = mtcars)
cat("t-value:", round(result$statistic, 2), "\n")
  t-value: -3.77
cat("p-value:", result$p.value)
  p-value: 0.001373638

Conclusion

  • Rejected H₀ (p < 0.05) → Manual cars have significantly higher MPG than automatics.

  • Hypothesis testing provides a framework to make data-driven decisions.