Skip to content

Commit 21adf83

Browse files
committed
fix for glm
1 parent 4ea1878 commit 21adf83

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

R/format.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,9 @@ equivalence_columns <- c(
838838
# estimate name
839839
if (is.null(predict_type)) {
840840
estimate_name <- "Mean"
841-
} else if (!is.null(predict_type) && tolower(predict_type) %in% .brms_aux_elements()) {
841+
} else if (predict_type == "context") {
842+
estimate_name <- "Estimate"
843+
} else if (tolower(predict_type) %in% .brms_aux_elements()) {
842844
# for Bayesian models with distributional parameter
843845
estimate_name <- tools::toTitleCase(predict_type)
844846
} else if (

R/get_contexteffects.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
# special contrasts: context effects ----------------------------------------
22
# ---------------------------------------------------------------------------
33

4-
get_contexteffects <- function(model, my_args, ci, ...) {
5-
out <- marginaleffects::avg_comparisons(
6-
model,
7-
variables = my_args$contrast,
8-
hypothesis = my_args$comparison,
9-
...
10-
)
4+
get_contexteffects <- function(model, my_args, model_info, ...) {
5+
if (model_info$is_linear) {
6+
out <- marginaleffects::avg_comparisons(
7+
model,
8+
variables = my_args$contrast,
9+
hypothesis = my_args$comparison,
10+
...
11+
)
12+
} else {
13+
out <- marginaleffects::avg_comparisons(
14+
model,
15+
variables = my_args$contrast,
16+
hypothesis = my_args$comparison,
17+
type = "link",
18+
transform = "exp",
19+
...
20+
)
21+
}
1122
# save some labels for printing
1223
attr(out, "by") <- my_args$by
1324
attr(out, "contrast") <- my_args$contrast

R/get_marginalcontrasts.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ get_marginalcontrasts <- function(
102102
)
103103
predict <- "response"
104104
} else if (isTRUE(my_args$context_effects)) {
105-
out <- get_contexteffects(model, my_args, ci, ...)
106-
predict <- "response"
105+
out <- get_contexteffects(model, my_args, model_info, ...)
106+
predict <- "context"
107107
} else if (compute_slopes) {
108108
# sanity check - contrast for slopes only makes sense when we have a "by" argument
109109
if (is.null(my_args$by)) {

tests/testthat/test-estimate_contrasts_context.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ skip_on_os("mac")
44
skip_if(getRversion() < "4.5.0")
55
skip_if_not_installed("datawizard")
66

7-
test_that("estimate_contrast, context effects", {
7+
test_that("estimate_contrast, context effects, linear", {
88
data(penguins, package = "datasets")
99
d <- datawizard::demean(penguins, "bill_len", by = "species")
1010
m <- lm(bill_dep ~ bill_len_between + bill_len_within, data = d)
@@ -20,3 +20,20 @@ test_that("estimate_contrast, context effects", {
2020
expect_equal(out$Mean, b[1] - b[2], tolerance = 1e-4, ignore_attr = TRUE)
2121
expect_equal(out$SE, sqrt((se[1]^2 + se[2]^2)), tolerance = 1e-4, ignore_attr = TRUE)
2222
})
23+
24+
test_that("estimate_contrast, context effects, linear", {
25+
data(penguins, package = "datasets")
26+
d <- datawizard::demean(penguins, "bill_len", by = "species")
27+
d$out <- datawizard::categorize(d$flipper_len) - 1
28+
m <- glm(out ~ bill_len_between + bill_len_within, data = d, family = binomial())
29+
30+
b <- coef(summary(m))[2:3, 1]
31+
se <- coef(summary(m))[2:3, 2]
32+
33+
out <- modelbased::estimate_contrasts(
34+
m,
35+
c("bill_len_between", "bill_len_within"),
36+
comparison = "context"
37+
)
38+
expect_equal(out$Estimate, exp(b[1] - b[2]), tolerance = 1e-4, ignore_attr = TRUE)
39+
})

0 commit comments

Comments
 (0)