BCompare groups · Guide test 019
ANCOVA F tests
Test group effects or covariate terms in a linear model adjusting for other predictors.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- ANCOVA (group means adjusted for covariates) — Do group means differ after adjusting for one or more numeric covariates?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · ANCOVA F tests
The group difference that remains at equal covariate values.
Groups that differ in the covariate: the unadjusted means mix this in.
Your data
Simulated with a known adjusted effect| Group | x | y |
|---|---|---|
| Group 1 | 6.987 | 4.972 |
| Group 1 | 5.901 | 5.015 |
| Group 1 | 3.435 | 2.615 |
| Group 1 | 2.444 | 4.323 |
| Group 1 | 5.307 | 3.380 |
| Group 1 | 6.239 | 5.272 |
| Group 1 | 5.160 | 5.146 |
| Group 1 | 5.197 | 5.406 |
| Group 1 | 4.104 | 4.340 |
| Group 1 | 7.730 | 4.373 |
| Group 1 | 2.726 | 3.478 |
| Group 1 | 5.945 | 2.757 |
3 groups of 12 with a covariate x · first 12 of 36 rows
If H₀ were true
- The reference: how F varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .004).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- residual sum of squares with the covariate only
- residual sum of squares after adding the groups
- the common within-group slope
The verdict
F(2, 32) = 6.47, p = .004, η²p = 0.29
p ≤ α = .050: reject H₀
Effect size: partial eta squared for the groups η²p = 0.288
Compares groups at the same covariate value, so groups that start at different x are not mistaken for different. It assumes parallel slopes: the homogeneity-of-slopes F checks that first.
- Statistic
- 6.474
- p-value
- 0.0044
ANCOVA F tests on 3 groups of 12 with a covariate x: F(2, 32) = 6.47, p = .004, η²p = 0.29. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: the selected group or covariate effects are zero after adjustment; H1: at least one is nonzero.
Assumptions
An appropriate linear mean relationship, independent errors, and classical normal equal variance errors for the usual F test. A common slope model assumes group by covariate interactions are absent.
Formula
Parameters and notation
X includes groups and covariates; q is the number of restrictions; p is the full model coefficient count. Adjusted means require explicit reference covariate values or a specified averaging population.
Structure of the method
Fit a linear model containing group indicators, covariates, and any scientifically required interactions, then compare a restricted contrast subspace with the full model. Adjusted group comparisons are linear-model hypotheses; an ANCOVA table does not itself define the covariate values or population over which adjusted means should be interpreted.
Estimated and nuisance parameters
Estimate covariate slopes, group contrasts, and residual variance. Reference covariate values, centering constants chosen from a target population, and marginal-mean weights are separate reporting choices. If slopes vary by group, the group effect is conditional on covariate value rather than one universal adjusted difference.
Settings and design choices
Prespecify baseline covariates, nonlinear terms, interactions, adjustment order or contrast matrix, significance level, and comparison family. Variable selection by p-value changes the inferential protocol. The supplied Type I examples place baseline first, so their group row is adjusted for baseline; reversing order changes the sequential hypothesis.
Fitting and adaptation
Refit all coefficients after changing adjustment variables and recalculate the target contrast. For a new target population, distinguish reweighting adjusted means from refitting the outcome model. Covariate adaptation is not neural fine-tuning: it requires a renewed estimand, overlap assessment, and appropriate uncertainty for any estimated weights.
Sensitivity and robustness
Poor covariate overlap makes adjusted means extrapolations. Measurement error in a covariate can leave residual confounding, and post-treatment adjustment can introduce bias. Nonlinearity or unequal group slopes can invalidate a common-slope interpretation even when the omnibus model fits reasonably. Missing covariates can change the analyzed population.
How to use and calibrate
Choose covariates and interactions from the design, fit the model, and test the targeted contrast using the partial F framework. Report adjusted differences and their covariate interpretation.
Example application
Compare treatment groups adjusting for a pretreatment baseline measurement; include an interaction if treatment effects plausibly depend on baseline.
Interpretation and pitfalls
Watch out
Adjustment is not automatically causal. Posttreatment covariates can change the estimand or introduce bias; poor covariate overlap makes adjusted comparisons extrapolations.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Sequential ANCOVA tests baseline first, then group; assumes common slopes unless an interaction is modeled.
Validated — Executed successfully in the validation environment · needs numpy, pandas, scipy, statsmodels
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
groups = [rng.normal(m,1,20) for m in (0,.4,.8)]
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols
d=pd.DataFrame({"y":np.concatenate(groups),"group":np.repeat(["A","B","C"],20),"baseline":rng.normal(size=60)})
print(sm.stats.anova_lm(ols("y~baseline+C(group)",d).fit(),typ=1))
Output
df sum_sq mean_sq F PR(>F) C(group) 2.0 21.860252 10.930126 12.065262 0.000044 baseline 1.0 1.380781 1.380781 1.524181 0.222143 Residual 56.0 50.731351 0.905917 NaN NaN
Validated — Executed successfully in the validation environment · needs Base/standard library
set.seed(2026)
d <- data.frame(value=c(rnorm(20),rnorm(20,.4),rnorm(20,.8)), group=factor(rep(c("A","B","C"),each=20)))
d$baseline <- rnorm(nrow(d)); print(anova(lm(value~baseline+group,d)))
Output
Analysis of Variance Table
Response: value
Df Sum Sq Mean Sq F value Pr(>F)
baseline 1 0.234 0.2339 0.2825 0.5971695
group 2 14.511 7.2554 8.7637 0.0004883 ***
Residuals 56 46.362 0.8279
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Sources
- R stats — R stats · anova.lmstat.ethz.ch
- anova.lmstat.ethz.ch
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 019, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT019.