One Way Within-Subjects ANOVA With Sphericity Diagnostics and Corrections
Source:R/mauchly_test.R
anova_within.RdPerforms the univariate one-way within-subjects F test together
with Mauchly's test of sphericity and the three standard
\(\varepsilon\)-corrected p-values (Greenhouse-Geisser,
Huynh-Feldt, and lower-bound). Returns everything in a single tidy
data.frame so the user can decide which adjustment to report.
Arguments
- x
Either an \(n \times k\) numeric matrix or
data.frame(rows = subjects, columns = repeated measurements); or a long-formatdata.frametogether withid,time, andoutcomecolumn names.- id
Column name in
xidentifying the subject whenxis in long format (NULLotherwise).- time
Column name in
xidentifying the within-subjects factor level whenxis in long format (NULLotherwise).- outcome
Column name in
xidentifying the dependent variable whenxis in long format (NULLotherwise).
Value
A data.frame with one row per reported F test:
adjustment ("none", "Greenhouse-Geisser",
"Huynh-Feldt", "lower_bound"), F_value,
df_1, df_2, p_value, and epsilon (the
correction factor used; NA for the unadjusted row).
attr(<output>, "mauchly") contains the row from
mauchly_test, and the partial \(\eta^2\) is attached
as attr(<output>, "partial_eta_squared").
Details
The unadjusted within-subjects F statistic is the same regardless of sphericity; corrections shrink the numerator and denominator degrees of freedom by a factor of \(\hat\varepsilon \in [1/(k - 1),\, 1]\), and the p-value is recomputed against the adjusted reference F distribution. When Mauchly's test rejects, prefer the Huynh-Feldt-corrected p-value (less conservative than Greenhouse-Geisser).
For multi-factor within-subjects designs or mixed designs, fit the
model with stats::aov(... + Error(id/within)) or with
lme4::lmer() directly.
References
Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing experiments and analyzing data: A model comparison perspective (4th ed.). Routledge. (See Chapter 11.)
See also
mauchly_test, epsilon_corrections,
aov
Other within-subjects analysis:
anova_within_two_way(),
epsilon_corrections(),
mauchly_test(),
pairwise_within(),
plot_trajectories_fitted()
Other hypothesis tests:
adjusted_means(),
ancova(),
ci_dunnett(),
ci_scheffe(),
ci_tukey_kramer(),
compare_cov_structures(),
contrast_test(),
correlations_test(),
equivalence_r(),
equivalence_smd(),
factorial_anova(),
manova_split_plot(),
mauchly_test(),
mixed_anova(),
obrien_test(),
pairwise_within(),
randomization_test(),
randomization_test_paired(),
regions_of_significance(),
simple_effects_AB(),
summary_t_test(),
welch_t()
Author
Ken Kelley kkelley@nd.edu
Examples
# Simulated within-subjects data with no real effect.
set.seed(113)
Y <- matrix(rnorm(20 * 4), nrow = 20)
anova_within(Y)
#> adjustment F_value df_1 df_2 p_value epsilon
#> none 0.618 3 57 0.6062 <NA>
#> Greenhouse-Geisser 0.618 2.36 44.9 0.5696 0.787
#> Huynh-Feldt 0.618 2.72 51.7 0.5911 0.906
#> lower_bound 0.618 1 19 0.4415 0.333
# Built-in within-subjects example: nlme::Orthodont (distance ~ age).
res <- anova_within(nlme::Orthodont,
id = "Subject", time = "age", outcome = "distance")
res
#> adjustment F_value df_1 df_2 p_value epsilon
#> none 38 3 78 < 0.0001 <NA>
#> Greenhouse-Geisser 38 2.63 68.4 < 0.0001 0.877
#> Huynh-Feldt 38 2.95 76.8 < 0.0001 0.984
#> lower_bound 38 1 26 < 0.0001 0.333
attr(res, "mauchly")
#> W statistic df p_value n_subjects n_levels method
#> 0.758 6.85 5 0.2326 27 4 Mauchly's test of sphericity
attr(res, "partial_eta_squared")
#> [1] 0.5940013