Hypothesis Testing
Hypothesis testing lets you make statistically grounded decisions from sample data — is a difference real, or just noise? R has built-in functions for every classic test.
1. The Core Idea — Null and Alternative Hypotheses
Every test compares a null hypothesis (H₀) — "no effect / no difference" — against an alternative hypothesis (H₁). The p-value tells you how surprising your observed data would be if H₀ were actually true.
Production Reality: A p-value below your significance threshold (commonly 0.05) suggests rejecting H0 — but it's not proof, and it says nothing about effect size or practical importance.
2. T-Tests — Comparing Means
One-Sample T-Test
scores <- c(72, 85, 90, 68, 75, 88, 95, 70)
t.test(scores, mu = 80) # is the mean significantly different from 80?
Two-Sample T-Test (Independent Groups)
group_a <- c(85, 90, 78, 92, 88)
group_b <- c(70, 75, 68, 80, 72)
t.test(group_a, group_b)
# or with a data frame:
t.test(salary ~ gender, data = df)
Paired T-Test
before <- c(80, 85, 78, 90, 82)
after <- c(88, 91, 85, 94, 89)
t.test(before, after, paired = TRUE)
3. Reading t.test() Output
| Field | Meaning |
|---|---|
| t | Test statistic |
| df | Degrees of freedom |
| p-value | Probability of observing this result under H0 |
| 95 percent confidence interval | Plausible range for the true difference |
4. ANOVA — Comparing 3+ Group Means
anova_model <- aov(salary ~ department, data = df)
summary(anova_model)
# Post-hoc test if ANOVA is significant
TukeyHSD(anova_model)
Pro Tip: Run ANOVA when comparing 3+ groups instead of multiple t-tests — running many t-tests inflates your false-positive rate (the multiple comparisons problem).
5. Chi-Square Test — Categorical Relationships
# Test of independence between two categorical variables
contingency_table <- table(df$department, df$gender)
chisq.test(contingency_table)
# Goodness-of-fit test
observed <- c(50, 30, 20)
expected_probs <- c(0.5, 0.3, 0.2)
chisq.test(observed, p = expected_probs)
6. Choosing the Right Test
| Question | Test |
|---|---|
| Is one group's mean different from a fixed value? | One-sample t-test |
| Do two independent groups differ in mean? | Two-sample t-test |
| Does the same group differ before/after? | Paired t-test |
| Do 3+ groups differ in mean? | ANOVA |
| Are two categorical variables related? | Chi-square test |
7. Checking Test Assumptions
shapiro.test(scores) # test for normality
var.test(group_a, group_b) # test for equal variances (before t.test)
8. Production-Ready Checklist
- ✅ Know your hypotheses before testing — H0 and H1, clearly stated.
- ✅ Choose the correct test — based on data type and number of groups.
- ✅ Check assumptions — normality, equal variance — before trusting results.
- ✅ Interpret p-values carefully — statistical significance ≠ practical importance.
Pro Tip: Always pair a p-value with an effect size and confidence interval — a tiny, statistically significant difference may be practically meaningless.
Ready to master R Programming?
Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.
.png)