Skip to content

BCompare groups · Guide test 019

ANCOVA F tests

Test group effects or covariate terms in a linear model adjusting for other predictors.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

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

3
12
0.60
0.90

The group difference that remains at equal covariate values.

0.60

Groups that differ in the covariate: the unadjusted means mix this in.

0.050

Your data

Simulated with a known adjusted effect
Groupxy
Group 16.9874.972
Group 15.9015.015
Group 13.4352.615
Group 12.4444.323
Group 15.3073.380
Group 16.2395.272
Group 15.1605.146
Group 15.1975.406
Group 14.1044.340
Group 17.7304.373
Group 12.7263.478
Group 15.9452.757

3 groups of 12 with a covariate x · first 12 of 36 rows

If H₀ were true

F = 6.47
  1. The reference: how F varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .004).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

F=(RSSx−RSSx+g)/(k−1)RSSx+g/(N−k−1)F = \dfrac{(\mathrm{RSS}_{x} - \mathrm{RSS}_{x+g})/(k-1)}{\mathrm{RSS}_{x+g}/(N-k-1)}

With your numbers

F=(46.22−32.91)/232.91/32=6.47F = \dfrac{(46.22 - 32.91)/2}{32.91/32} = 6.47
RSSx\mathrm{RSS}_{x}
residual sum of squares with the covariate only
RSSx+g\mathrm{RSS}_{x+g}
residual sum of squares after adding the groups
bb
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 H0.

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

y=Xβ+ε,F=(RSSrestricted−RSSfull)/qRSSfull/(N−p)y=X\beta+\varepsilon,\qquad F=\frac{(\mathrm{RSS}_{\mathrm{restricted}}-\mathrm{RSS}_{\mathrm{full}})/q}{\mathrm{RSS}_{\mathrm{full}}/(N-p)}

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.

Python · native

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
R

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 ‘ ’ 1

Sources

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.