Skip to content

Multiple Groups (ANOVA & Kruskal-Wallis)

With three or more groups, the first idea is a t-test on every pair. Each of those tests brings its own chance of a false positive, and the chances add up. One-way analysis of variance, or ANOVA, puts one question to all the groups at once: could these group means come from one common mean? This page compares the log2 expression of a gene across a control and two drug doses.

Use one-way ANOVA when the question concerns the means of three or more independent groups. Its F-test keeps the chance of a false positive at the chosen level, usually 5%, when every group has the same mean, however many groups there are. The p-value rests on two assumptions about the errors within the groups: they are roughly normal, and they have a similar variance in every group. Welch’s ANOVA drops the second assumption.

The Kruskal-Wallis test answers a different question. It compares the distributions of the groups through ranks, and it fits an experiment whose claim is “values in one group tend to be higher than in another”. It is not a repair for an ANOVA whose residuals fail a normality test, because it no longer compares means.

Download group_expression.csv into your working directory. The invented table has 45 rows and two columns: group (control, drug_low, or drug_high) and expression, a log2 expression value. Each row is a different sample, and each group has 15 samples.

In R, the factor levels put the groups in dose order for every table and figure. rstatix reports on load that it masks filter() from the stats package, which this page does not use.

library(ggpubr)
library(rstatix)
samples <- read.csv("group_expression.csv")
dose_order <- c("control", "drug_low", "drug_high")
samples$group <- factor(samples$group, levels = dose_order)
dir.create("outputs", showWarnings = FALSE)
for (name in dose_order) {
values <- samples$expression[samples$group == name]
cat(sprintf("%-9s n = %d, mean = %.3f, SD = %.3f\n", name,
length(values), mean(values), sd(values)))
}
control n = 15, mean = 5.124, SD = 0.709
drug_low n = 15, mean = 5.232, SD = 0.681
drug_high n = 15, mean = 6.455, SD = 0.592

The control and low-dose means are close, 5.124 and 5.232, and the high-dose mean is more than one log2 unit above both. The three standard deviations are similar.

ANOVA splits the total variation in expression into a part between the group means and a part within the groups. Each part is a sum of squares, and dividing by its degrees of freedom gives a mean square. The F statistic is the ratio of the two mean squares. Under the null model of one common mean, F stays near 1.

fit <- aov(expression ~ group, data = samples)
summary(fit)
Df Sum Sq Mean Sq F value Pr(>F)
group 2 16.39 8.196 18.67 1.58e-06 ***
Residuals 42 18.43 0.439
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The groups account for a sum of squares of 16.39 on 2 degrees of freedom, and about 18.4 remains within the groups on 42. The mean squares are 8.196 and 0.439, and their ratio is F = 18.67, with p = 1.58e-06. Three means this far apart would rarely arise from one common mean. The F-test does not say which group differs, or by how much.

Classic one-way ANOVA pools the variance within the groups into one estimate, which assumes that the groups have similar variances. Welch’s ANOVA does not pool. It weights each group by its own variance and lowers the denominator degrees of freedom to match. It is oneway.test() in R and anova_oneway() with use_var="unequal" in statsmodels.

welch <- oneway.test(expression ~ group, data = samples)
cat(sprintf("Welch F = %.3f, df = %.0f and %.2f, p = %.3g\n",
welch$statistic, welch$parameter[1], welch$parameter[2],
welch$p.value))
Welch F = 20.209, df = 2 and 27.82, p = 3.8e-06

The three standard deviations here are 0.709, 0.681, and 0.592, and Welch’s test reads much like the classic one: F = 20.209 on 2 and 27.82 degrees of freedom, p = 3.8e-06. The two tests part ways when a group with a large variance also has few samples, where the classic p-value can come out too small. Choose between them from what you know about the assay before the analysis. A variance test with a large p-value does not show that the variances are equal.

Tukey’s HSD procedure estimates the difference in means for every pair of groups. It widens each interval so that all of them cover their true values together with 95% confidence, and it adjusts each p-value in the same way. Like the classic ANOVA, it uses the pooled variance.

tukey <- TukeyHSD(fit)$group
for (pair in rownames(tukey)) {
cat(sprintf("%-18s diff = %6.3f [%6.3f, %6.3f], p adj = %.3g\n", pair,
tukey[pair, "diff"], tukey[pair, "lwr"], tukey[pair, "upr"],
tukey[pair, "p adj"]))
}
drug_low-control diff = 0.108 [-0.480, 0.696], p adj = 0.896
drug_high-control diff = 1.331 [ 0.743, 1.919], p adj = 6.1e-06
drug_high-drug_low diff = 1.223 [ 0.635, 1.811], p adj = 2.62e-05

Each row is the higher dose minus the lower one. The high dose raises expression by 1.331 log2 units over control, with an interval from 0.743 to 1.919, and by 1.223 log2 units over the low dose, with an interval from 0.635 to 1.811. Both adjusted p-values are below 1e-04.

The low dose differs from control by 0.108 log2 units, with an adjusted p-value of 0.896. Its interval runs from −0.480 to 0.696, so it includes zero and it also includes an increase of more than half a log2 unit. “The low dose has no effect” would misread this row. Fifteen samples per group cannot separate a small increase from none.

A boxplot with one bracket per pair puts the three adjusted p-values on the data. In R, tukey_hsd() from rstatix returns the same comparisons as a table, add_xy_position() adds the bracket coordinates, and stat_pvalue_manual() draws them. The jitter entry of add.params keeps the jitter sideways, so each point stays at its measured value. In Python, one helper draws the boxes and the points, and a second draws a bracket for each pair.

