Closed larmarange closed 1 year ago
library(broom.helpers)
library(gtsummary)
#>
#> Attachement du package : 'gtsummary'
#> Les objets suivants sont masqués depuis 'package:broom.helpers':
#>
#> all_continuous, all_contrasts
mod <- glm(response ~ stage + grade + age, family = binomial, data = trial)
mod %>%
tbl_regression(
tidy_fun = tidy_margins,
estimate_fun = scales::label_percent(style_positive = "plus")
) %>%
as_kable()
Characteristic | Average Marginal Effects | 95% CI | p-value |
---|---|---|---|
Age | +0.41% | -0.1%, +0.87% | 0.084 |
Grade | |||
I | — | — | |
II | -3.59% | -19.9%, +12.72% | 0.7 |
III | +0.95% | -15.5%, +17.37% | >0.9 |
T Stage | |||
T1 | — | — | |
T2 | -11.81% | -29.7%, +6.06% | 0.2 |
T3 | -2.17% | -22.3%, +17.97% | 0.8 |
T4 | -5.88% | -25.3%, +13.52% | 0.6 |
mod %>%
tbl_regression(
tidy_fun = tidy_all_effects,
estimate_fun = scales::label_percent()
) %>%
as_kable()
Characteristic | Marginal Effects | 95% CI |
---|---|---|
T Stage | ||
T1 | 36.60% | 24.26%, 50.984% |
T2 | 24.65% | 14.75%, 38.234% |
T3 | 34.40% | 21.17%, 50.582% |
T4 | 30.63% | 18.58%, 46.079% |
Grade | ||
I | 32.07% | 21.71%, 44.548% |
II | 28.42% | 18.18%, 41.496% |
III | 33.04% | 22.21%, 46.026% |
Age | ||
6 | 16.97% | 6.94%, 35.916% |
30 | 24.54% | 16.15%, 35.447% |
40 | 28.30% | 21.42%, 36.372% |
60 | 36.76% | 27.86%, 46.671% |
80 | 46.13% | 28.25%, 65.057% |
mod %>%
tbl_regression(
tidy_fun = tidy_ggpredict,
estimate_fun = scales::label_percent()
) %>%
as_kable()
#> Data were 'prettified'. Consider using `terms="age [all]"` to get smooth
#> plots.
Characteristic | Conditional Effects | 95% CI |
---|---|---|
T Stage | ||
T1 | 37.54% | 22.15%, 55.947% |
T2 | 25.41% | 13.58%, 42.493% |
T3 | 35.31% | 20.47%, 53.651% |
T4 | 31.50% | 17.25%, 50.343% |
Grade | ||
I | 37.54% | 22.15%, 55.947% |
II | 33.58% | 19.93%, 50.660% |
III | 38.59% | 22.59%, 57.495% |
Age | ||
0 | 19.44% | 6.24%, 46.657% |
10 | 22.65% | 8.78%, 47.103% |
20 | 26.22% | 12.00%, 48.074% |
30 | 30.13% | 15.75%, 49.864% |
40 | 34.35% | 19.62%, 52.867% |
50 | 38.84% | 23.04%, 57.396% |
60 | 43.53% | 25.59%, 63.336% |
70 | 48.33% | 27.24%, 70.034% |
80 | 53.16% | 28.19%, 76.648% |
90 | 57.94% | 28.67%, 82.524% |
Created on 2022-12-10 with reprex v2.0.2
@ddsjoberg I had to prepare a course on marginal effects and I was realising that they were no nicely formatted output for such a thing.
This is exploratory work, but it seems quite easy to implement using customer tieders in broom.helpers
(and some other tweaks in other functions).
As you can see, the results are nicely formatted by tbl_regression()
, out of the box!
It probably needs to mature a little. Do not hesitate if you have any feedback.
@larmarange the update looks awesome!
@ddsjoberg Any comment about the name of the estimate column?
@ddsjoberg Any comment about the name of the estimate column?
I think estimate
is the best name!
@ddsjoberg Any comment about the name of the estimate column?
I think
estimate
is the best name!
Currently, I'm using "Average Marginal Effect", "Marginal Effect" and "Conditional Effect". I would prefer to be explicit (as we do with OR for logistic regression).
Does that seem OK? Or would you prefer "AME"? (but less known)
@ddsjoberg do you want to have a look at this PR before merge?
Merging #195 (37156c0) into main (9a19921) will decrease coverage by
0.25%
. The diff coverage is93.33%
.
First draft of support of margin effects (with
effects::allEffects()
), average margin effects (frommargins::margins()
) and conditional effects (withggeffects::ggpredict()
).Need to mature and to be tested. Requires proper documentation, unit tests and examples.