tests/testthat/test-quantreg.R

skip_if_not_installed("quantreg")
skip_if_not_installed("psych")

sat <- psych::sat.act
sat$female <- as.integer(sat$gender == 2)
# quantreg warns that solutions with integer outcomes may be nonunique
rq_fit <- function(...) suppressWarnings(quantreg::rq(..., data = sat))
median_fit <- rq_fit(ACT ~ education + female, tau = .5)
quartile_fit <- rq_fit(ACT ~ education + female, tau = c(.25, .5, .75))

test_that("apa_parameters() makes a coefficient table for quantile regression", {
  tbl <- apa_parameters(median_fit)
  expect_s3_class(tbl, "apa7_tbl")
  expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*t*(697)", "*p*"))
  s <- quantreg::summary.rq(median_fit, se = "nid")$coefficients
  expect_equal(trimws(tbl$`*SE*`[2]), sprintf("%.2f", s["education", 2]))
  # Other standard errors
  iid <- apa_parameters(median_fit, se = "iid")
  s_iid <- quantreg::summary.rq(median_fit, se = "iid")$coefficients
  expect_equal(trimws(iid$`*SE*`[2]), sprintf("%.2f", s_iid["education", 2]))
  expect_error(apa_parameters(median_fit, se = "rank"), "nid")
})

test_that("the note names the quantile and the standard errors", {
  note <- apa_note(apa_parameters(median_fit))
  expect_match(note, "quantile regression (Koenker & Bassett, 1978) of the median of ACT (*&tau;*\u00A0=\u00A0.50)", fixed = TRUE)
  expect_match(note, "Hendricks–Koenker sandwich estimates", fixed = TRUE)
  expect_match(note, paste0("*N*\u00A0=\u00A0", format(length(median_fit$residuals), big.mark = ","), "."), fixed = TRUE)
  expect_match(apa_note(apa_parameters(quartile_fit)), paste0("*N*\u00A0=\u00A0", format(length(median_fit$residuals), big.mark = ","), "."), fixed = TRUE)
  q25 <- apa_note(apa_parameters(rq_fit(ACT ~ education, tau = .25), se = "iid"))
  expect_match(q25, "of the .25 quantile of ACT (*&tau;*\u00A0=\u00A0.25)", fixed = TRUE)
  expect_match(q25, "independent and identically distributed errors", fixed = TRUE)
  expect_match(
    apa_note(apa_parameters(quartile_fit)),
    "quantile regressions (Koenker & Bassett, 1978) of ACT at *&tau;*\u00A0=\u00A0.25, .50, and .75.",
    fixed = TRUE
  )
})

test_that("several quantiles are side by side or in rows", {
  wide <- apa_parameters(quartile_fit)
  expect_equal(nrow(wide), 3)
  expect_true(all(c("*&tau;* = .25_*B*", "*&tau;* = .75_*p*") %in% colnames(wide)))
  ft <- apa_flextable(wide)
  expect_true(any(startsWith(ft$col_keys, "apa7breakcolumn")))
  long <- apa_parameters(quartile_fit, quantiles = "rows")
  expect_equal(nrow(long), 12)
  expect_equal(long$Variable[c(1, 5, 9)], paste0("*&tau;* = ", c(".25", ".50", ".75")))
  # Both layouts show the same estimates
  expect_equal(trimws(wide$`*&tau;* = .75_*B*`[3]), trimws(long$`*B*`[12]))
})

test_that("apa_performance() gives the Koenker-Machado R1", {
  check_loss <- function(u, tau) sum(u * (tau - (u < 0)))
  null_fit <- rq_fit(ACT ~ 1, tau = .5)
  r1 <- 1 - check_loss(stats::residuals(median_fit), .5) /
    check_loss(stats::residuals(null_fit), .5)
  perf <- apa_performance(median_fit)
  expect_equal(colnames(perf), c("*R*^1^", "*AIC*"))
  expect_equal(perf$`*R*^1^`, sub("^0", "", sprintf("%.2f", r1)))
  # A row for each quantile
  quartiles <- apa_performance(quartile_fit)
  expect_equal(quartiles$Quantile, c(".25", ".50", ".75"))
})

test_that("lists of quantile regressions are compared by R1 and AIC", {
  small <- rq_fit(ACT ~ education, tau = .5)
  comparison <- apa_performance_comparison(small, median_fit)
  expect_equal(colnames(comparison), c("Model", "*R*^1^", "*AIC*"))
  both <- apa_parameters(list(small, median_fit), performance_layout = "columns")
  expect_true(all(c("*R*^1^", "*AIC*") %in% colnames(both)))
  expect_match(apa_note(both), "*R*^1^\u00A0=\u00A0Koenker–Machado goodness of fit", fixed = TRUE)
  expect_no_match(apa_note(both), "\\*p\\*")
})

test_that("tau_text() keeps the digits that quantiles need", {
  expect_equal(apa7:::tau_text(c(.5, .25, .9, .333)), c(".50", ".25", ".90", ".333"))
})

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.