Skip to content

Commit d71c6d4

Browse files
authored
Merge branch 'main' into strengejacke/issue594
2 parents 39c7259 + 25b2ac5 commit d71c6d4

File tree

8 files changed

+248
-188
lines changed

8 files changed

+248
-188
lines changed

R/estimate_slopes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
#' @examplesIf all(insight::check_if_installed(c("marginaleffects", "emmeans"), quietly = TRUE)) && getRversion() >= "4.5.0"
110110
#' \dontrun{
111111
#' # marginal effects with different `estimate` options
112-
#' data(penguins)
112+
#' data(penguins, package = "datasets")
113113
#' penguins$long_bill <- factor(datawizard::categorize(penguins$bill_len), labels = c("short", "long"))
114114
#' m <- glm(long_bill ~ sex + species + island * bill_dep, data = penguins, family = "binomial")
115115
#'

R/p_adjust.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88

99
# extract information
1010
datagrid <- attributes(params)$datagrid
11-
focal <- .safe(insight::trim_ws(gsub("=.*", "\\1", attributes(params)$contrast)))
11+
12+
# when contrasting slopes, we need "by" as focal term
13+
if (inherits(params, "estimate_slopes")) {
14+
focal <- .safe(insight::trim_ws(gsub("=.*", "\\1", attributes(params)$by)))
15+
} else {
16+
focal <- .safe(insight::trim_ws(gsub("=.*", "\\1", attributes(params)$contrast)))
17+
}
18+
1219
# extract degrees of freedom
1320
dof <- .safe(params$df[1])
21+
1422
if (is.null(dof)) {
1523
dof <- insight::get_df(model, type = "wald", verbose = FALSE)
1624
}

