Skip to main content
v2026.11,986 entries · CC-BY 4.0

Nested ANOVA: Formula, Degrees of Freedom and Example

How a nested ANOVA works: formula, degrees of freedom, a worked example, interpretation, APA reporting and code.

Written and maintained by CASRAI Editorial Board

Published

On this page

A nested ANOVA is an analysis of variance for data in which the levels of one factor (the nested factor, B) occur within only one level of another factor (the main factor, A), so that B cannot be crossed with A. It tests whether the means differ between A levels and whether they differ between the B levels inside each A level. The key rule is the F test denominator: A is tested against the mean square of B within A, not against the residual error.

What does a nested ANOVA test?

A nested ANOVA asks two questions at once: do the A groups differ, and is there variation among the B units inside each A group? The NIST/SEMATECH e-Handbook describes the nested layout as one where fewer than all levels of one factor occur within each level of the other factor, and its example is lots drawn from a production run with units taken from each lot. Because each B level belongs to exactly one A level, the A-by-B interaction cannot be estimated.

The model in the handbook is yijk = μ + αi + βj(i) + εijk. The subscript j(i) is read “j within i“: vial 1 under batch 1 is a different vial from vial 1 under batch 2, even if both carry the same label.

Nested versus crossed factors

In a crossed design every level of A appears with every level of B, so an interaction can be estimated, as in a factorial analysis of variance. In a nested design each B level appears under one A level only. Typical cases are sub-samples within a specimen, technicians within a laboratory, or readings from several vials made from one batch. GraphPad Prism calls the one-factor-plus-nested-factor version a nested one-way ANOVA, and its documentation notes that the residuals are often technical-replicate variation; the NIST handbook calls the same structure a two-way nested ANOVA, and other texts call it hierarchical ANOVA. These are names for one family of designs, not different tests.

A nested design is not a split-plot design. In a split-plot, the sub-plot factor is crossed with the whole-plot factor; in a nested design the lower-level units are simply different units under each higher-level unit.

Nested ANOVA formula and degrees of freedom

With I levels of A, J levels of B within each A level and K replicate readings per B level, the NIST handbook gives these sums of squares. Here a bar over y with a dot means “averaged over that subscript”: ȳi.. is the mean of A level i, ȳij. the mean of B level j within A level i, and ȳ… the grand mean.

  • SSA = JK Σi (ȳi.. − ȳ…)², with I − 1 degrees of freedom.
  • SSB(A) = K Σi Σj (ȳij. − ȳi..)², with I(J − 1) degrees of freedom.
  • SSE = Σ Σ Σ (yijk − ȳij.)², with IJ(K − 1) degrees of freedom.
  • Total degrees of freedom are IJK − 1, and each mean square is the sum of squares divided by its degrees of freedom.

The F ratios in the NIST table are FA = MSA / MSB(A) and FB(A) = MSB(A) / MSE. LibreTexts explains why from the expected mean squares when both factors are random: the expected value of MSA contains σ² plus a term for B(A) variance plus a term for A variance, and the expected value of MSB(A) contains σ² plus the B(A) term. Dividing MSA by MSB(A) therefore cancels everything except the A variance; dividing by MSE would not.

The data table below sets the degrees of freedom and the example values side by side.

Worked example with the arithmetic shown

This is an illustrative composite, not data from a real study. Suppose three batches of a reagent (A, I = 3), two vials drawn from each batch (B within A, J = 2) and three concentration readings per vial (K = 3).

Batch Vial Readings Vial mean Batch mean
1 1 10.1, 10.3, 10.2 10.2 10.6
1 2 10.9, 11.1, 11.0 11.0
2 1 12.0, 12.2, 12.1 12.1 12.3
2 2 12.4, 12.6, 12.5 12.5
3 1 9.8, 9.9, 10.0 9.9 10.1
3 2 10.2, 10.4, 10.3 10.3

