Skip to contents

Computes the classical F-ratios for one- and two-way ANOVA designs when one or more factors are random rather than fixed, using the expected-mean-square (EMS) rules that determine the correct denominator for each test (Searle, Casella, & McCulloch, 1992). The function is the closed-form alternative to fitting via lmer and is the standard treatment in classical psychometrics and design-of-experiments texts (Maxwell, Delaney, & Kelley, 2027, Ch. 10).

Usage

mixed_anova(
  data,
  outcome,
  factor_A,
  factor_B = NULL,
  A_type = c("fixed", "random"),
  B_type = c("random", "fixed")
)

Arguments

data

A data.frame containing the response, the factor(s), and (when both factors are crossed) the subject identifier.

outcome

Character name of the response column.

factor_A

Character name of factor A.

factor_B

Character name of factor B, or NULL for one-way designs. Default NULL.

A_type

One of "fixed" (default) or "random": the EMS classification of factor A.

B_type

One of "fixed" or "random" (default): the EMS classification of factor B. Ignored when factor_B is NULL.

Value

A data.frame with one row per testable effect. Columns: effect, ss, df, ms, denominator, F_value, p_value.

Details

One way design (only factor_A).

  • Both A fixed and A random use the same observed F-ratio \(MS_A / MS_{\mathrm{within}}\); the test of "is there an effect of A?" is identical. The interpretation differs: the random-effects test asks whether the variance component \(\sigma^2_A\) is zero.

Two-way design, both fixed (Model I).

  • \(F_A = MS_A / MS_{AB}\) (when interaction is present in the model and treated as error) or \(F_A = MS_A / MS_{\mathrm{within}}\) (when interaction is pooled into error). The function uses \(MS_{\mathrm{within}}\) as the denominator throughout for Model I.

Two-way design, both random (Model II).

  • \(F_A = MS_A / MS_{AB}\), \(F_B = MS_B / MS_{AB}\), \(F_{AB} = MS_{AB} / MS_{\mathrm{within}}\).

Two-way mixed design (Model III, e.g., A fixed, B random).

  • \(F_A = MS_A / MS_{AB}\) (fixed factor against the interaction with the random factor)

  • \(F_B = MS_B / MS_{\mathrm{within}}\) (random factor against the within-cell residual)

  • \(F_{AB} = MS_{AB} / MS_{\mathrm{within}}\).

Balanced data assumed. The classical EMS rules require equal cell sizes. The function errors out on unbalanced data and recommends a mixed-effects fit via lmer.

Sums of squares. For the balanced designs this function targets, the Type I, Type II, and Type III sums of squares for each effect coincide, so the decomposition is unambiguous and no sums-of-squares type needs to be chosen (Maxwell, Delaney, & Kelley, 2027, Ch. 7). The returned object carries a numeric sum_of_squares_type attribute equal to 3, with the understanding that it equals Types I and II here; it records the convention without implying a choice that would matter for these designs.

References

Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing experiments and analyzing data: A model comparison perspective (4th ed.). Routledge. (See Chapter 7 on the sums of squares for balanced designs and Chapter 10 on random and mixed effects.)

Searle, S. R., Casella, G., & McCulloch, C. E. (1992). Variance components. Wiley.

Author

Ken Kelley kkelley@nd.edu

Examples

# 1. Two-way mixed design: A fixed, B random.
set.seed(113)
grid <- expand.grid(A = factor(1:3), B = factor(1:5), rep = 1:4)
grid$y <- with(grid, 0.8 * as.integer(A) + rep(rnorm(5, 0, 1.5), each = 1)[as.integer(B)] +
                      rnorm(nrow(grid), 0, 1))
mixed_anova(grid, outcome = "y", factor_A = "A", factor_B = "B",
            A_type = "fixed", B_type = "random")
#>   effect         ss df         ms denominator    F_value      p_value
#> 1      A  42.287564  2 21.1437821       MS_AB 54.7505466 2.148782e-05
#> 2      B 153.417534  4 38.3543835   MS_within 31.2357357 1.817787e-12
#> 3    A:B   3.089472  8  0.3861839   MS_within  0.3145075 9.565106e-01