Nothing
skip_if_not_installed("robustbase")
skip_if_not_installed("MASS")
skip_if_not_installed("carData")
# Removes figure-space padding, too
strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
davis <- stats::na.omit(carData::Davis[, c("weight", "repwt", "sex")])
davis$Sex <- factor(davis$sex, levels = c("F", "M"), labels = c("Female", "Male"))
mm <- robustbase::lmrob(repwt ~ weight * Sex, data = davis)
mm_additive <- robustbase::lmrob(repwt ~ weight + Sex, data = davis)
huber <- MASS::rlm(repwt ~ weight * Sex, data = davis)
test_that("robust regression tables have no standardized coefficients", {
tbl <- apa_parameters(mm)
expect_s3_class(tbl, "apa7_tbl")
expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*t*(179)", "*p*"))
expect_equal(strip(tbl$`*B*`[2]), sprintf("%.2f", stats::coef(mm)[["weight"]]))
expect_equal(colnames(apa_parameters(huber)), colnames(tbl))
})
test_that("the note names the estimator, the weights, and the down-weighted cases", {
note <- apa_note(apa_parameters(mm))
expect_match(note, "MM-estimation with bisquare weights (Yohai, 1987).", fixed = TRUE)
expect_match(note, "Standard errors are robust estimates.", fixed = TRUE)
expect_match(note, "*N*\u00A0=\u00A0183.", fixed = TRUE)
low <- sum(mm$rweights < .1)
expect_match(note, paste0("Cases with weights below .10 (*n*\u00A0=\u00A0", low, ")"), fixed = TRUE)
expect_match(apa_note(apa_parameters(huber)), "M-estimation with Huber weights (Huber, 1964).", fixed = TRUE)
bisquare <- MASS::rlm(repwt ~ weight * Sex, data = davis, psi = MASS::psi.bisquare)
expect_match(apa_note(apa_parameters(bisquare)), "M-estimation with bisquare weights", fixed = TRUE)
mm_rlm <- MASS::rlm(repwt ~ weight * Sex, data = davis, method = "MM")
expect_match(apa_note(apa_parameters(mm_rlm)), "MM-estimation with bisquare weights (Yohai, 1987).", fixed = TRUE)
expect_match(apa_note(apa_parameters(mm_rlm)), "asymptotic estimates", fixed = TRUE)
})
test_that("apa_performance() gives the robust R2 and the robust scale", {
perf <- apa_performance(mm)
expect_equal(colnames(perf), c("*R*^2^~robust~", "*s*~robust~"))
expect_equal(perf$`*R*^2^~robust~`, sub("^0", "", sprintf("%.2f", summary(mm)$r.squared)))
expect_equal(strip(perf$`*s*~robust~`), sprintf("%.2f", mm$scale))
expect_equal(strip(apa_performance(huber)$`*s*~robust~`), sprintf("%.2f", huber$s))
})
test_that("lmrob models are compared with robust Wald tests, and rlm models without tests", {
comparison <- apa_performance_comparison(mm_additive, mm)
wald <- as.data.frame(stats::anova(mm, mm_additive))
expect_equal(strip(comparison$`*χ*^2^`[2]), sprintf("%.2f", wald$Test.Stat[2]))
expect_equal(strip(comparison$`*df*`[2]), "1")
huber_additive <- MASS::rlm(repwt ~ weight + Sex, data = davis)
expect_equal(colnames(apa_performance_comparison(huber_additive, huber)), c("Model", "*s*~robust~"))
# In lists, the down-weighted cases of each model are in one sentence
note <- apa_note(apa_parameters(list(mm_additive, mm)))
expect_match(note, "in the models, in order)", fixed = TRUE)
expect_equal(lengths(regmatches(note, gregexpr("Estimates are from", note, fixed = TRUE))), 1)
})
test_that("rlm models that did not converge give a warning", {
set.seed(1)
bad <- suppressWarnings(MASS::rlm(repwt ~ weight * Sex, data = davis, maxit = 1))
expect_false(bad$converged)
expect_warning(apa_parameters(bad), "did not converge")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.