From 0bff129b10b5b19b2a56a1e1401d3355832c2649 Mon Sep 17 00:00:00 2001 From: mariusbarth Date: Wed, 26 Jun 2024 14:36:00 +0200 Subject: [PATCH] tests for stats::anova.glm() and car::Anova.glm() --- R/apa_print_anova.R | 7 ++- R/lookup_tables.R | 2 + tests/testthat/test_apa_print_anova.R | 65 ++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/R/apa_print_anova.R b/R/apa_print_anova.R index 099631e1..8413f2c8 100644 --- a/R/apa_print_anova.R +++ b/R/apa_print_anova.R @@ -416,11 +416,14 @@ apa_print.anova <- function( , simplify = TRUE ) ) + # stats::anova.glm() and car::Anova.glm } else if(any(grepl("Deviance", object_heading))) { x$Term <-rownames(x) y <- canonize(x) y <- remove_residuals_row(y) - y$df.residual <- NULL + if(all(colnames(x) != "F values")) y$df.residual <- NULL + if(any(colnames(x) == "Cp")) y$df <- NULL + if(is.null(y$statistic)) { y$statistic <- y$deviance variable_label(y$statistic) <- "$\\chi^2$" @@ -503,7 +506,7 @@ apa_print.anova <- function( } else if(identical(object_heading[1], "ANOVA-like table for random-effects: Single term deletions")) { stop("Single-term deletions are not supported, yet.\nVisit https://github.com/crsh/papaja/issues to request support.") } - # anova::lm (single model) ---- + # anova.lm() (single model) ---- # Canonize, beautify, glue ---- y <- as.data.frame(x, stringsAsFactors = FALSE) y$Effect <- trimws(rownames(y)) diff --git a/R/lookup_tables.R b/R/lookup_tables.R index 32a0ce2f..97dd2c9a 100644 --- a/R/lookup_tables.R +++ b/R/lookup_tables.R @@ -83,6 +83,7 @@ lookup_names <- c( , "F.value" = "statistic" , "F" = "statistic" , "F.ratio" = "statistic" + , "F.values" = "statistic" , "LRT" = "statistic" , "LR.Chisq" = "statistic" , "Chisq" = "statistic" @@ -208,6 +209,7 @@ lookup_labels <- c( , "F.value" = "$F$" , "F" = "$F$" , "F.ratio" = "$F$" + , "F.values" = "$F$" , "approx.F" = "$F$" , "LRT" = "$\\chi^2$" , "LR.Chisq" = "$\\chi^2$" diff --git a/tests/testthat/test_apa_print_anova.R b/tests/testthat/test_apa_print_anova.R index a0b79a01..34c207f2 100644 --- a/tests/testthat/test_apa_print_anova.R +++ b/tests/testthat/test_apa_print_anova.R @@ -545,7 +545,7 @@ test_that( ) test_that( - "Analysis of deviance from the stats package" + "Analysis of deviance from the stats and car packages" , { # From stats::glm() examples section: ## Dobson (1990) Page 93: Randomized Controlled Trial : @@ -554,11 +554,64 @@ test_that( treatment <- gl(3,3) data.frame(treatment, outcome, counts) # showing data glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) - apa_print(anova(glm.D93, test = "Chisq")) - apa_print(anova(glm.D93, test = "Cp")) - apa_print(anova(glm.D93, test = "LRT")) - apa_print(car::Anova(glm.D93, test.statistic = "LR")) - apa_print(anova(glm.D93, test = "Rao")) + + chisq_out <- apa_print(anova(glm.D93, test = "Chisq")) + cp_out <- apa_print(anova(glm.D93, test = "Cp")) + lrt_out <- apa_print(anova(glm.D93, test = "LRT")) + rao_out <- apa_print(anova(glm.D93, test = "Rao")) + + expect_identical( + chisq_out$full_result + , list( + outcome = "$\\chi^2(2) = 5.45$, $p = .065$" + , treatment = "$\\chi^2(2) = 0.00$, $p > .999$" + ) + ) + expect_identical( + cp_out$full_result + , list( + outcome = "$C_p = 11.13$" + , treatment = "$C_p = 15.13$" + ) + ) + expect_identical( + lrt_out + , chisq_out + ) + expect_identical( + rao_out$full_result + , list( + outcome = "$\\mathit{RS}(2) = 5.56$, $p = .062$" + , treatment = "$\\mathit{RS}(2) = 0.00$, $p > .999$" + ) + ) + + car_lr_out <- apa_print(car::Anova(glm.D93, type = 3, test.statistic = "LR")) + car_wald_out <- apa_print(car::Anova(glm.D93, type = 3, test.statistic = "Wald")) + car_f_out <- apa_print(car::Anova(glm.D93, type = 3, test.statistic = "F")) + + expect_identical( + car_lr_out$full_result + , list( + outcome = "$\\chi^2(2) = 5.45$, $p = .065$" + , treatment = "$\\chi^2(2) = 0.00$, $p > .999$" + ) + ) + expect_identical( + car_wald_out$full_result + , list( + Intercept = "$\\chi^2(1) = 317.37$, $p < .001$" + , outcome = "$\\chi^2(2) = 5.49$, $p = .064$" + , treatment = "$\\chi^2(2) = 0.00$, $p > .999$" + ) + ) + expect_identical( + car_f_out$full_result + , list( + outcome = "$F(2, 4) = 2.11$, $p = .237$" + , treatment = "$F(2, 4) = 0.00$, $p > .999$" + ) + ) } )