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

Support for pairwise contrasts from emmeans #192

Closed larmarange closed 1 year ago

larmarange commented 1 year ago
ddsjoberg commented 1 year ago

hey hey @larmarange !

this all looks great!

I was doing some testing with other types of contrasts. do you think we should document in the help file how various contrasts are handled? for example, if you make a ploy contrast from a factor variable, it is ignored by this new function.

larmarange commented 1 year ago

I need to check. I guess that this is not covered by emmeans. But not sure

larmarange commented 1 year ago

It seems to work indeed. What was your exact test?

library(gtsummary)
library(broom.helpers)
#> 
#> Attachement du package : 'broom.helpers'
#> Les objets suivants sont masqués depuis 'package:gtsummary':
#> 
#>     all_continuous, all_contrasts

mod <- glm(
  response ~ stage + grade + trt,
  gtsummary::trial,
  family = binomial,
  contrasts = list(
    stage = contr.sum,
    grade = contr.helmert,
    trt = contr.poly
  )
)

mod |> 
  tbl_regression(exponentiate = TRUE, add_pairwise_contrasts = TRUE) |> 
  as_kable()
Characteristic OR 95% CI p-value
T Stage
T2 / T1 0.61 0.20, 1.86 0.7
T3 / T1 1.10 0.35, 3.45 >0.9
T3 / T2 1.82 0.56, 5.94 0.6
T4 / T1 0.81 0.27, 2.42 >0.9
T4 / T2 1.33 0.42, 4.18 >0.9
T4 / T3 0.73 0.23, 2.34 0.9
Grade
II / I 0.96 0.39, 2.37 >0.9
III / I 1.13 0.47, 2.76 >0.9
III / II 1.19 0.48, 2.95 0.9
Chemotherapy Treatment
Drug B / Drug A 1.24 0.67, 2.29 0.5

Created on 2022-11-23 with reprex v2.0.2

ddsjoberg commented 1 year ago

@larmarange that is great! The contrast I looked at was the one from ordered factor variables:

library(broom.helpers)

mod <- glm(response ~ factor(stage, ordered = TRUE) + trt, gtsummary::trial, family = binomial)

mod %>%
  tidy_and_attach(exponentiate = TRUE) %>%
  tidy_add_pairwise_contrasts() %>%
  dplyr::select(term, variable, var_class, var_type, contrasts, contrasts_type, estimate) %>%
  knitr::kable()
term variable var_class var_type contrasts contrasts_type estimate
(Intercept) (Intercept) NA intercept NA NA 0.4153831
factor(stage, ordered = TRUE).L factor(stage, ordered = TRUE) ordered continuous contr.poly poly 1.0009112
factor(stage, ordered = TRUE).Q factor(stage, ordered = TRUE) ordered continuous contr.poly poly 1.0862980
factor(stage, ordered = TRUE).C factor(stage, ordered = TRUE) ordered continuous contr.poly poly 0.6407352
Drug B / Drug A trt character dichotomous pairwise pairwise 1.2431465

Created on 2022-11-23 with reprex v2.0.2

codecov[bot] commented 1 year ago

Codecov Report

Merging #192 (c27d9b3) into main (46e8c6d) will decrease coverage by 0.23%. The diff coverage is 93.33%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/larmarange/broom.helpers/pull/192/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/192?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 #192 +/- ## ========================================== - Coverage 98.03% 97.80% -0.24% ========================================== Files 38 40 +2 Lines 1785 1870 +85 ========================================== + Hits 1750 1829 +79 - Misses 35 41 +6 ``` | [Impacted Files](https://codecov.io/gh/larmarange/broom.helpers/pull/192?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange) | Coverage Δ | | |---|---|---| | [R/model\_get\_pairwise\_contrasts.R](https://codecov.io/gh/larmarange/broom.helpers/pull/192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfcGFpcndpc2VfY29udHJhc3RzLlI=) | `88.88% <88.88%> (ø)` | | | [R/tidy\_add\_pairwise\_contrasts.R](https://codecov.io/gh/larmarange/broom.helpers/pull/192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9wYWlyd2lzZV9jb250cmFzdHMuUg==) | `93.75% <93.75%> (ø)` | | | [R/broom.helpers-package.R](https://codecov.io/gh/larmarange/broom.helpers/pull/192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9icm9vbS5oZWxwZXJzLXBhY2thZ2UuUg==) | `100.00% <100.00%> (ø)` | | | [R/tidy\_and\_attach.R](https://codecov.io/gh/larmarange/broom.helpers/pull/192/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\_plus\_plus.R](https://codecov.io/gh/larmarange/broom.helpers/pull/192/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X3BsdXNfcGx1cy5S) | `100.00% <100.00%> (ø)` | |