Tutorials › Bioavailability and Bioequivalence › Hand-Calculating a Balanced Crossover ANOVA

Log-Scale Analysis and ANOVA · Tutorial 204 of 399

Hand-Calculating a Balanced Crossover ANOVA

Work from a balanced two-sequence dataset to calculate log-scale crossover contrasts and assemble and check the classical ANOVA table.

Intermediate 12 min read

What You'll Learn

  • Define subject means and half-contrasts for a balanced two-period, two-sequence crossover.
  • Calculate treatment and period estimates with explicit sequence and period signs.
  • Partition variation into sequence, subject-within-sequence, period, treatment, and residual sums of squares.
  • Check the ANOVA degrees of freedom against the number of observations.
  • Select the classical denominator mean square for each F statistic and recognize scaling errors.
  • Distinguish a treatment-effect test from a bioequivalence conclusion.

From Log Exposures to an ANOVA Table

In a balanced two-sequence, two-period crossover, every subject receives both formulations, but the order differs by sequence. That structure permits a hand calculation of treatment and period contrasts while accounting separately for sequence and between-subject variation. Working through the sums of squares is also a useful safeguard: a factor-of-two error in a within-subject contrast can produce a factor-of-four error in its sum of squares and a misleading F statistic.

Let \(Y_{ij1}\) and \(Y_{ij2}\) denote the log exposure for subject \(j\) in sequence \(i\), in periods 1 and 2. Sequence TR means Test in period 1 and Reference in period 2; RT means Reference in period 1 and Test in period 2. Assume \(n\) subjects are observed in each sequence, with no missing periods. The response is a log-transformed exposure, such as log AUC, so contrasts are in log units.

Definition: For each subject, the subject mean is \(M_{ij}=(Y_{ij1}+Y_{ij2})/2\), and the half-contrast is \(q_{ij}=(Y_{ij1}-Y_{ij2})/2\). The half-contrast retains the period-1-minus-period-2 sign but is half the full difference.

The half-contrast is especially useful because its sequence-specific mean has a direct interpretation. Write \(\tau=\mu_T-\mu_R\) for the Test-minus-Reference treatment effect on the log scale, and \(\pi=\mu_{P1}-\mu_{P2}\) for the period-1-minus-period-2 effect. Under an additive model, the expected half-contrasts are \((\tau+\pi)/2\) in TR and \((-\tau+\pi)/2\) in RT. The treatment order reverses the treatment sign in the second sequence, while the period sign does not.

$$ \hat{\tau}=\bar q_{TR}-\bar q_{RT}, \qquad \hat{\pi}=\bar q_{TR}+\bar q_{RT} $$

Here \(\bar q_{TR}\) and \(\bar q_{RT}\) are the mean half-contrasts within their respective sequences. Equivalently, if \(\bar D_{TR}\) and \(\bar D_{RT}\) are the sequence means of the full differences \(D=Y_1-Y_2\), then \(\hat{\tau}=(\bar D_{TR}-\bar D_{RT})/2\) and \(\hat{\pi}=(\bar D_{TR}+\bar D_{RT})/2\). These signs depend on defining the full difference as period 1 minus period 2.

Assumptions and the Classical Model

The calculation below is for a complete, balanced 2-by-2 crossover: each subject has both period observations, both sequences contain the same number of subjects, and treatment allocation to sequence is randomized. The classical ANOVA assumes independent subjects, normally distributed residual errors with common variance, and an additive mean structure for sequence, subject within sequence, period, and treatment. It also presumes that carryover is not distorting the period-specific responses; in practice, the design and washout must support that premise. This two-period design does not provide a separate, generally reliable carryover estimate.

A standard model representation is \(Y=\) overall mean + sequence effect + subject-within-sequence effect + period effect + treatment effect + error. The subject term accounts for persistent differences between people. The treatment and period estimates come from within-subject contrasts, rather than from comparing the raw means of Test and Reference observations as if they were independent groups.

Key result: In a balanced two-sequence crossover, the classical sums of squares can be calculated from subject means and half-contrasts. With \(n\) subjects per sequence, \(SS_{\text{treatment}}=n\hat{\tau}^{\,2}\), \(SS_{\text{period}}=n\hat{\pi}^{\,2}\), and \(SS_{\text{residual}}=2\sum_i\sum_j(q_{ij}-\bar q_i)^2\). Here \(i\) indexes the two sequences.