The grand mean is 11.0. Applying the formulas above:

  • SSA = 2 × 3 × [(10.6 − 11)² + (12.3 − 11)² + (10.1 − 11)²] = 6 × (0.16 + 1.69 + 0.81) = 6 × 2.66 = 15.96, df = 2.
  • SSB(A) = 3 × [(10.2 − 10.6)² + (11.0 − 10.6)² + (12.1 − 12.3)² + (12.5 − 12.3)² + (9.9 − 10.1)² + (10.3 − 10.1)²] = 3 × (0.16 + 0.16 + 0.04 + 0.04 + 0.04 + 0.04) = 3 × 0.48 = 1.44, df = 3 × (2 − 1) = 3.
  • SSE = the squared deviations of each reading from its vial mean, which are 0.01 + 0 + 0.01 for every vial, so 6 × 0.02 = 0.12, df = 3 × 2 × (3 − 1) = 12.
  • Check: 15.96 + 1.44 + 0.12 = 17.52, which equals the total sum of squares about the grand mean (df = 18 − 1 = 17, and 2 + 3 + 12 = 17).

The mean squares are MSA = 15.96 / 2 = 7.98, MSB(A) = 1.44 / 3 = 0.48 and MSE = 0.12 / 12 = 0.01. Then FA = 7.98 / 0.48 = 16.625 on (2, 3) df, p = .024, and FB(A) = 0.48 / 0.01 = 48.0 on (3, 12) df, p < .001. The 5% critical values are 9.55 for F(2, 3) and 3.49 for F(3, 12), so both effects are significant at α = .05.

Had we wrongly divided MSA by MSE, F would have been 798 on (2, 12) df. That overstates the evidence, because it ignores that the only independent replicates of a batch are the vials, not the individual readings.

How to interpret a nested ANOVA

Result Reading
FA significant, FB(A) not significant The A groups differ and the nested units within groups are not shown to differ. The NIST machine/operator example has this pattern: machine significant, operator within machine not.
FA not significant, FB(A) significant The main groups are not shown to differ, but the nested units vary within them. Most of the variation sits at the lower level.
Both significant Both levels contribute variation, as in the example above: batches differ, and vials within a batch also differ.
Neither significant No evidence of differences at either level at the chosen α.

The F test for A has I − 1 and I(J − 1) degrees of freedom, so with few B units the test is low-powered even when many readings were taken. In the example the numerator has 2 df but the denominator only 3, which is why the critical value is 9.55 rather than the 3.89 that F(2, 12) would give. The number of independent B units, not the number of readings, controls the power for A.

Assumptions and common mistakes

The NIST handbook states that the data must be adequately described by the model and that errors are random, Gaussian, with fixed location and spread. GraphPad’s checklist adds that the residuals should be Gaussian and the standard deviation should be the same in each subcolumn; Prism does not test this, so inspect the data, and a log transform may help when spread grows with the mean. Check the same things in the residuals of any software you use; the assumptions of two-way ANOVA carry over.

  • Using the residual as the denominator for A. When B(A) is random, A is tested against MSB(A). Software that reports a single residual-based table for an A/B formula gives the wrong F for A.
  • Treating readings as independent units. Repeated readings from one vial are replicates of that vial; they belong in the error term, not in the count of independent units for A. Counting them as independent replicates inflates the denominator degrees of freedom (pseudoreplication).
  • Fixed versus random B. GraphPad Prism treats the main factor as fixed and the nested factor as random and cannot run when the main factor is random. If A is random too, use a mixed-model or variance-components tool; see mixed-effects models: random versus fixed effects.
  • Labelling B levels 1, 2 across groups and crossing them. Vial 1 under batch 1 is a different unit from vial 1 under batch 2; code the unit so the software nests it instead of treating it as crossed.
  • Looking for an interaction. It cannot be estimated in a nested layout.

How to report a nested ANOVA (APA example)

Report each F test with its degrees of freedom and exact p, and say which term served as the denominator. For the example:

“A nested ANOVA with vials nested within batches showed a significant effect of batch, F(2, 3) = 16.63, p = .024, and of vial within batch, F(3, 12) = 48.00, p < .001. The batch effect was tested against the vial-within-batch mean square."

Also report the number of units at each level (3 batches, 6 vials, 18 readings) so a reader can judge the power for the main factor.

Nested ANOVA in Excel, R and Python

Nested ANOVA in Excel

