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

basic support for `zeroinfl()` and `hurdle()` and `betareg()` models #233

Closed larmarange closed 1 year ago

larmarange commented 1 year ago

fix #232 fix #234

larmarange commented 1 year ago

Just added basic support of pairwise contrasts for zeroinfl() models.

Still unsure if this is the correct approach. Right now, we use mode = "count" and mode = "zero" for each component. See https://cran.r-project.org/web/packages/emmeans/vignettes/models.html#C

Would the same approach be relevant for beta regression with mode = "response" and mode = "precision".

I'm not sufficiently familiar with these types of models.

A more conservative approach would be to not support pairwise contrast for such models (and throw an error), and letting users to call themselves emmeans. Support could be added later, once we have feedbacks about what should be expected.

@ddsjoberg What do you think?

library(pscl)
#> Classes and Methods for R developed in the
#> Political Science Computational Laboratory
#> Department of Political Science
#> Stanford University
#> Simon Jackman
#> hurdle and zeroinfl functions by Achim Zeileis
library(broom.helpers)

mod <- zeroinfl(art ~ fem + mar + phd | fem, data = bioChemists)

mod |> 
  tidy_plus_plus(add_pairwise_contrasts = TRUE, keep_model_terms = TRUE) |> 
  knitr::kable()
#> ℹ <zeroinfl> model detected.
#> ✔ `tidy_zeroinfl()` used instead.
#> ℹ Add `tidy_fun = broom.helpers::tidy_zeroinfl` to quiet these messages.
component term original_term variable var_label var_class var_type var_nlevels contrasts contrasts_type reference_row label n_obs estimate std.error conf.level conf.low conf.high statistic df.error p.value
conditional femMen NA fem fem factor dichotomous 2 contr.treatment treatment TRUE Men 494 0.0000000 NA NA NA NA NA NA NA
conditional femWomen count_femWomen fem fem factor dichotomous 2 contr.treatment treatment FALSE Women 421 -0.2321456 0.0638427 0.95 -0.3572750 -0.1070162 -3.6362116 Inf 0.0002767
zero_inflated femMen NA fem fem factor dichotomous 2 contr.treatment treatment TRUE Men 494 0.0000000 NA NA NA NA NA NA NA
zero_inflated femWomen zero_femWomen fem fem factor dichotomous 2 contr.treatment treatment FALSE Women 421 0.0219932 0.2429610 0.95 -0.4542016 0.4981881 0.0905216 Inf 0.9278727
NA Women - Men NA fem fem factor dichotomous 2 pairwise pairwise NA Women - Men NA -0.3910931 0.1044908 NA -0.5958913 -0.1862949 -3.7428481 NA 0.0001819
conditional marSingle NA mar mar factor dichotomous 2 contr.treatment treatment TRUE Single 309 0.0000000 NA NA NA NA NA NA NA
conditional marMarried count_marMarried mar mar factor dichotomous 2 contr.treatment treatment FALSE Married 606 0.0349971 0.0619596 0.95 -0.0864415 0.1564358 0.5648372 Inf 0.5721845
NA Married - Single NA mar mar factor dichotomous 2 pairwise pairwise NA Married - Single NA 0.0581360 0.1023191 NA -0.1424058 0.2586778 0.5681832 NA 0.5699106
conditional phd count_phd phd phd numeric continuous 2 NA NA NA phd 915 0.0647841 0.0285166 0.95 0.0088925 0.1206756 2.2718005 Inf 0.0230986

Created on 2023-08-06 with reprex v2.0.2

ddsjoberg commented 1 year ago

Ooof, I tend to agree with your second comment. It's not a model I've ever used (outside of a brief introduction in school), I would just print a note that it's not supported.

codecov[bot] commented 1 year ago

Codecov Report

Merging #233 (e3cab2f) into main (7c1b1a0) will decrease coverage by 0.52%. The diff coverage is 87.82%.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233/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://app.codecov.io/gh/larmarange/broom.helpers/pull/233?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 #233 +/- ## ========================================== - Coverage 95.99% 95.47% -0.52% ========================================== Files 43 43 Lines 2544 2676 +132 ========================================== + Hits 2442 2555 +113 - Misses 102 121 +19 ``` | [Files Changed](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange) | Coverage Δ | | |---|---|---| | [R/custom\_tidiers.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9jdXN0b21fdGlkaWVycy5S) | `86.57% <83.78%> (-3.17%)` | :arrow_down: | | [R/tidy\_add\_header\_rows.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9oZWFkZXJfcm93cy5S) | `95.85% <87.50%> (-4.15%)` | :arrow_down: | | [R/broom.helpers-package.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9icm9vbS5oZWxwZXJzLXBhY2thZ2UuUg==) | `97.22% <100.00%> (ø)` | | | [R/model\_get\_contrasts.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfY29udHJhc3RzLlI=) | `96.42% <100.00%> (+1.42%)` | :arrow_up: | | [R/model\_get\_model\_matrix.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfbW9kZWxfbWF0cml4LlI=) | `82.85% <100.00%> (+1.60%)` | :arrow_up: | | [R/model\_get\_pairwise\_contrasts.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfcGFpcndpc2VfY29udHJhc3RzLlI=) | `90.90% <100.00%> (+0.66%)` | :arrow_up: | | [R/model\_get\_terms.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfdGVybXMuUg==) | `91.66% <100.00%> (+0.75%)` | :arrow_up: | | [R/model\_get\_xlevels.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9nZXRfeGxldmVscy5S) | `100.00% <100.00%> (ø)` | | | [R/model\_list\_terms\_levels.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui9tb2RlbF9saXN0X3Rlcm1zX2xldmVscy5S) | `100.00% <100.00%> (ø)` | | | [R/tidy\_add\_pairwise\_contrasts.R](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange#diff-Ui90aWR5X2FkZF9wYWlyd2lzZV9jb250cmFzdHMuUg==) | `86.48% <100.00%> (ø)` | | | ... and [2 more](https://app.codecov.io/gh/larmarange/broom.helpers/pull/233?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Joseph+Larmarange) | |
yuryzablotski commented 12 months ago

Hi guys,

good new. I managed to find the way to make "add_pairwise_contrasts = T" work with beta-regression. I though you would wanna know that. With the "betareg" package, it still don't work, but it gives a message to use "emmeans", which is fine, but very cumbersome for multivariable models. Ben Bolker also said that's it's perfectly fine to use glmmTMB without random effect! Most of the time the results of betareg and glmmTMB with beta_family are identical. Sometimes a bit different. But the possibility to use {gtsummary} and {broom.helpers} with it makes glmmTMB more valuable! The only thing it does not do is to rename exp(Beta) into OR. But that's no problem at all.

So, I just wanted to let you know, because may be you can build new features into the gtsummary (& broom.helpers) for beta regression (and may be even more, like zeroinfl etc) via glmmTMB package much easier then before. Below is an example without data.

Kind regards and thanks again for your work with broom.helpers and gtsummary. They are amazing!

library(glmmTMB) mod <- glmmTMB(percent ~ breed_kat, data=bla, family = beta_family()) car::Anova(mod) plot_model(mod, type = "pred") tbl_regression(mod, add_pairwise_contrasts = T, exponentiate = T) emmeans(mod, ~ breed_kat, type = "response") %>% pairs(reverse = TRUE)