Worked Example: Recovering Treatment and Period Effects

Suppose hypothetical log-exposure data give mean full differences \(Y_1-Y_2\) of 0.08333 in TR and -0.06667 in RT. Since TR receives Test in period 1, its positive difference combines a positive Test effect and a period-1 effect. In RT the treatment order reverses.

First convert full differences to half-contrast means: \(\bar q_{TR}=0.08333/2=0.041665\), and \(\bar q_{RT}=-0.06667/2=-0.033335\). Using the unrounded values in the calculations,

$$ \hat{\tau}=0.041665-(-0.033335)=0.07500, \qquad \hat{\pi}=0.041665+(-0.033335)=0.008330 $$

The estimated Test-minus-Reference difference is 0.07500 log units, and the estimated period-1-minus-period-2 difference is 0.008330 log units. The latter is not the treatment estimate: averaging the sequence contrasts isolates period, whereas subtracting them isolates treatment. These are log-scale estimates; exponentiating a treatment estimate gives an estimated geometric mean ratio.

Hand Calculation of Each ANOVA Component

For each sequence, calculate the mean of the subject means, denoted \(\bar M_i\), and the overall mean of all subject means, \(\bar M\). The sequence sum of squares is the variation between the two sequence means, counting each subject mean twice because each subject contributes two observations. The subject-within-sequence sum of squares measures how individual subject means vary around their own sequence mean.

$$ SS_{\text{sequence}}=2n\sum_i(\bar M_i-\bar M)^2 =n(\bar M_{TR}-\bar M_{RT})^2, \qquad SS_{\text{subject(sequence)}}=2\sum_i\sum_j(M_{ij}-\bar M_i)^2 $$

The treatment and period sums of squares use the orthogonal contrasts above. The residual sum of squares is based on deviations of half-contrasts from their sequence-specific means. The factor 2 appears because each such contrast deviation corresponds to two opposite-signed residuals in the original pair of period observations.

$$ SS_{\text{treatment}}=n(\bar q_{TR}-\bar q_{RT})^2, \quad SS_{\text{period}}=n(\bar q_{TR}+\bar q_{RT})^2, \quad SS_{\text{residual}}=2\sum_i\sum_j(q_{ij}-\bar q_i)^2 $$

The degrees of freedom are 1 for sequence, 1 for period, 1 for treatment, and \(2(n-1)\) each for subject(sequence) and residual. There are \(2n\) subjects and \(4n\) observations, so total degrees of freedom are \(4n-1\). The components add to this total, which is an important arithmetic check.

Worked Example: A Complete Balanced Crossover ANOVA

The following are hypothetical log-exposure values for six subjects, three in each sequence. All entries are in log units. \(M\) is the subject mean and \(D\) is period 1 minus period 2.

SequenceSubjectPeriod 1 treatmentPeriod 1 log exposurePeriod 2 treatmentPeriod 2 log exposure\(M\)\(D\)
TR1T2.0500R1.95002.00000.1000
TR2T2.0450R1.99502.02000.0500
TR3T2.0801R1.98012.03010.1000
RT4R1.9700T2.07002.0200-0.1000
RT5R2.0050T2.05502.0300-0.0500
RT6R2.0249T2.07492.0499-0.0500

The TR subject-mean average is \((2.0000+2.0200+2.0301)/3=2.0167\); the RT average is \((2.0200+2.0300+2.0499)/3=2.0333\). Thus the sequence contrast, calculated from these displayed averages, is \(2.0167-2.0333=-0.0166\) log units. The overall average is 2.0250.

The half-contrast means are \(\bar q_{TR}=(0.0500+0.0250+0.0500)/3=0.04167\) and \(\bar q_{RT}=(-0.0500-0.0250-0.0250)/3=-0.03333\). Therefore \(\hat{\tau}=0.04167-(-0.03333)=0.07500\) log units and \(\hat{\pi}=0.04167+(-0.03333)=0.008333\) log units.

