TExcelTAD
Home › R › R Hypothesis Tests

R Hypothesis Tests

A hypothesis test asks whether a pattern in a sample is strong enough to believe it exists in the population, or whether it could easily be random noise.

Every test follows the same recipe:

  1. State a null hypothesis H₀ ("no difference", "no association") and an alternative H₁.
  2. Compute a test statistic from the data.
  3. Get the p-value: the probability of seeing a result at least this extreme if H₀ were true.
  4. If the p-value is below a chosen significance level (often 0.05), reject H₀.

One-sample t-test

Does a sample mean differ from a reference value? A café claims its espresso shots weigh 30 g on average; we weigh 12 shots:

r · try it yourself
shots <- c(28.1, 29.4, 30.2, 27.8, 28.9, 29.1, 28.4, 30.5, 28.0, 29.0, 28.7, 29.3)
mean(shots)
t.test(shots, mu = 30)
[1] 28.95

	One Sample t-test

data:  shots
t = -4.3673, df = 11, p-value = 0.001123
alternative hypothesis: true mean is not equal to 30
95 percent confidence interval:
 28.42083 29.47917
sample estimates:
mean of x
    28.95

The p-value is far below 0.05, so we reject H₀: the shots are lighter than 30 g on average. The 95% confidence interval for the true mean lies entirely below 30.

Two-sample t-test

Do two groups have different means? In ToothGrowth, guinea pigs got vitamin C as orange juice (OJ) or as ascorbic acid (VC). Let's compare the two supplements at the lowest dose:

r · try it yourself
low <- subset(ToothGrowth, dose == 0.5)
tapply(low$len, low$supp, mean)
result <- t.test(len ~ supp, data = low)
result
   OJ    VC
13.23  7.98

	Welch Two Sample t-test

data:  len by supp
t = 3.1697, df = 14.969, p-value = 0.006359
alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
95 percent confidence interval:
 1.719057 8.780943
sample estimates:
mean in group OJ mean in group VC
           13.23             7.98

By default R runs Welch's t-test, which does not assume equal variances. Test results are lists, so you can pull out the numbers you need:

r · try it yourself
low <- subset(ToothGrowth, dose == 0.5)
result <- t.test(len ~ supp, data = low)
round(result$p.value, 4)
round(result$conf.int, 2)
names(result)
[1] 0.0064
[1] 1.72 8.78
attr(,"conf.level")
[1] 0.95
 [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"
 [6] "null.value"  "stderr"      "alternative" "method"      "data.name"

For measurements taken twice on the same subjects (before/after), use t.test(after, before, paired = TRUE).

Chi-squared test of independence

For two categorical variables, the chi-squared test asks whether they are associated. Is the transmission type in mtcars related to the number of cylinders?

r · try it yourself
tab <- table(mtcars$am, mtcars$cyl)
tab
suppressWarnings(chisq.test(tab))

     4  6  8
  0  3  4 12
  1  8  3  2

	Pearson's Chi-squared test

data:  tab
X-squared = 8.7407, df = 2, p-value = 0.01265

The p-value is below 0.05, so transmission and cylinders appear to be related (manual cars tend to have fewer cylinders). We wrapped the call in suppressWarnings() because some expected counts are below 5. Without it, R warns that the approximation may be inaccurate:

r · try it yourself
chisq.test(table(mtcars$am, mtcars$cyl))$p.value
[1] 0.01264661
Warning message:
In chisq.test(table(mtcars$am, mtcars$cyl)) : Chi-squared approximation may be incorrect

Goodness of fit

chisq.test() also checks whether observed counts match expected proportions. Is this die fair?

r · try it yourself
rolls <- c(`1` = 18, `2` = 22, `3` = 16, `4` = 25, `5` = 19, `6` = 20)
chisq.test(rolls, p = rep(1/6, 6))

	Chi-squared test for given probabilities

data:  rolls
X-squared = 2.5, df = 5, p-value = 0.7765

With a p-value well above 0.05, there is no evidence that the die is unfair. Note that this does not prove it is fair, only that the data is consistent with fairness.

Other common tests

Question Function
Is a correlation different from zero? cor.test(x, y)
Do 3+ group means differ? aov(y ~ group) + summary() (ANOVA)
Non-parametric two-group comparison wilcox.test()
Is a sample normally distributed? shapiro.test()
Proportions prop.test(), binom.test()

Test yourself

A p-value of 0.003 means…

  1. H₀ is true with probability 0.003
  2. Data this extreme would be very unlikely if H₀ were true
  3. The effect is small
  4. The test failed

Which test compares the means of two independent groups?

  1. chisq.test()
  2. t.test()
  3. cor.test()
  4. shapiro.test()

Which test checks whether two categorical variables are associated?

  1. t.test()
  2. lm()
  3. chisq.test()
  4. quantile()