Skip to content

Commit d544ba3

Browse files
Adjusted p values from afex::aov_4() not retained by model_parameters() (#1102)
* Adjusted p values from `afex::aov_4()` not retained by `model_parameters()` Fixes #1101 * add test * fix test * automatically detect p-adjustment in afex * fix * fix., add test * update and add test, news * p_adjust for all anova stuff #1101 * docs * styler --------- Co-authored-by: Mattan S. Ben-Shachar <[email protected]>
1 parent 1f046a8 commit d544ba3

File tree

8 files changed

+131
-18
lines changed

8 files changed

+131
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.25.0.5
4+
Version: 0.25.0.6
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
`brmsfit` and `stanreg` gets an additional `"grouplevel"` option, to return
77
the group-level estimates for random effects.
88

9+
* `model_parameters()` for Anova-objects gains a `p_adjust` argument, to apply
10+
p-adjustment where possible. Furthermore, for models from package *afex*, where
11+
p-adjustment was applied during model-fitting, the correct p-values are now
12+
returned (before, unadjusted p-values were returned in some cases).
13+
914
# parameters 0.25.0
1015

1116
## Changes

R/extract_parameters_anova.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#' @keywords internal
2-
.extract_parameters_anova <- function(model, test = "multivariate") {
2+
.extract_parameters_anova <- function(model,
3+
test = "multivariate",
4+
p_adjust = NULL,
5+
verbose = TRUE) {
36
# Processing
47
if (inherits(model, "manova")) {
58
parameters <- .extract_anova_manova(model)
@@ -19,6 +22,12 @@
1922
parameters <- .extract_anova_aov_svyglm(model)
2023
}
2124

25+
# remove intercept
26+
intercepts <- parameters$Parameter %in% c("Intercept", "(Intercept)")
27+
if (any(intercepts)) {
28+
parameters <- parameters[!intercepts, ]
29+
}
30+
2231
# Rename
2332

2433
# p-values
@@ -79,6 +88,11 @@
7988
)
8089
parameters <- parameters[col_order[col_order %in% names(parameters)]]
8190

91+
# ==== adjust p-values?
92+
if (!is.null(p_adjust)) {
93+
parameters <- .p_adjust(parameters, p_adjust, model, verbose)
94+
}
95+
8296
insight::text_remove_backticks(parameters, verbose = FALSE)
8397
}
8498

R/methods_aov.R

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ model_parameters.aov <- function(model,
102102
df_error = NULL,
103103
ci = NULL,
104104
alternative = NULL,
105+
p_adjust = NULL,
105106
test = NULL,
106107
power = FALSE,
107108
es_type = NULL,
@@ -138,7 +139,7 @@ model_parameters.aov <- function(model,
138139
}
139140

140141
# extract standard parameters
141-
params <- .extract_parameters_anova(model, test)
142+
params <- .extract_parameters_anova(model, test, p_adjust = p_adjust, verbose = verbose)
142143

143144
# add effect sizes, if available
144145
params <- .effectsizes_for_aov(
@@ -251,16 +252,19 @@ model_parameters.afex_aov <- function(model,
251252
type = NULL,
252253
keep = NULL,
253254
drop = NULL,
255+
p_adjust = NULL,
254256
verbose = TRUE,
255257
...) {
256258
if (inherits(model$Anova, "Anova.mlm")) {
257259
params <- model$anova_table
258260
with_df_and_p <- summary(model$Anova)$univariate.tests
259261
params$`Sum Sq` <- with_df_and_p[-1, 1]
260262
params$`Error SS` <- with_df_and_p[-1, 3]
261-
out <- .extract_parameters_anova(params, test = NULL)
263+
out <- .extract_parameters_anova(params, test = NULL, p_adjust = NULL, verbose)
264+
p_adjust <- .extract_p_adjust_afex(model, p_adjust)
262265
} else {
263-
out <- .extract_parameters_anova(model$Anova, test = NULL)
266+
p_adjust <- .extract_p_adjust_afex(model, p_adjust)
267+
out <- .extract_parameters_anova(model$Anova, test = NULL, p_adjust, verbose)
264268
}
265269

266270
out <- .effectsizes_for_aov(
@@ -273,7 +277,15 @@ model_parameters.afex_aov <- function(model,
273277
)
274278

275279
# add attributes
276-
out <- .add_anova_attributes(out, model, ci, test = NULL, alternative = NULL, ...)
280+
out <- .add_anova_attributes(
281+
out,
282+
model,
283+
ci,
284+
test = NULL,
285+
alternative = NULL,
286+
p_adjust = p_adjust,
287+
...
288+
)
277289

278290
# filter parameters
279291
if (!is.null(keep) || !is.null(drop)) {
@@ -561,3 +573,17 @@ model_parameters.seqanova.svyglm <- model_parameters.aov
561573

562574
data[, col_order]
563575
}
576+
577+
578+
#' @keywords internal
579+
.extract_p_adjust_afex <- function(model, p_adjust) {
580+
if (is.null(p_adjust) && inherits(model, "afex_aov")) {
581+
p_adjust <- attr(model$anova_table, "p_adjust_method")
582+
583+
if (p_adjust == "none") {
584+
p_adjust <- NULL
585+
}
586+
}
587+
588+
p_adjust
589+
}

R/utils_model_parameters.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,14 @@
388388

389389

390390
#' @keywords internal
391-
.add_anova_attributes <- function(params, model, ci, test = NULL, alternative = NULL, ...) {
391+
.add_anova_attributes <- function(params, model, ci, test = NULL, alternative = NULL, p_adjust = NULL, ...) {
392392
dot.arguments <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x) # nolint
393393

394394
attr(params, "ci") <- ci
395395
attr(params, "model_class") <- class(model)
396396
attr(params, "anova_type") <- .anova_type(model)
397397
attr(params, "text_alternative") <- .anova_alternative(params, alternative)
398+
attr(params, "p_adjust") <- p_adjust
398399

399400
if (inherits(model, "Anova.mlm") && !identical(test, "univariate")) {
400401
attr(params, "anova_test") <- model$test

man/model_parameters.aov.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-model_parameters.afex_aov.R

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,76 @@ test_that("afex_aov", {
1212
mp1 <- model_parameters(m_between, verbose = FALSE)
1313
mp2 <- model_parameters(m_within, verbose = FALSE)
1414

15-
expect_equal(c(nrow(mp1), ncol(mp1)), c(5, 7))
16-
expect_equal(mp1$Sum_Squares, c(450.62069, 11.98202, 5.56322, 8.68275, 15.2037), tolerance = 1e-3)
15+
expect_equal(c(nrow(mp1), ncol(mp1)), c(4, 7))
16+
expect_equal(mp1$Sum_Squares, c(11.98202, 5.56322, 8.68275, 15.2037), tolerance = 1e-3)
1717
expect_equal(c(nrow(mp2), ncol(mp2)), c(3, 9))
1818
expect_equal(mp2$Sum_Squares, c(167.5, 106.29167, 11.08333), tolerance = 1e-3)
19-
expect_equal(
20-
colnames(mp1),
19+
expect_named(
20+
mp1,
2121
c("Parameter", "Sum_Squares", "df", "Mean_Square", "F", "p", "Method")
2222
)
23-
expect_equal(
24-
colnames(mp2),
25-
c("Parameter", "Sum_Squares", "Sum_Squares_Error", "df", "df_error", "Mean_Square", "F", "p", "Method")
23+
expect_named(
24+
mp2,
25+
c(
26+
"Parameter", "Sum_Squares", "Sum_Squares_Error", "df", "df_error",
27+
"Mean_Square", "F", "p", "Method"
28+
)
29+
)
30+
})
31+
32+
33+
test_that("afex_aov, p-adjustement", {
34+
skip_if_not_installed("afex")
35+
data(laptop_urry, package = "afex")
36+
afx <- afex::aov_4(
37+
overall ~ condition * talk + (1 | pid),
38+
data = laptop_urry,
39+
anova_table = list(p_adjust_method = "bonferroni")
40+
)
41+
out1 <- model_parameters(afx, ci = 0.95)
42+
out2 <- model_parameters(afx, ci = 0.95, p_adjust = "bonferroni")
43+
44+
expect_identical(dim(out1), c(4L, 7L))
45+
expect_equal(out1$Sum_Squares, c(115.01087, 6703.72241, 1944.0391, 29101.23396), tolerance = 1e-3)
46+
expect_named(
47+
out1,
48+
c("Parameter", "Sum_Squares", "df", "Mean_Square", "F", "p", "Method")
49+
)
50+
expect_equal(out1$p, c(1, 0, 0.2157, NA), tolerance = 1e-3)
51+
expect_equal(out2$p, c(1, 0, 0.2157, NA), tolerance = 1e-3)
52+
53+
afx <- afex::aov_4(
54+
overall ~ condition * talk + (1 | pid),
55+
data = laptop_urry
56+
)
57+
out3 <- model_parameters(afx, ci = 0.95)
58+
out4 <- model_parameters(afx, ci = 0.95, p_adjust = "bonferroni")
59+
expect_equal(out3$p, c(0.4714, 0, 0.0719, NA), tolerance = 1e-3)
60+
expect_equal(out4$p, c(1, 0, 0.2157, NA), tolerance = 1e-3)
61+
})
62+
63+
64+
test_that("afex_aov_ez, p-adjustement", {
65+
skip_if_not_installed("afex")
66+
data(obk.long, package = "afex")
67+
a2 <- afex::aov_ez(
68+
"id",
69+
"value",
70+
data = obk.long,
71+
between = c("treatment", "gender"),
72+
within = c("phase", "hour"),
73+
observed = "gender",
74+
anova_table = list(p_adjust_method = "fdr")
75+
)
76+
77+
out <- model_parameters(a2)
78+
expect_equal(a2$anova_table$`Pr(>F)`, out$p, tolerance = 1e-4)
79+
expect_identical(dim(out), c(15L, 9L))
80+
expect_named(
81+
out,
82+
c(
83+
"Parameter", "Sum_Squares", "Sum_Squares_Error", "df", "df_error",
84+
"Mean_Square", "F", "p", "Method"
85+
)
2686
)
2787
})

tests/testthat/test-model_parameters.anova.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ test_that("print-model_parameters", {
6868
})
6969

7070

71-
test_that("model_parameters_Anova.mlm", {
71+
test_that("model_parameters_Anova.mlm-1", {
7272
skip_if_not_installed("car")
7373

7474
m <- lm(cbind(hp, mpg) ~ factor(cyl) * am, data = mtcars)
7575
a <- car::Anova(m, type = 3, test.statistic = "Pillai")
7676
mp <- model_parameters(a, verbose = FALSE)
7777

7878
expect_named(mp, c("Parameter", "df", "Statistic", "df_num", "df_error", "F", "p"))
79-
expect_equal(mp[["F"]], c(158.2578, 6.60593, 3.71327, 3.28975), tolerance = 1e-3)
80-
expect_equal(mp$Statistic, c(0.9268, 0.67387, 0.22903, 0.4039), tolerance = 1e-3)
79+
expect_equal(mp[["F"]], c(6.60593, 3.71327, 3.28975), tolerance = 1e-3)
80+
expect_equal(mp$Statistic, c(0.67387, 0.22903, 0.4039), tolerance = 1e-3)
8181
})
8282

8383

84-
test_that("model_parameters_Anova.mlm", {
84+
test_that("model_parameters_Anova.mlm-2", {
8585
skip_if_not_installed("MASS")
8686
skip_if_not_installed("car")
8787
data(housing, package = "MASS")

0 commit comments

Comments
 (0)