Skip to content

Commit

Permalink
tests for stats::anova.glm() and car::Anova.glm()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbarth committed Jun 26, 2024
1 parent e667def commit 0bff129
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
7 changes: 5 additions & 2 deletions R/apa_print_anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions R/lookup_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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$"
Expand Down
65 changes: 59 additions & 6 deletions tests/testthat/test_apa_print_anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand All @@ -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$"
)
)
}
)

Expand Down

0 comments on commit 0bff129

Please sign in to comment.