New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Results (snapshots) have changed in _marginaleffects_ 0.15.0 #903
Comments
This is not exactly a bug. It’s related to an improvement in functionality, but I did not expect it to break existing code. Sorry about that! The error was not triggered when I checked rev-deps before releasing to CRAN. Is this snapshot in your test suite? I have now added a snapshot test to my suite to make sure this doesn’t change: de7f8b6 Pre-0.15.0, the
As soon as you called This implicit behavior was hard to document and explain. In 0.15.0, I improved it by introducing a new explicit This means that you now get different results if you use library(marginaleffects)
set.seed(123)
dat <- data.frame(
outcome = rbinom(n = 100, size = 1, prob = 0.35),
var_binom = as.factor(rbinom(n = 100, size = 1, prob = 0.2)),
var_cont = rnorm(n = 100, mean = 10, sd = 7),
groups = sample(letters[1:4], size = 100, replace = TRUE)
)
m <- glm(outcome ~ var_binom * var_cont + groups,
data = dat, family = binomial()
)
d <- structure(list(var_binom = structure(1:2, levels = c("0", "1"
), class = "factor"), var_cont = c(9.24717241397544, 9.24717241397544
), groups = structure(c(1L, 1L), levels = "b", class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
# type = NULL or type = "invlink(link)"
predictions(
m, newdata = d, by = "var_binom", hypothesis = "pairwise", transform = "exp"
)$estimate
# [1] 1.61968
# type = "response"
predictions(
m, newdata = d, by = "var_binom", hypothesis = "pairwise", transform = "exp", type = "response"
)$estimate
# [1] 0.9867827 |
Yes, but snapshot tests are not run on CRAN, and furthermore, I think that suggested packages (marginaleffects is suggested by ggeffects) are ignored in rev-dep checks (i.e. reverse-suggests are not run for your package - I think). Thank you for clarifying! One question remains: which of the options now returns contrasts on the odds ratio scale, and which on the probability (response) scale? |
Neither. Both options exponentiate a response scale. The
I'm not sure if that's what you want... |
ok, got it, how set.seed(1234)
dat <- data.frame(
outcome = rbinom(n = 100, size = 1, prob = 0.35),
x1 = as.factor(sample(1:3, size = 100, TRUE, prob = c(0.5, 0.2, 0.3))),
x2 = rnorm(n = 100, mean = 10, sd = 7)
)
m <- glm(outcome ~ x1 + x2, data = dat, family = binomial())
d <- ggeffects::data_grid(m, "x1")
# what is this scale?
marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise"
)$estimate
#> [1] 0.6268201 0.3798960 0.2672561
# contrasts as log-odds?
marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise", type = "link"
)$estimate
#> [1] 0.5185991 -0.4899896 -1.0085887
# contrasts as probabilities?
marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise", type = "response"
)$estimate
#> [1] 0.08277724 -0.10101103 -0.18378827 Created on 2023-09-15 with reprex v2.0.2 |
In other words: If I now want the odds ratio scale, I would use marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise", type = "link", transform = "exp"
) right? |
This command marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise", type = "link", transform = "exp"
) gives you this: This command marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise", type = "response", transform = "exp"
) gives you this: I'll need to keep this issue open to check what happens when Unfortunately I don't have time to check now. |
Ok, and what is the default for logistic regression when neither is provided? # what is this scale?
marginaleffects::predictions(
m, newdata = d, by = "x1", hypothesis = "pairwise"
)$estimate I think then I have everything I need to know :-) |
I think you made me find a really major problem. I'll look at it tonight and will probably have to make an emergency release. |
OK, this should be sorted on Github. I will release a new version to CRAN as soon as possible. See the NEWS for a description of the bug. Thanks again for the report! Note that a warning is now issued in some cases. That warning can be suppressed by choosing the library(marginaleffects)
set.seed(123)
dat <- data.frame(
outcome = rbinom(n = 100, size = 1, prob = 0.35),
var_binom = as.factor(rbinom(n = 100, size = 1, prob = 0.2)),
var_cont = rnorm(n = 100, mean = 10, sd = 7),
groups = sample(letters[1:4], size = 100, replace = TRUE)
)
m <- glm(outcome ~ var_binom * var_cont + groups,
data = dat, family = binomial()
)
d <- structure(list(var_binom = structure(1:2, levels = c("0", "1"
), class = "factor"), var_cont = c(9.24717241397544, 9.24717241397544
), groups = structure(c(1L, 1L), levels = "b", class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
predictions(
m, newdata = d, by = "var_binom", hypothesis = "pairwise", transform = "exp"
)$estimate
# Warning: The `type="invlink"` argument is not available unless `hypothesis` is
# `NULL` or a single number. The value of the `type` argument was changed
# to "response" automatically. To suppress this warning, use
# `type="response"` explicitly in your function call.
# [1] 0.9867827
predictions(
m, newdata = d, by = "var_binom", hypothesis = "pairwise", transform = "exp", type = "invlink(link)"
)$estimate
# Warning: The `type="invlink"` argument is not available unless `hypothesis` is
# `NULL` or a single number. The value of the `type` argument was changed
# to "response" automatically. To suppress this warning, use
# `type="response"` explicitly in your function call.
# [1] 0.9867827
predictions(
m, newdata = d, by = "var_binom", hypothesis = "pairwise", transform = "exp", type = "response",
)$estimate
# [1] 0.9867827 |
Great! To summarize:
right? As an example: for logistic regression, |
But you're right: The difference in two link scale predictions made by incrementing a single variable is the corresponding regression coefficient, and exponentiating a logit coefficient gives us an odds ratio. |
btw, the changes in marginaleffects did not really "break" any code on my side, they just returned different results. So there's no need for me to update ggeffects as well, because results will be correct when the current dev of marginaleffects is installed. I just need to update the snapshot tests sometime... |
Yeah, but the 1.62 result is incorrect... |
But that result should automatically change (to the correct one) once marginaleffects is updated, right? |
Absolutely! |
on cran now |
Hi Vincent,
I just saw that some tests are failing for ggeffects, and it turned out that something has changed since the latest marginaleffects update to 0.15.0 - not sure if this was a bug fix or is a new bug.
See here results from version 0.15.0:
Created on 2023-09-15 with reprex v2.0.2
And here for version 0.14.0:
Could you clarify whether the changes are intentional or a bug?
The text was updated successfully, but these errors were encountered: