R/check_test_assumptions.R
check_test_assumptions.RdFor a one-way, independent-groups design
(outcome ~ group), check the two assumptions that decide which family
of tests is appropriate — normality (Shapiro-Wilk, per group) and
homogeneity of variance (Levene) — and return the verdicts together with
the recommended omnibus and post-hoc test:
each group normal and equal variances: anova_test() +
tukey_hsd();
each group normal but unequal variances: welch_anova_test()
+ games_howell_test();
at least one group not normal: kruskal_test() + dunn_test().
The result is a tidy one-row tibble, so the same single assumption check can
drive both the omnibus and the post-hoc coherently — run the recommended
omnibus, then pass the result to posthoc_test() via its
.assumptions argument to avoid re-checking.
A note on choosing a test from the data. Selecting the test by first testing its assumptions on the same data is convenient but has a known cost: the assumption gate is least reliable exactly when it matters (Shapiro-Wilk has little power at small n and rejects trivial departures at large n), and conditioning the choice on it makes the p-value of the test finally run no longer the exact nominal quantity. A common alternative is to skip the gate and use a robust method unconditionally — Welch ANOVA with Games-Howell (which reduce to the classic result when variances are equal) or a rank-based test. Treat this recommendation as guidance, not a substitute for judgement.
See the Datanovia tutorial Statistical Tests and Assumptions in R for a worked walkthrough.
check_test_assumptions(data, formula, significance = 0.05)a one-row tibble with the columns .y. (the outcome),
normality.p (the smallest Shapiro-Wilk p across groups),
homogeneity.p (Levene's p), the logical verdicts normal and
equal.variance, the significance used, and the recommended
omnibus and posthoc test names.
posthoc_test(), shapiro_test(),
levene_test().
The Datanovia tutorial: Statistical Tests and Assumptions in R.
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df %>% check_test_assumptions(len ~ dose)
#> # A tibble: 1 × 8
#> .y. normality.p homogeneity.p normal equal.variance significance omnibus
#> <chr> <dbl> <dbl> <lgl> <lgl> <dbl> <chr>
#> 1 len 0.164 0.528 TRUE TRUE 0.05 anova_test
#> # ℹ 1 more variable: posthoc <chr>