Skip to content
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

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

Merged
merged 22 commits into from Dec 24, 2022

Conversation

larmarange
Copy link
Owner

@larmarange larmarange commented Dec 9, 2022

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.

  • NEWS
  • unit tests

@larmarange
Copy link
Owner Author

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
Copy link
Owner Author

@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
Copy link
Collaborator

@larmarange the update looks awesome!

@larmarange
Copy link
Owner Author

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

@ddsjoberg
Copy link
Collaborator

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

I think estimate is the best name!

@larmarange
Copy link
Owner Author

@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
Copy link
Owner Author

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

@codecov
Copy link

codecov bot commented Dec 22, 2022

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

@@            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 Coverage Δ
R/tidy_add_term_labels.R 97.56% <72.72%> (-2.44%) ⬇️
R/tidiers.R 93.75% <91.11%> (-1.78%) ⬇️
R/tidy_add_coefficients_type.R 100.00% <100.00%> (ø)
R/tidy_add_pairwise_contrasts.R 94.73% <100.00%> (+0.98%) ⬆️
R/tidy_add_reference_rows.R 100.00% <100.00%> (ø)
R/tidy_and_attach.R 100.00% <100.00%> (ø)
R/tidy_identify_variables.R 100.00% <100.00%> (ø)

@larmarange larmarange merged commit fceda0f into main Dec 24, 2022
@larmarange larmarange deleted the margins branch December 24, 2022 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants