Closed arielhasidim closed 1 year ago
This is likely because of the automatic back transform described on this page: https://vincentarelbundock.github.io/marginaleffects/articles/predictions.html
You can probably specify the type
argument explicitly to get identical results.
Thank's @vincentarelbundock!
I did manage to replicate the identical results using the type
argument, but I'm afraid there are still some inconsistencies:
transform = NULL
argument still gives a different result than transform = function(x) x
(it turns out the same as without the transform
argument).marginaleffects:::type_dictionary
df, glm
class should get type = "response"
by default, but stating type = "response"
argument is how I replicated the result that I got with the transform = function(x) x
argument (which differs from the result without type
and transform
arguments, although this should be the default).type
option that is available for glm
in this dictionary is "link"
, which gives a third different result. I'm not able to reproduce the original result with any of the available type
options.Leaving here the third result:
example <- glm(vs ~ am * (mpg + cyl + disp + hp + drat + wt),
data = mtcars,
family = binomial())
avg_predictions(example,
variables = "am",
type= "link"
)
Resulted in: | am | estimate | std.error | statistic | p.value | conf.low | conf.high |
---|---|---|---|---|---|---|---|
0 | 19.58492 | 169,918.80 | 0.00011526 | 0.999908 | -333,015.20 | 333,054.30 | |
1 | -253.43351 | 765,612.70 | -0.000331 | 0.9997359 | ########## | ########## |
It’s easier to see the equivalences with a model that converges. See the equivalences below. And please read the paragraph here, paying special attention to the first sentence: https://vincentarelbundock.github.io/marginaleffects/articles/reference/predictions.html#details
library(marginaleffects)
example <- glm(vs ~ am * mpg,
data = mtcars,
family = binomial())
avg_predictions(example,
variables = "am",
type = "response")
#
# am Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
# 0 0.553 0.0496 11.16 <0.001 93.6 0.456 0.650
# 1 0.296 0.0856 3.45 <0.001 10.8 0.128 0.464
#
# Columns: am, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
avg_predictions(example,
variables = "am",
transform = \(x) x)
#
# am Estimate Pr(>|z|) S 2.5 % 97.5 %
# 0 0.553 <0.001 93.6 0.456 0.650
# 1 0.296 <0.001 10.8 0.128 0.464
#
# Columns: am, estimate, p.value, s.value, conf.low, conf.high
predictions(example,
variables = "am",
type = "link",
transform = \(x) plogis(x)) |>
aggregate(estimate ~ am, data = _, FUN = mean)
# am estimate
# 1 0 0.5532474
# 2 1 0.2958730
I'm using the
transform =
argument in theavg_predictions()
function in order to present the treatment effect on the predicted proportions of a binary outcome. The documentation regarding thetransform
argument states that:That's why I think it's odd that the
transform = function(x) x
shows different result than without the trandform.For example:
Gives:
While:
sessionInfo()
: