## tests/testthat/test-statAPA.R library(statAPA) # ---- shared data ---- mtcars2 <- mtcars mtcars2$cyl <- factor(mtcars2$cyl) mtcars2$gear <- factor(mtcars2$gear) # ============================================================================= # apa_descriptives # ============================================================================= test_that("apa_descriptives returns correct structure (ungrouped)", { res <- apa_descriptives(mtcars, vars = c("mpg", "wt")) expect_type(res, "list") expect_true(is.data.frame(res$descriptives_df)) expect_equal(nrow(res$descriptives_df), 2L) expect_true(all(c("Variable", "M", "SD", "N") %in% names(res$descriptives_df))) }) test_that("apa_descriptives returns correct structure (grouped)", { res <- apa_descriptives(mtcars2, vars = "mpg", group = "cyl") expect_true(is.data.frame(res$descriptives_df)) expect_equal(nrow(res$descriptives_df), 1L) }) test_that("apa_descriptives stops on missing variable", { expect_error(apa_descriptives(mtcars, vars = "nonexistent")) }) # ============================================================================= # apa_t_test # ============================================================================= test_that("apa_t_test one-sample returns list with table and note", { res <- apa_t_test(mtcars$mpg, mu = 20) expect_type(res, "list") expect_true(is.data.frame(res$table)) expect_equal(nrow(res$table), 1L) expect_true("g" %in% names(res$table)) }) test_that("apa_t_test two-sample Welch works", { res <- apa_t_test(mtcars$mpg[mtcars$am == 0], mtcars$mpg[mtcars$am == 1]) expect_true(grepl("Welch", res$table$Test)) }) test_that("apa_t_test paired works", { res <- apa_t_test(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2], paired = TRUE) expect_true(grepl("Paired", res$table$Test)) }) # ============================================================================= # apa_chisq # ============================================================================= test_that("apa_chisq independence test works", { m <- matrix(c(30, 10, 20, 40), nrow = 2) res <- apa_chisq(m, output = "list") expect_true(is.data.frame(res$table)) expect_equal(res$table$Effect, "V") }) test_that("apa_chisq goodness-of-fit works", { res <- apa_chisq(c(50, 30, 20), p = c(0.5, 0.3, 0.2), output = "list") expect_true(is.data.frame(res$table)) }) # ============================================================================= # apa_prop_test # ============================================================================= test_that("apa_prop_test one-sample works", { res <- apa_prop_test(x = 35, n = 50, p0 = 0.5) expect_true(is.data.frame(res$table)) expect_equal(res$table$RD, "") }) test_that("apa_prop_test two-sample includes RD/RR/OR", { res <- apa_prop_test(x = c(30, 20), n = c(50, 50)) expect_true(nzchar(res$table$RD)) expect_true(nzchar(res$table$RR)) expect_true(nzchar(res$table$OR)) }) # ============================================================================= # apa_anova # ============================================================================= test_that("apa_anova returns list with anova and table", { fit <- lm(mpg ~ cyl, data = mtcars2) res <- suppressMessages(apa_anova(fit, es = "eta2")) expect_type(res, "list") expect_true(is.data.frame(res$table)) expect_true("eta2" %in% names(res$table)) }) test_that("apa_anova partial_eta2 works", { fit <- lm(mpg ~ cyl, data = mtcars2) res <- suppressMessages(apa_anova(fit, es = "partial_eta2")) expect_true("partial_eta2" %in% names(res$table)) }) # ============================================================================= # apa_ancova # ============================================================================= test_that("apa_ancova returns model, anova_table, and adjusted_means", { res <- suppressMessages(apa_ancova( mpg ~ cyl + wt, data = mtcars2, covariate = "wt", focal = "cyl" )) expect_type(res, "list") expect_true(is.data.frame(res$anova_table)) expect_true(is.data.frame(res$adjusted_means)) expect_true("cyl" %in% names(res$adjusted_means)) }) # ============================================================================= # apa_twoway_anova # ============================================================================= test_that("apa_twoway_anova returns anova_table and simple_effects", { res <- suppressMessages(apa_twoway_anova( mpg ~ cyl * gear, data = mtcars2, factorA = "cyl", factorB = "gear" )) expect_type(res, "list") expect_true(is.data.frame(res$anova_table)) # simple effects may be NULL if model is rank-deficient — just check type expect_true(is.null(res$simple_effects) || is.data.frame(res$simple_effects)) }) # ============================================================================= # apa_manova # ============================================================================= test_that("apa_manova returns table with four test rows per effect", { res <- suppressMessages(apa_manova( cbind(Sepal.Length, Petal.Length) ~ Species, data = iris )) expect_type(res, "list") expect_true(is.data.frame(res$table)) expect_true("Test" %in% names(res$table)) # Should have Pillai, Wilks, Hotelling-Lawley, Roy expect_true(all(c("Pillai", "Wilks", "Hotelling-Lawley", "Roy") %in% res$table$Test)) }) # ============================================================================= # apa_posthoc # ============================================================================= test_that("apa_posthoc returns pairs data.frame", { fit <- aov(mpg ~ cyl, data = mtcars2) res <- suppressMessages(apa_posthoc(fit, by = "cyl")) expect_type(res, "list") expect_true(is.data.frame(res$pairs)) expect_gt(nrow(res$pairs), 0L) }) # ============================================================================= # apa_hetero / apa_homoskedasticity # ============================================================================= test_that("apa_hetero returns data.frame with Breusch-Pagan row", { fit <- lm(mpg ~ wt + hp, data = mtcars) res <- suppressMessages(apa_hetero(fit)) expect_true(is.data.frame(res)) expect_true("Breusch-Pagan" %in% res$Test) }) test_that("apa_homoskedasticity returns data.frame", { fit <- lm(mpg ~ wt + hp, data = mtcars) res <- suppressMessages(apa_homoskedasticity(fit)) expect_true(is.data.frame(res)) }) # ============================================================================= # apa_robust # ============================================================================= test_that("apa_robust returns table and vcov", { fit <- lm(mpg ~ wt + hp, data = mtcars) res <- suppressMessages(apa_robust(fit, type = "HC3")) expect_type(res, "list") expect_true(is.data.frame(res$table)) expect_true(is.matrix(res$vcov)) }) # ============================================================================= # apa_table # ============================================================================= test_that("apa_table returns fixed_effects data.frame for lm", { fit <- lm(mpg ~ wt + hp, data = mtcars) res <- suppressMessages(apa_table(fit)) expect_type(res, "list") expect_true(is.data.frame(res$fixed_effects)) }) # ============================================================================= # apa_z_test_mean # ============================================================================= test_that("apa_z_test_mean one-sample works", { res <- apa_z_test_mean(mtcars$mpg, sigma_x = 6, mu = 20, output = "list") expect_true(is.data.frame(res$table)) expect_true("stat" %in% names(res$table)) }) # ============================================================================= # format helpers # ============================================================================= test_that(".format_p handles edge cases", { expect_equal(statAPA:::.format_p(0.0001), "< .001") expect_equal(statAPA:::.format_p(0.05), ".050") expect_true(is.na(statAPA:::.format_p(NA))) })