What is Hypothesis Testing?

  • A statistical method used to make decisions using data.
  • Involves a null hypothesis (\(H_0\)) and an alternative hypothesis (\(H_1\)).
  • Uses sample data to determine if there is enough evidence to reject \(H_0\).

Key Concepts

  • \(H_0\): Default or status quo assumption
  • \(H_1\): What you want to test
  • Test statistic
  • P-value
  • Significance level \(\alpha\)

Real Example: Mean Test

We want to test if the average height of a population is 170 cm.

\(H_0: \mu = 170\)
\(H_1: \mu \ne 170\)

set.seed(88)
sample_heights <- rnorm(30, mean = 172, sd = 5)
test_result <- t.test(sample_heights, mu = 170)
cat(paste(
  "t-statistic =", round(test_result$statistic, 3), "|",
  "p-value =", round(test_result$p.value, 5)))
## t-statistic = 1.46 | p-value = 0.15499

ggplot: Histogram of Sample Heights

ggplot(data.frame(x = sample_heights), aes(x = x)) +
  geom_histogram(color = "black", fill = "skyblue", bins = 10) +
  geom_vline(xintercept = 170, color = "red", linetype = "dashed") +
  labs(title = "Histogram of Sample Heights", x = "Height (cm)", 
       y ="Frequency") +
  theme(plot.title = element_text(hjust = 0.5))

ggplot: Boxplot of Sample Heights

ggplot(data.frame(group = "Sample", value = sample_heights), 
       aes(x = group, y = value)) + geom_boxplot(fill = "lightgreen", 
      color = "darkgreen") + geom_hline(yintercept = 170, color = "red",    
                                        linetype = "dashed") +
  labs(title = "Boxplot of Sample Heights", x = "", y = "Height (cm)") +
  theme(plot.title = element_text(hjust = 0.5))

LaTeX: t-test Statistic

\[ t = \frac{\overline{x} - \mu_0}{s/\sqrt{n}} \]

Where:
- \(\overline{x}\) is the sample mean
- \(\mu_0\) is the hypothesized mean
- \(s\) is the sample standard deviation
- \(n\) is the sample size

Interactive Plot: t-distribution Surface

This 3D surface shows how the shape of the t-distribution changes with degrees of freedom.

Plotly 3D Surface of t-distribution

What is a p-value?

The p-value helps us decide if the difference we observe is likely due to chance.

For a two-sided t-test, the formula is:

\[ p = 2 \cdot P(T \ge |t|) \]

Where:

  • \(t\) is the t-statistic we calculated
  • \(T \sim t_{n-1}\) follows a t-distribution with \(n-1\) degrees of freedom
  • The value \(|t|\) is the distance from the hypothesized mean

This formula gives us the total probability of seeing a result as extreme as ours, in either direction, if the null hypothesis is true.

Smaller p-value → stronger evidence against \(H_0\).

Hypothesis Test with R Code

x_bar <- mean(sample_heights)
s <- sd(sample_heights)
n <- length(sample_heights)
t_stat <- (x_bar - 170) / (s / sqrt(n))
t_stat
## [1] 1.460168
df <- n - 1
p_val <- 2 * pt(-abs(t_stat), df)
p_val
## [1] 0.1549935

Conclusion

  • If p-value < \(\alpha = 0.05\), we reject \(H_0\).
  • The result suggests a significant difference from 170 cm.
  • Hypothesis testing evaluates evidence, not absolute truth.