For treatment, \(SS_{\text{treatment}}=3(0.07500)^2=0.016875\). For period, \(SS_{\text{period}}=3(0.008333)^2=0.0002083\). For sequence, \(SS_{\text{sequence}}=3(-0.0166)^2=0.0008267\).

For subject(sequence), the TR deviations from 2.0167 are -0.0167, 0.0033, and 0.0134; their squared deviations sum to 0.00046934. The RT deviations from 2.0333 are -0.0133, -0.0033, and 0.0166; their squared deviations sum to 0.00046334. Hence \(SS_{\text{subject(sequence)}}=2(0.00046934+0.00046334)=0.001865\).

For residual error, use the half-contrasts. In TR, \(q\) values are 0.0500, 0.0250, and 0.0500, with mean 0.04167; squared deviations sum to 0.0004167. In RT, \(q\) values are -0.0500, -0.0250, and -0.0250, with mean -0.03333; squared deviations also sum to 0.0004167. Thus \(SS_{\text{residual}}=2(0.0004167+0.0004167)=0.001667\).

The resulting ANOVA table is:

SourcedfSSMSF
Sequence10.00082670.00082671.773
Subject(sequence)40.0018650.0004663—
Period10.00020830.00020830.5000
Treatment10.0168750.01687540.50
Residual40.0016670.0004167—
Total110.021442——

For example, \(F_{\text{treatment}}=0.016875/0.0004167=40.50\), with 1 and 4 degrees of freedom. The period statistic is \(0.0002083/0.0004167=0.5000\). The sequence statistic uses subject(sequence) as its denominator: \(0.0008267/0.0004663=1.773\). The SS values sum to 0.021442, and the degrees of freedom sum to 11, matching 12 observations. These checks support the arithmetic, not the model assumptions.

Deriving the Residual Scaling and Choosing Denominators

The residual scaling follows directly from the two observations for each subject. After fitting the sequence-specific mean half-contrast, let \(e=q_{ij}-\bar q_i\). The fitted period-treatment contrast leaves residuals \(e\) and \(-e\) in the two periods. Their squared contributions sum to \(e^2+(-e)^2=2e^2\). Summing over subjects gives \(SS_{\text{residual}}=2\sum e^2\). If full differences \(D=Y_1-Y_2\) are used instead, then \(e=(D-\bar D_i)/2\), and the equivalent formula is \(\tfrac12\sum(D-\bar D_i)^2\).

The mean squares are each sum of squares divided by its degrees of freedom. In this classical balanced analysis, treatment and period are compared with the residual mean square; sequence is compared with the subject(sequence) mean square because sequence varies between subjects. This denominator structure is part of the model, not an arbitrary choice made after seeing the data.

1
Organize the data.
Confirm two observations per subject, the correct treatment order, and equal numbers in TR and RT. Record log exposures, not raw exposures.
2
Calculate subject summaries.
For each person calculate \(M=(Y_1+Y_2)/2\), \(D=Y_1-Y_2\), and \(q=D/2\). Keep the sign convention consistent.
3
Estimate the contrasts and sums of squares.
Use the sequence means of \(q\) for treatment and period, subject means for sequence and subject(sequence), and centered \(q\) values for residual error.
4
Complete and verify the table.
Calculate mean squares and F ratios with the specified denominators. Check that all SS components and degrees of freedom add to their totals.

Worked Example: Detecting a Contrast-Scaling Error

Suppose three subjects in each sequence have full-difference deviations from their respective sequence means of 0.01667, -0.03333, and 0.01667 in TR, and -0.03333, 0.01667, and 0.01667 in RT. The squared deviations sum to approximately 0.001667 within each sequence.

Those are deviations of \(D=Y_1-Y_2\), not of the half-contrast \(q=D/2\). The \(q\) deviations are half as large, so their squared deviations sum to one quarter as much: \(0.001667/4=0.0004167\) per sequence. The correct residual sum of squares is therefore \(2(0.0004167+0.0004167)=0.001667\), not 0.006667.

If the treatment sum of squares is 0.016875, the correct residual mean square with 4 residual degrees of freedom is \(0.001667/4=0.0004167\), giving \(F=0.016875/0.0004167=40.50\). Using the incorrectly inflated residual SS would divide the F statistic by four. A simple prevention is to write down whether each deviation is based on \(D\) or \(q\) before squaring it.