group_colors <- c("#e07a5f", "#81b29a", "#4c72b0")
point_jitter <- position_jitter(width = 0.2, height = 0, seed = 1)
tukey_marks <- add_xy_position(tukey_hsd(samples, expression ~ group),
x = "group", step.increase = 0.25)
tukey_marks$label <- sprintf("p = %.2g", tukey_marks$p.adj)
tukey_plot <- ggboxplot(samples, x = "group", y = "expression",
fill = "group", palette = group_colors,
add = "jitter",
add.params = list(jitter = point_jitter)) +
stat_pvalue_manual(tukey_marks, label = "label", tip.length = 0.01) +
labs(x = NULL, y = "Expression (log2)",
title = "Tukey HSD adjusted p-values") +
theme(legend.position = "none")
ggsave("outputs/anova-boxplot-r.png", tukey_plot, width = 5, height = 4.6,
dpi = 150)

Boxplot of log2 expression for the control, low-dose, and high-dose groups, drawn with ggpubr, with each of the 15 samples per group as a point. Three brackets carry the Tukey adjusted p-values: 0.9 for low dose against control, 6.1e-06 for high dose against control, and 2.6e-05 for high dose against low dose.

The Kruskal-Wallis test ranks all 45 values together and asks whether the ranks are spread evenly across the groups. Its null model is that the three groups share one distribution. A small p-value says that values in at least one group tend to be higher or lower than in another. That becomes a statement about medians only when the group distributions have the same shape.

Choose this test when ranks carry the question of the experiment, as with an ordinal score, and make the choice before you see the ANOVA result.

rank_test <- kruskal.test(expression ~ group, data = samples)
cat(sprintf("Kruskal-Wallis H = %.3f, df = %.0f, p = %.3g\n",
rank_test$statistic, rank_test$parameter, rank_test$p.value))
Kruskal-Wallis H = 19.926, df = 2, p = 4.71e-05

H = 19.926 on 2 degrees of freedom gives p = 4.71e-05: the ranks are not spread evenly across the three groups. Like the F-test, this is one answer for all the groups, and it does not name a pair. It also gives no estimate of size. For a pair of groups, the estimate that goes with a rank test is the probability that a sample from one group exceeds a sample from the other, which the two-group page computes.

The same boxplot can carry the Kruskal-Wallis p-value. stat_compare_means() runs the test and writes the label in R. The Python code reuses the boxplot helper and writes the label with text().

rank_plot <- ggboxplot(samples, x = "group", y = "expression",
fill = "group", palette = group_colors,
add = "jitter",
add.params = list(jitter = point_jitter)) +
stat_compare_means(method = "kruskal.test") +
labs(x = NULL, y = "Expression (log2)", title = "Kruskal-Wallis test") +
theme(legend.position = "none")
ggsave("outputs/kw-boxplot-r.png", rank_plot, width = 5, height = 4,
dpi = 150)

The same boxplot of the control, low-dose, and high-dose groups, drawn with ggpubr. A label in the top left corner gives the Kruskal-Wallis p-value, 4.7e-05.

The F-test and the Kruskal-Wallis test both return small p-values here, and they answer different questions, one about means and one about ranks. Agreement between them says nothing about the design. Pick the test from the question, run that one, and report it.

Do not replace the F-test with a t-test on every pair. Three groups give 3 pairs and five groups give 10. If the 10 tests were independent and each ran at the 5% level, the chance of at least one false positive would be 1 − 0.95^10 = 0.40. Pairwise tests that share groups are not independent, so the true rate is lower than 40%, but it stays several times the 5% that each test claims.

Do not check normality on the pooled expression values. The assumptions concern the errors within each group, and three groups with different means form a mixture that is not normal even when each group is. The assumption diagnostics page inspects residuals instead.

Do not count measurements that are not independent samples. Three wells from one culture are one experimental unit, and an ANOVA that treats them as three samples returns intervals that are too narrow.

One gene needs one ANOVA. An ANOVA for each gene of a transcriptome produces thousands of p-values, and they need the correction that the multiple testing page describes.

Give the F-test with both degrees of freedom, then the pairwise differences with their intervals in the units of the measurement: “Expression differed across the three groups (one-way ANOVA, F(2, 42) = 18.67, p = 1.6e-06). The high dose raised expression by 1.33 log2 units over control (Tukey 95% CI 0.74 to 1.92) and by 1.22 log2 units over the low dose (0.64 to 1.81). The low dose differed from control by 0.11 log2 units (−0.48 to 0.70); 15 samples per group.”

Welch, 1951 describes the comparison of several means without the assumption of equal variances, the test that oneway.test() runs.

Kruskal and Wallis, 1952 describe the use of ranks in a one-way analysis of several groups.

The post hoc tests page goes further into pairwise comparisons after an ANOVA, including comparisons against one control and procedures for unequal variances.

The R documentation of aov() explains how the function fits the model and why it is designed for balanced designs. The documentation of TukeyHSD() describes the simultaneous intervals that this page prints, and the documentation of oneway.test() describes the var.equal argument that switches between Welch’s test and the classic one.

The statsmodels documentation of anova_oneway() lists the variance options of the Python test, and the SciPy documentation of tukey_hsd() describes the result object whose matrices this page indexes.

The multi-group figure page draws several groups with their estimates and intervals.