tests/testthat/test-mlm.R

strip <- function(x) trimws(x, whitespace = "[\\h\\v]")

mlm_fit <- function() lm(cbind(mpg, qsec) ~ wt + hp, data = mtcars)

test_that("multivariate models show each outcome's coefficients under a heading", {
  tbl <- apa_parameters(mlm_fit())
  expect_s3_class(tbl, "apa7_tbl")
  expect_equal(
    strip(tbl$Variable),
    c("Mpg", "Constant", "Wt", "Hp", "Qsec", "Constant", "Wt", "Hp")
  )
  expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "&beta;", "*t*(29)", "*p*"))
  # The coefficients of each outcome's own regression
  single <- apa_parameters(lm(qsec ~ wt + hp, data = mtcars))
  expect_equal(tbl[6:8, -1], single[, -1], ignore_attr = TRUE)
  # Values are padded to align across outcomes
  single_mpg <- apa_parameters(lm(mpg ~ wt + hp, data = mtcars))
  expect_equal(
    lapply(tbl[2:4, -1], strip),
    lapply(single_mpg[, -1], strip),
    ignore_attr = TRUE
  )
})

test_that("multivariate models can show outcomes side by side", {
  tbl <- apa_parameters(mlm_fit(), outcomes = "columns")
  expect_equal(strip(tbl$Variable), c("Constant", "Wt", "Hp"))
  expect_true(all(c("Mpg_*B*", "Qsec_*B*", "Qsec_&beta;") %in% colnames(tbl)))
  expect_equal(
    tbl[["Qsec_*B*"]],
    apa_parameters(lm(qsec ~ wt + hp, data = mtcars))[["*B*"]]
  )
  expect_s3_class(apa_flextable(tbl), "flextable")
})

test_that("standardized coefficients match those of each outcome's lm()", {
  skip_if_not_installed("carData")
  d <- carData::Baumann
  fit <- lm(cbind(post.test.1, post.test.3) ~ group + pretest.1, data = d)
  std <- apa7:::mlm_std_coefficients(fit)
  for (y in c("post.test.1", "post.test.3")) {
    expected <- parameters::standardise_parameters(
      stats::lm(stats::reformulate(c("group", "pretest.1"), y), data = d),
      method = "basic"
    )
    expect_equal(std$Std_Coefficient[std$Response == y], expected$Std_Coefficient, label = y)
  }
})

test_that("the note says the coefficients are from separate regressions", {
  note <- apa_note(apa_parameters(mlm_fit()))
  expect_match(
    note,
    "Coefficients are from separate regressions of Mpg and Qsec on the same predictors, so their tests do not take the correlations among the outcomes into account. *N*\u00A0=\u00A032.",
    fixed = TRUE
  )
  expect_match(note, "&beta;\u00A0=\u00A0standardized coefficient", fixed = TRUE)
})

test_that("names with spaces and matrix outcomes work", {
  d <- mtcars
  d$`Car Weight` <- d$wt
  tbl <- apa_parameters(lm(cbind(mpg, qsec) ~ `Car Weight`, data = d))
  expect_true("Car Weight" %in% strip(tbl$Variable))
  y <- as.matrix(mtcars[c("mpg", "qsec")])
  tbl <- apa_parameters(lm(y ~ wt, data = mtcars))
  expect_equal(strip(tbl$Variable)[c(1, 4)], c("Mpg", "Qsec"))
})

test_that("multivariate performance has a row for each outcome", {
  fit <- mlm_fit()
  tbl <- apa_performance(fit)
  expect_equal(tbl$Outcome, c("Mpg", "Qsec"))
  expect_equal(colnames(tbl), c("Outcome", "*R*^2^", "adj*R*^2^", "&sigma;~*e*~"))
  s <- summary(fit)
  expect_equal(
    tbl$`*R*^2^`,
    sub("^0", "", sprintf("%.2f", c(s[[1]]$r.squared, s[[2]]$r.squared)))
  )
  all <- apa_performance(fit, metrics = "all")
  expect_true(all(c("*F*", "*df*~1~", "*df*~2~", "*p*") %in% colnames(all)))
  expect_equal(strip(all$`*F*`), sprintf("%.2f", c(s[[1]]$fstatistic[1], s[[2]]$fstatistic[1])))
})

test_that("lists of multivariate models give a clear error", {
  m1 <- lm(cbind(mpg, qsec) ~ wt, data = mtcars)
  m2 <- mlm_fit()
  expect_error(apa_parameters(list(m1, m2)), "not supported yet")
  expect_error(apa_performance_comparison(m1, m2), "not supported yet")
})

test_that("manova models still get the MANOVA table", {
  fit <- manova(cbind(mpg, qsec) ~ factor(cyl), data = mtcars)
  expect_true("*V*" %in% colnames(apa_parameters(fit)))
})

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.