larmarange / broom.helpers

A set of functions to facilitate manipulation of tibbles produced by broom
https://larmarange.github.io/broom.helpers/
GNU General Public License v3.0
21 stars 8 forks source link

Displaying margin effects, average margin effects or conditional effects #195

Closed larmarange closed 1 year ago

larmarange commented 1 year ago

First draft of support of margin effects (with effects::allEffects()), average margin effects (from margins::margins()) and conditional effects (with ggeffects::ggpredict()).

Need to mature and to be tested. Requires proper documentation, unit tests and examples.

larmarange commented 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

larmarange commented 1 year ago

Relevant documentation:

larmarange commented 1 year ago

@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.

ddsjoberg commented 1 year ago

@larmarange the update looks awesome!

larmarange commented 1 year ago

@ddsjoberg Any comment about the name of the estimate column?

ddsjoberg commented 1 year ago

@ddsjoberg Any comment about the name of the estimate column?

I think estimate is the best name!

larmarange commented 1 year ago

@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)

larmarange commented 1 year ago

@ddsjoberg do you want to have a look at this PR before merge?

codecov[bot] commented 1 year ago

Codecov Report

Merging #195 (37156c0) into main (9a19921) will decrease coverage by 0.25%. The diff coverage is 93.33%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/larmarange/broom.helpers/pull/195/graphs/tree.svg?width=650&height=150&src=pr&token=onefg3YI04&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange)](https://codecov.io/gh/larmarange/broom.helpers/pull/195?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange) ```diff @@ Coverage Diff @@ ## main #195 +/- ## ========================================== - Coverage 97.80% 97.54% -0.26% ========================================== Files 40 40 Lines 1870 1958 +88 ========================================== + Hits 1829 1910 +81 - Misses 41 48 +7 ``` | [Impacted Files](https://codecov.io/gh/larmarange/broom.helpers/pull/195?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange) | Coverage Δ | | |---|---|---| | [R/tidy\_add\_term\_labels.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF90ZXJtX2xhYmVscy5S) | `97.56% <72.72%> (-2.44%)` | :arrow_down: | | [R/tidiers.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWRpZXJzLlI=) | `93.75% <91.11%> (-1.78%)` | :arrow_down: | | [R/tidy\_add\_coefficients\_type.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9jb2VmZmljaWVudHNfdHlwZS5S) | `100.00% <100.00%> (ø)` | | | [R/tidy\_add\_pairwise\_contrasts.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9wYWlyd2lzZV9jb250cmFzdHMuUg==) | `94.73% <100.00%> (+0.98%)` | :arrow_up: | | [R/tidy\_add\_reference\_rows.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9yZWZlcmVuY2Vfcm93cy5S) | `100.00% <100.00%> (ø)` | | | [R/tidy\_and\_attach.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FuZF9hdHRhY2guUg==) | `100.00% <100.00%> (ø)` | | | [R/tidy\_identify\_variables.R](https://codecov.io/gh/larmarange/broom.helpers/pull/195/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2lkZW50aWZ5X3ZhcmlhYmxlcy5S) | `100.00% <100.00%> (ø)` | |