man/estimate_slopes.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-estimate_contrasts.R

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ test_that("estimate_contrasts - p.adjust", {
703703
skip_if(getRversion() < "4.5.0")
704704
skip_if_not_installed("emmeans")
705705

706-
data(penguins)
706+
data(penguins, package = "datasets")
707707
m_spec <- lm(body_mass ~ species, data = penguins)
708708
out1 <- estimate_contrasts(m_spec, p_adjust = "tukey")
709709
out2 <- estimate_contrasts(m_spec, p_adjust = "tukey", backend = "emmeans")
@@ -1709,7 +1709,7 @@ test_that("estimate_contrast, informative error when `by` and `contrast` are the
17091709

17101710
test_that("estimate_contrast, works with aov (when no statistic is extracted)", {
17111711
skip_if(getRversion() < "4.5.0")
1712-
data(penguins)
1712+
data(penguins, package = "datasets")
17131713
fit <- aov(formula = body_mass ~ species, data = penguins)
17141714

17151715
out1 <- marginaleffects::avg_predictions(fit, by = "species", hypothesis = ~pairwise)
@@ -1736,7 +1736,7 @@ test_that("estimate_contrast, works with aov (when no statistic is extracted)",
17361736
test_that("estimate_contrast, slopes with different estimate options", {
17371737
skip_if(getRversion() < "4.5.0")
17381738
skip_if_not_installed("datawizard")
1739-
data(penguins)
1739+
data(penguins, package = "datasets")
17401740
penguins$long_bill <- factor(
17411741
datawizard::categorize(penguins$bill_len),
17421742
labels = c("short", "long")
@@ -1766,3 +1766,26 @@ test_that("estimate_contrast, comparison-options as strings", {
17661766
out <- estimate_contrasts(mod2, contrast = "cyl_helmert", comparison = "poly")
17671767
expect_equal(out$Difference, c(-8.17673, 0.92996), tolerance = 1e-4)
17681768
})
1769+
1770+
1771+
test_that("estimate_contrast, p-adjust tukey works for contrasting slopes", {
1772+
skip_if(getRversion() < "4.5.0")
1773+
skip_if_not_installed("emmeans")
1774+
1775+
data(penguins, package = "datasets")
1776+
m <- lm(flipper_len ~ body_mass * species, data = penguins)
1777+
1778+
out1 <- as.data.frame(pairs(emmeans::emtrends(m, ~species, var = "body_mass")))
1779+
out2 <- estimate_contrasts(
1780+
m,
1781+
contrast = "body_mass",
1782+
by = "species",
1783+
p_adjust = "tukey"
1784+
)
1785+
1786+
# Note: p-values from emmeans::emtrends() + pairs() and estimate_contrasts()
1787+
# can differ slightly due to different underlying calculation/adjustment
1788+
# methods, especially with Tukey p-adjustment. A tolerance of 1e-2 is used
1789+
# here to avoid fragile tests while still ensuring close agreement.
1790+
expect_equal(out1$p.value, out2$p, tolerance = 1e-2)
1791+
})

tests/testthat/test-estimate_contrasts_inequality.R

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ skip_if_not_installed("datawizard")
77

88

99
test_that("estimate_contrast, marginal effects inequalities", {
10-
data(penguins)
10+
data(penguins, package = "datasets")
1111
penguins$long_bill <- factor(
1212
datawizard::categorize(penguins$bill_len),
1313
labels = c("short", "long")
@@ -88,14 +88,22 @@ test_that("estimate_contrast, marginal effects inequalities", {
8888
tolerance = 1e-4,
8989
ignore_attr = TRUE
9090
)
91-
expect_named(out, c("Parameter", "Mean_Difference", "SE", "CI_low", "CI_high", "z", "p"))
91+
expect_named(
92+
out,
93+
c("Parameter", "Mean_Difference", "SE", "CI_low", "CI_high", "z", "p")
94+
)
9295
expect_identical(
9396
out$Parameter,
9497
c("Biscoe - Dream", "Biscoe - Torgersen", "Dream - Torgersen")
9598
)
9699

97100
# pairwise inequality comparisons with `by`
98-
out <- estimate_contrasts(m, "species", by = "island", comparison = "inequality_pairwise")
101+
out <- estimate_contrasts(
102+
m,
103+
"species",
104+
by = "island",
105+
comparison = "inequality_pairwise"
106+
)
99107
expect_equal(
100108
out$Mean_Difference,
101109
c(0.127399, 0.000135, -0.127264),
@@ -110,7 +118,11 @@ test_that("estimate_contrast, marginal effects inequalities", {
110118
)
111119

112120
# check formula interface for grouping
113-
m <- glm(long_bill ~ species * sex * island + bill_dep, data = penguins, family = "binomial")
121+
m <- glm(
122+
long_bill ~ species * sex * island + bill_dep,
123+
data = penguins,
124+
family = "binomial"
125+
)
114126
out <- suppressWarnings(estimate_contrasts(
115127
m,
116128
"species",
@@ -153,7 +165,11 @@ test_that("estimate_contrast, marginal effects inequalities", {
153165
expect_identical(dim(out), c(60L, 9L))
154166

155167
# contrasting slopes
156-
m <- glm(long_bill ~ sex + species + island * bill_dep, data = penguins, family = "binomial")
168+
m <- glm(
169+
long_bill ~ sex + species + island * bill_dep,
170+
data = penguins,
171+
family = "binomial"
172+
)
157173
out <- estimate_contrasts(
158174
m,
159175
"bill_dep",
@@ -183,15 +199,25 @@ test_that("estimate_contrast, marginal effects inequalities", {
183199

184200

185201
test_that("estimate_contrast, inequality ratios", {
186-
data(penguins)
202+
data(penguins, package = "datasets")
187203
m <- lm(bill_len ~ species * bill_dep + island, data = penguins)
188204

189205
# pairwise ratios for slopes
190206
out <- estimate_contrasts(m, "bill_dep", by = "species", comparison = ratio ~ pairwise)
191-
expect_equal(out$Ratio, c(2.262453, 2.378612, 1.051342), tolerance = 1e-4, ignore_attr = TRUE)
207+
expect_equal(
208+
out$Ratio,
209+
c(2.262453, 2.378612, 1.051342),
210+
tolerance = 1e-4,
211+
ignore_attr = TRUE
212+
)
192213

193214
# inequality ratios for slopes
194-
out <- estimate_contrasts(m, "bill_dep", by = "species", comparison = ratio ~ inequality)
215+
out <- estimate_contrasts(
216+
m,
217+
"bill_dep",
218+
by = "species",
219+
comparison = ratio ~ inequality
220+
)
195221
expect_equal(out$Mean_Ratio, 1.897469, tolerance = 1e-4, ignore_attr = TRUE)
196222
expect_identical(out$Parameter, "species")
197223

@@ -217,8 +243,18 @@ test_that("estimate_contrast, inequality ratios", {
217243
expect_identical(as.character(out$sex), c("female", "male"))
218244

219245
# pairwise inequality ratios
220-
out <- estimate_contrasts(m, "island", by = "sex", comparison = ratio ~ inequality + pairwise)
221-
expect_equal(out$Mean_Ratio_Difference, 0.01355006, tolerance = 1e-4, ignore_attr = TRUE)
246+
out <- estimate_contrasts(
247+
m,
248+
"island",
249+
by = "sex",
250+
comparison = ratio ~ inequality + pairwise
251+
)
252+
expect_equal(
253+
out$Mean_Ratio_Difference,
254+
0.01355006,
255+
tolerance = 1e-4,
256+
ignore_attr = TRUE
257+
)
222258
})
223259

224260

@@ -232,16 +268,25 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
232268
m <- lm(QoL ~ time * education * grp, data = qol_cancer)
233269

234270
# test integer handling
235-
expect_silent(
236-
estimate_contrasts(m, "time", by = "education", integer_as_continuous = TRUE)
237-
)
238-
expect_silent(
239-
estimate_contrasts(m, "time", by = "education", integer_as_continuous = 2)
240-
)
271+
expect_silent(estimate_contrasts(
272+
m,
273+
"time",
274+
by = "education",
275+
integer_as_continuous = TRUE
276+
))
277+
expect_silent(estimate_contrasts(
278+
m,
279+
"time",
280+
by = "education",
281+
integer_as_continuous = 2
282+
))
241283
# we also show no warning when user explicitly sets integer_as_continuous
242-
expect_silent(
243-
estimate_contrasts(m, "time", by = "education", integer_as_continuous = 10)
244-
)
284+
expect_silent(estimate_contrasts(
285+
m,
286+
"time",
287+
by = "education",
288+
integer_as_continuous = 10
289+
))
245290
expect_message(
246291
estimate_contrasts(m, "time", by = "education"),
247292
regex = "Numeric variable appears to be ordinal"
@@ -268,15 +313,15 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
268313
estimate = "average"
269314
)
270315
out2 <- marginaleffects::hypotheses(
271-
marginaleffects::avg_slopes(m, variables = "time", by = "education", hypothesis = ~pairwise),
272-
hypothesis = ~I(mean(abs(x)))
273-
)
274-
expect_equal(
275-
out1$Mean_Difference,
276-
out2$estimate,
277-
tolerance = 1e-4,
278-
ignore_attr = TRUE
316+
marginaleffects::avg_slopes(
317+
m,
318+
variables = "time",
319+
by = "education",
320+
hypothesis = ~pairwise
321+
),
322+
hypothesis = ~ I(mean(abs(x)))
279323
)
324+
expect_equal(out1$Mean_Difference, out2$estimate, tolerance = 1e-4, ignore_attr = TRUE)
280325

281326
# inequality with slopes and grouping
282327
out <- estimate_contrasts(
@@ -286,29 +331,39 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
286331
comparison = ~inequality,
287332
integer_as_continuous = TRUE
288333
)
289-
expect_equal(out$Mean_Difference, c(4.742403, 2.883987), tolerance = 1e-4, ignore_attr = TRUE)
334+
expect_equal(
335+
out$Mean_Difference,
336+
c(4.742403, 2.883987),
337+
tolerance = 1e-4,
338+
ignore_attr = TRUE
339+
)
290340
expect_identical(out$Parameter, c("education: Group 1", "education: Group 2"))
291341

292342
# inequality with slopes and grouping, using formula interface
293343
out <- estimate_contrasts(
294344
m,
295345
"time",
296346
by = c("education", "grp"),
297-
comparison = ~inequality | grp,
347+
comparison = ~ inequality | grp,
298348
integer_as_continuous = TRUE
299349
)
300-
expect_equal(out$Mean_Difference, c(4.742403, 2.883987), tolerance = 1e-4, ignore_attr = TRUE)
350+
expect_equal(
351+
out$Mean_Difference,
352+
c(4.742403, 2.883987),
353+
tolerance = 1e-4,
354+
ignore_attr = TRUE
355+
)
301356
expect_identical(out$Parameter, c("education: Group 1", "education: Group 2"))
302357

303358
# pairwise inequality with slopes and grouping
304359
out <- estimate_contrasts(
305360
m,
306361
"time",
307362
by = c("education", "grp"),
308-
comparison = inequality ~ pairwise| grp,
363+
comparison = inequality ~ pairwise | grp,
309364
integer_as_continuous = TRUE
310365
)
311-
expect_equal(out$Mean_Difference, 1.858416, tolerance = 1e-4, ignore_attr = TRUE)
366+
expect_equal(out$Mean_Difference, 1.858416, tolerance = 1e-4, ignore_attr = TRUE)
312367
expect_identical(out$Parameter, "Group 1 - Group 2")
313368

314369
# validate against marginaleffects
@@ -321,15 +376,15 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
321376
integer_as_continuous = TRUE
322377
)
323378
out2 <- marginaleffects::hypotheses(
324-
marginaleffects::avg_slopes(m, variables = "time", by = c("education", "grp"), hypothesis = ~pairwise | grp),
325-
hypothesis = ~I(mean(abs(x))) | grp
326-
)
327-
expect_equal(
328-
out1$Mean_Difference,
329-
out2$estimate,
330-
tolerance = 1e-4,
331-
ignore_attr = TRUE
379+
marginaleffects::avg_slopes(
380+
m,
381+
variables = "time",
382+
by = c("education", "grp"),
383+
hypothesis = ~ pairwise | grp
384+
),
385+
hypothesis = ~ I(mean(abs(x))) | grp
332386
)
387+
expect_equal(out1$Mean_Difference, out2$estimate, tolerance = 1e-4, ignore_attr = TRUE)
333388

334389
# pairwise inequality with slopes and grouping, using string
335390
out <- estimate_contrasts(
@@ -359,7 +414,12 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
359414
comparison = ratio ~ inequality,
360415
integer_as_continuous = TRUE
361416
)
362-
expect_equal(out$Mean_Ratio, c(0.0198939, 1.9717087), tolerance = 1e-4, ignore_attr = TRUE)
417+
expect_equal(
418+
out$Mean_Ratio,
419+
c(0.0198939, 1.9717087),
420+
tolerance = 1e-4,
421+
ignore_attr = TRUE
422+
)
363423
expect_identical(out$Parameter, c("education: Group 1", "education: Group 2"))
364424

365425
out <- estimate_contrasts(
@@ -375,12 +435,12 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
375435
m,
376436
"time",
377437
by = c("education", "grp"),
378-
comparison = ~pairwise | grp,
438+
comparison = ~ pairwise | grp,
379439
integer_as_continuous = TRUE
380440
)
381441
expect_identical(dim(out), c(6L, 9L))
382442
expect_named(
383443
out,
384-
c("Parameter", "grp", "Difference", "SE", "CI_low", "CI_high", "t", "df", "p")
444+
c("Parameter", "grp", "Difference", "SE", "CI_low", "CI_high", "t", "df", "p")
385445
)
386446
})

tests/testthat/test-estimate_slopes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ test_that("estimate_slopes, works with glmmTMB and splines", {
310310
test_that("estimate_slopes, estimate-argument works", {
311311
skip_if(getRversion() < "4.5.0")
312312
skip_if_not_installed("datawizard")
313-
data(penguins)
313+
data(penguins, package = "datasets")
314314
penguins$long_bill <- factor(
315315
datawizard::categorize(penguins$bill_len),
316316
labels = c("short", "long")

tests/testthat/test-ordinal.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ test_that("estimate_relation prints ordinal models correctly", {
2424

2525
# keep row column
2626
out <- suppressWarnings(estimate_relation(m, data = iris[1:3, ], verbose = FALSE))
27-
expect_named(out, c("Row", "Response", "Sepal.Width", "Predicted", "CI_low", "CI_high", "Residuals")) # nolint
27+
expect_named(
28+
out,
29+
c("Row", "Response", "Sepal.Width", "Predicted", "CI_low", "CI_high", "Residuals")
30+
)
2831
expect_identical(dim(out), c(9L, 7L))
2932
})
3033

@@ -34,7 +37,7 @@ test_that("estimate_means, print bracl", {
3437
# required for the penguins dataset, which was added in R 4.5.0
3538
skip_if(getRversion() < "4.5.0")
3639

37-
data(penguins)
40+
data(penguins, package = "datasets")
3841

3942
m <- brglm2::bracl(species ~ island + sex, data = penguins)
4043
out <- estimate_means(m, by = "island")

0 commit comments

Comments
 (0)