tests/testthat/test-manova.R

skip_if_not_installed("carData")

manova_fit <- function() {
  manova(
    cbind(post.test.1, post.test.2, post.test.3) ~ group,
    data = carData::Baumann
  )
}

test_that("MANOVA tables match summary.manova() for each test statistic", {
  fit <- manova_fit()
  headers <- c(Pillai = "*V*", Wilks = "*&Lambda;*", `Hotelling-Lawley` = "*T*", Roy = "*&Theta;*")
  for (test in names(headers)) {
    tbl <- apa_anova(fit, test = test)
    expect_s3_class(tbl, "apa7_tbl")
    expect_equal(
      colnames(tbl),
      c("Effect", headers[[test]], "*F*", "*df*~1~", "*df*~2~", "*p*", "*&eta;*^2^~p~"),
      label = test
    )
    stats <- summary(fit, test = test)$stats
    expect_equal(tbl$`*F*`, sprintf("%.2f", stats["group", "approx F"]), label = test)
    expect_equal(tbl$`*df*~2~`, as.character(stats["group", "den Df"]), label = test)
  }
  expect_equal(apa_anova(fit)$Effect, "Group")
})

test_that("partial eta squared comes from the approximate F", {
  fit <- manova_fit()
  tbl <- apa_anova(fit, anova_parameters = c("Parameter", "Statistic", "F", "df", "df_error", "p", "Effect_Size", "CI"))
  s <- summary(fit)$stats
  es <- effectsize::F_to_eta2(s["group", "approx F"], s["group", "num Df"], s["group", "den Df"], alternative = "two.sided")
  expect_equal(tbl$`*&eta;*^2^~p~`, sub("^0", "", sprintf("%.2f", es$Eta2_partial)))
  expect_match(tbl$`95% CI`, "^\\[")
  # For Pillai's trace, this is V / s, as effectsize computes it
  expect_equal(es$Eta2_partial, unname(s["group", "Pillai"] / 2))
})

test_that("multivariate linear models get Type II and III tests from car", {
  skip_if_not_installed("car")
  fit <- lm(cbind(post.test.1, post.test.2, post.test.3) ~ group + pretest.1, data = carData::Baumann)
  tbl <- apa_anova(fit, type = 2, test = "Wilks")
  expect_equal(tbl$Effect, c("Group", "Pretest 1"))
  a <- car::Anova(fit, type = 2, test.statistic = "Wilks")
  expected <- utils::capture.output(print(a))
  expect_true(any(grepl("7.1351", expected, fixed = TRUE)))
  expect_equal(trimws(tbl$`*F*`, whitespace = "[\\h\\v]"), c("7.14", "21.48"))
  expect_match(apa_note(tbl), "Type II sums of squares", fixed = TRUE)
  # car::Anova() tables give the same table, with their own statistic
  expect_equal(unclass(apa_anova(a))[colnames(tbl)], unclass(tbl)[colnames(tbl)], ignore_attr = TRUE)
  # The intercept of Type III tests is not shown
  expect_equal(apa_anova(fit, type = 3)$Effect, c("Group", "Pretest 1"))
  expect_warning(apa_anova(a, type = 3), "ignored")
  expect_error(apa_anova(a, univariate = TRUE), "do not keep the model")
})

test_that("the MANOVA note names the statistic, outcomes, N, and sums of squares", {
  note <- apa_note(apa_anova(manova_fit()))
  expect_match(note, "Multivariate tests of Post Test 1, Post Test 2, and Post Test 3, with approximate *F* tests. *N*\u00A0=\u00A066.", fixed = TRUE)
  expect_match(note, "Sums of squares are sequential (Type I).", fixed = TRUE)
  expect_match(note, "*V*\u00A0=\u00A0Pillai's trace (Pillai, 1955)", fixed = TRUE)
  expect_match(note, "computed from the approximate *F*", fixed = TRUE)
  expect_match(apa_note(apa_anova(manova_fit(), test = "Wilks")), "Wilks's lambda (Wilks, 1932)", fixed = TRUE)
})

test_that("univariate follow-ups show each outcome's ANOVA under a heading", {
  fit <- manova_fit()
  tbl <- apa_anova(fit, univariate = TRUE)
  strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
  expect_equal(
    strip(tbl$Source),
    c("Post Test 1", "Group", "Error", "Post Test 2", "Group", "Error", "Post Test 3", "Group", "Error")
  )
  uni <- summary.aov(fit)
  f <- vapply(uni, \(x) x[["F value"]][1], numeric(1))
  expect_equal(tbl$`*F*`[c(2, 5, 8)], sprintf("%.2f", f))
  expect_match(apa_note(tbl), "Univariate analyses of variance of each outcome.", fixed = TRUE)
  skip_if_not_installed("car")
  ml <- lm(cbind(post.test.1, post.test.2, post.test.3) ~ group + pretest.1, data = carData::Baumann)
  expect_equal(nrow(apa_anova(ml, univariate = TRUE, type = 2)), 12)
})

test_that("univariate follow-ups need cbind() outcomes", {
  d <- carData::Baumann
  y <- as.matrix(d[c("post.test.1", "post.test.2")])
  fit <- lm(y ~ d$group)
  expect_error(apa_anova(fit, univariate = TRUE), "cbind")
})

test_that("apa_parameters() of a manova model gives the MANOVA table", {
  expect_equal(apa_parameters(manova_fit())$Effect, "Group")
})

test_that("MANOVA tables refuse generalized and non-partial effect sizes", {
  expect_error(apa_anova(manova_fit(), generalized = TRUE), "Generalized")
  expect_error(apa_anova(manova_fit(), partial = FALSE), "partial")
})

Try the apa7 package in your browser

Any scripts or data that you put into this service are public.

apa7 documentation built on Dec. 1, 2026, 5:06 p.m.