Diagnostics, Alternatives, and Limits

Several arithmetic and data checks are useful before interpreting the table. Verify sequence labels against actual treatment order, recompute \(M\), \(D\), and \(q\) from the period records, and confirm the half-contrast sign. Check that every subject contributes two observations and that the sequence sizes match. A disagreement between the sums of squares and the total, or between total degrees of freedom and the number of observations, signals an error in the calculations or data organization.

Model diagnostics concern the residuals and design assumptions, not just whether the ANOVA arithmetic balances. Look for unusually large residuals, unequal residual spread by formulation or period, and patterns suggesting nonnormality or subject influence. These can undermine the standard error and F-test calibration. A balanced design makes the hand calculations transparent, but it does not make the assumptions automatically true.

The paired t-test gives a direct estimate and test of the treatment contrast in a two-period crossover under the corresponding balanced assumptions; its treatment estimate agrees with the contrast calculation here. The ANOVA additionally displays sequence, period, subject(sequence), and residual components. A mixed-effects model is often more flexible for missing observations or more complex structures, but its software output requires careful identification of the fitted model and denominator degrees of freedom. The hand ANOVA is not a replacement for a model suited to an incomplete or unbalanced design.

Finally, a large or small treatment F statistic is not itself a bioequivalence decision. It tests a null hypothesis of zero treatment difference under its model; equivalence requires prespecified margins and an equivalence procedure. Nor does a nonsignificant sequence test establish that sequence effects are absent. The sequence term is a between-subject comparison and can be imprecise, especially in a small study.

Common Pitfalls

  • Mixing full differences and half-contrasts. Squaring deviations from \(D\) while applying the formula for \(q\) makes the residual SS four times too large. Either use \(2\sum(q-\bar q_i)^2\) or \(\tfrac12\sum(D-\bar D_i)^2\).
  • Reversing the RT sign. RT has Reference in period 1, so the treatment contribution to \(Y_1-Y_2\) has the opposite sign from TR. Use the stated formulas rather than treating both sequences as if they had the same order.
  • Using the wrong F denominator. The classical sequence F ratio uses subject(sequence) MS; treatment and period use residual MS.
  • Confusing total sample size with subjects per sequence. With \(n\) per sequence there are \(2n\) subjects and \(4n\) observations, not \(4n\) subjects.
  • Calling a treatment test an equivalence test. A test against zero does not establish that the treatment difference lies within clinically or regulatorily specified equivalence limits.
Key takeaway: Preserve the sequence and period signs, distinguish \(D\) from \(q=D/2\), and verify both the SS and df totals. Those checks make a hand-calculated balanced crossover ANOVA auditable and help prevent a scaling error from changing the inferential result.

Check Your Understanding

Answer each question, then compare your work with the solutions.

  1. If \(\bar q_{TR}=0.030\) and \(\bar q_{RT}=-0.010\), what are the treatment and period estimates, and what are their units?
  2. There are 5 subjects in each sequence. How many subjects and observations are there, and what are the total, subject(sequence), and residual degrees of freedom?
  3. With \(n=4\), \(\hat{\tau}=0.05\) log units, and \(SS_{\text{residual}}=0.006\), calculate treatment SS, residual MS, and the treatment F statistic.
  4. Why is a residual SS calculated from full-difference deviations one quarter of the corresponding sum of squared deviations calculated from those same deviations after conversion to half-contrasts?

Solutions. (1) \(\hat{\tau}=0.030-(-0.010)=0.040\) log units and \(\hat{\pi}=0.030+(-0.010)=0.020\) log units. (2) There are \(2(5)=10\) subjects and \(4(5)=20\) observations; total df is 19, subject(sequence) df is \(2(5-1)=8\), and residual df is also 8. (3) \(SS_{\text{treatment}}=4(0.05)^2=0.010\); residual \(MS=0.006/6=0.001\), since residual df is \(2(4-1)=6\); hence \(F=0.010/0.001=10.00\) on 1 and 6 df. (4) Each half-contrast deviation is half the full-difference deviation, and squaring a halved value multiplies its square by \(1/4\).