Hypothesis Testing (t.test, chisq.test)
Hypothesis testing is the statistical procedure for deciding whether a pattern in your data is real or could plausibly have arisen by chance, by comparing it against a null hypothesis of "no effect" and measuring the surprise with a p-value.
Learn Hypothesis Testing (t.test, chisq.test) in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn the logic of a test and the meaning of a p-value, run one- and two-sample t.test() , switch to the robust wilcox.test() , test categorical counts with chisq.test() , and read every part of R's output — including the confidence interval.
What You'll Learn in This Lesson
1️⃣ The Logic & the t-test
Every test starts with a null hypothesis (H0) of "no effect," then asks how surprising your data would be if H0 were true. The t.test() compares means. A one-sample test checks a mean against a fixed target.
The two-sample version asks whether two groups differ. R's default is Welch's t-test , which doesn't assume the groups share the same variance — the safer choice in practice.
2️⃣ When Normality Fails: wilcox.test
The t-test leans on roughly normal data. With small, skewed, or outlier-ridden samples, switch to the non-parametric wilcox.test() , which compares distributions through ranks and is far more robust.
You read its output the same way — a test statistic (W) and a p-value — but the underlying null is "the two distributions are the same," and no normality is assumed.
3️⃣ Categorical Data: chisq.test
When your data are counts in categories rather than numbers you average, the chi-squared test asks whether two categorical variables are associated. You feed it a contingency table of observed frequencies.
Finally, every test returns an object you can index. Pulling out the $p.value or $conf.int lets you use results in reports and pipelines.
Your turn. Fill in the # TODO blank, run it, and read the p-values and intervals.
The hardest part of testing is picking the RIGHT test. These are counts by category, so it's a chi-squared problem — not a t-test. Build the table, run it, and write a plain-English conclusion.
📋 Quick Reference — Hypothesis Tests
Practice quiz
What does a p-value measure?
- The probability of data this extreme if the null hypothesis were true
- The probability the null is true
- The size of the effect
- The sample size needed
Answer: The probability of data this extreme if the null hypothesis were true. A p-value is the chance of data at least this extreme assuming the null (no effect).
Which test compares a numeric MEAN against a target or between two groups?
- chisq.test()
- t.test()
- fisher.test()
- prop.test()
Answer: t.test(). t.test() compares means (one-sample against mu, or two groups).
By default, R's two-sample t.test() runs which version?
- Paired t-test
- A one-sample test
- Welch's t-test (does not assume equal variances)
- A z-test
Answer: Welch's t-test (does not assume equal variances). R defaults to Welch's t-test, which does not assume equal variances.
Which non-parametric test replaces t.test when normality is doubtful?
- chisq.test()
- cor.test()
- aov()
- wilcox.test()
Answer: wilcox.test(). wilcox.test() compares distributions via ranks and is robust to non-normality.
Which test works on COUNTS to check association between categorical variables?
- chisq.test()
- t.test()
- wilcox.test()
- lm()
Answer: chisq.test(). chisq.test() tests association in a contingency table of counts.
In t.test(value ~ group, data = df), what does the formula say?
- Multiply value by group
- Sort value by group
- Compare value across the levels of group
- Add value and group
Answer: Compare value across the levels of group. value ~ group compares the numeric value across the groups; data = df supplies the columns.
Conventionally, what does p < 0.05 lead you to do?
- Accept the null as proven
- Reject the null hypothesis
- Ignore the result
- Increase the sample
Answer: Reject the null hypothesis. p < 0.05 is taken as evidence against the null, so you reject it (by convention).
If a two-sample 95% CI for the difference of means does NOT contain 0, this agrees with:
- A non-significant result
- An error in the data
- No possible conclusion
- A significant difference (p < 0.05)
Answer: A significant difference (p < 0.05). A CI excluding 0 is consistent with a significant difference at the 0.05 level.
How do you extract just the p-value from a test result res?
- res$p.value
- res$pval
- pvalue(res)
Answer: res$p.value. Tests return an object you index with $; res$p.value gives the p-value.
A non-significant result (p > 0.05) means:
- The null is definitely true
- There is no effect at all
- You lack evidence against the null, not proof it is true
- The test was run wrong
Answer: You lack evidence against the null, not proof it is true. Absence of evidence is not evidence of absence; you simply failed to reject the null.