Excel’s Analysis ToolPak has no nested ANOVA, so compute the sums of squares directly. With the example laid out so that batch means, vial means and readings are in ranges: SSA is =J*K*DEVSQ(batch_means); SSB(A) is =K*(DEVSQ(vial_means_batch1)+DEVSQ(vial_means_batch2)+DEVSQ(vial_means_batch3)); SSE is the sum of DEVSQ over each vial’s readings. Then =F.DIST.RT(F, df1, df2) gives p. For the example, 6*DEVSQ of (10.6, 12.3, 10.1) returns 15.96. For the one-way case see ANOVA in Excel.

Nested ANOVA in R

Fit fit = aov(y ~ A + A:B, data = d), with A and B stored as factors, and read the mean squares from summary(fit). The table tests every term against the residual, so for random B compute FA yourself: ms = summary(fit)[[1]][['Mean Sq']]; F_A = ms[1]/ms[2]; pf(F_A, 2, 3, lower.tail = FALSE). More on the function is in running ANOVA in R.

Nested ANOVA in Python

The following uses NumPy and SciPy and reproduces the example above (FA = 16.625, p ≈ .0238; FB(A) = 48.0, p ≈ 5.9 × 10−7):

import numpy as npfrom scipy.stats import f# data[i][j] = the K readings for B level j inside A level idata = np.array([    [[10.1, 10.3, 10.2], [10.9, 11.1, 11.0]],    [[12.0, 12.2, 12.1], [12.4, 12.6, 12.5]],    [[9.8, 9.9, 10.0], [10.2, 10.4, 10.3]],])I, J, K = data.shapegrand = data.mean()a_means = data.mean(axis=(1, 2))b_means = data.mean(axis=2)ss_a = J * K * np.square(a_means - grand).sum()ss_b = K * np.square(b_means - a_means[:, None]).sum()ss_e = np.square(data - b_means[:, :, None]).sum()df_a, df_b, df_e = I - 1, I * (J - 1), I * J * (K - 1)ms_a, ms_b, ms_e = ss_a / df_a, ss_b / df_b, ss_e / df_ef_a = ms_a / ms_b          # A is tested against B(A), not against errorf_b = ms_b / ms_eprint(round(f_a, 3), f.sf(f_a, df_a, df_b))print(round(f_b, 2), f.sf(f_b, df_b, df_e))

Frequently asked questions

Why use an ANOVA instead of a t test?

Because comparing three or more groups with repeated t tests inflates the chance of a false positive. If three pairwise tests at α = .05 were independent, the chance of at least one false positive would be 1 − 0.95³ ≈ 14%, and with ten tests about 40%. ANOVA tests the means of two or more groups with one omnibus F test, as the NIST handbook describes it, and a nested ANOVA adds the structure of units within groups, which a t test cannot represent.

ANOVA and regression are both linear models. An ANOVA is a linear model whose predictors are categorical group indicators, and the F test compares explained to unexplained variation, as regression does. Nested ANOVA is the same linear model with the nested factor coded within each level of the main factor.

Is ANOVA outdated?

No. The NIST/SEMATECH e-Handbook still presents ANOVA as the standard procedure for comparing means across groups. What has changed is that mixed models handle unbalanced nested data, several random factors and non-Gaussian responses more flexibly. Software such as statsmodels documents linear mixed effects models for this purpose, and a nested ANOVA is the balanced special case that can be solved with the sums of squares above.

What does a nested ANOVA test?

It tests two null hypotheses: that the A level means are equal, and that the B units within each A level contribute no extra variance beyond error. A is tested against MSB(A) and B(A) against MSE.

Sources

Data: Nested two-level ANOVA: degrees of freedom, sums of squares and F denominators

Compiled for casrai.org from the NIST/SEMATECH two-way nested ANOVA table and the LibreTexts expected-mean-square derivation. Each row gives the source of variation, its degrees of freedom formula, the F denominator when B within A is random, and the values for an illustrative composite example with I=3, J=2, K=3 (sums of squares and mean squares computed by the writer).

Nested two-level ANOVA: degrees of freedom, sums of squares and F denominators
Source of variationDegrees of freedom formulaDegrees of freedom in exampleSum of squares in exampleMean square in exampleF denominator (B within A random)
A (main factor)I - 1215.967.98MS B(A)
B within A (nested factor)I(J - 1)31.440.48MS E
Error (residual)IJ(K - 1)120.120.01not applicable
TotalIJK - 11717.52not applicable

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about Nested ANOVA: Formula, Degrees of Freedom and Example

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →