ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.05k stars 122 forks source link

tbl_regression() fails for 'stanreg' models #852

Closed BeauMeche closed 3 years ago

BeauMeche commented 3 years ago

Greetings, I noticed an issue in the performance of the last update. After installing update 1.3.7, I am unable to display the results from my model fit with rstanarm::stan_glm(). I have temporarily re-installed version 1.3.5 as a workaround to display these models, for which the code below produces the properly formatted table. Here is an example of the error given while using v.1.3.7:


library(rstanarm)
library(gtsummary)
library(broom.mixed)

fit_1 <- stan_glm(formula = mpg ~ cyl + wt,
                  data = mtcars,
                  family = gaussian(),
                  refresh = 0)

tbl_regression(fit_1)

#> Error in colSums(tcm * w): 'x' must be an array of at least two dimensions

Let me know if there is any other information I can provide in effort to sort this out. Thanks!

ddsjoberg commented 3 years ago

Thank you @BeauMeche for the report! We'll investigate ASAP

ddsjoberg commented 3 years ago

Hey @BeauMeche !

Can you download the dev version of broom.helpers and give it a shot? devtools::install_github("larmarange/broom.helpers")

library(magrittr)

packageVersion("broom.helpers")
#> [1] '1.2.1.9000'
packageVersion("gtsummary")
#> [1] '1.3.7'

fit_1 <- rstanarm::stan_glm(formula = mpg ~ cyl + wt,
                            data = mtcars,
                            family = gaussian(),
                            refresh = 0)

broom.mixed::tidy(fit_1)
#> # A tibble: 3 x 3
#>   term        estimate std.error
#>   <chr>          <dbl>     <dbl>
#> 1 (Intercept)    39.7      1.71 
#> 2 cyl            -1.52     0.426
#> 3 wt             -3.20     0.765
broom.helpers::tidy_plus_plus(fit_1, tidy_fun = broom.mixed::tidy)
#> # A tibble: 2 x 15
#>   term  variable var_label var_class var_type   var_nlevels contrasts
#>   <chr> <chr>    <chr>     <chr>     <chr>            <int> <chr>    
#> 1 cyl   cyl      cyl       numeric   continuous          NA <NA>     
#> 2 wt    wt       wt        numeric   continuous          NA <NA>     
#> # ... with 8 more variables: contrasts_type <chr>, reference_row <lgl>,
#> #   label <chr>, n_obs <dbl>, estimate <dbl>, std.error <dbl>, conf.low <dbl>,
#> #   conf.high <dbl>
gtsummary::tbl_regression(fit_1, tidy_fun = broom.mixed::tidy) %>%
  gtsummary::as_tibble()
#> # A tibble: 2 x 3
#>   `**Characteristic**` `**Beta**` `**95% CI**`
#>   <chr>                <chr>      <chr>       
#> 1 cyl                  -1.5       -2.3, -0.65 
#> 2 wt                   -3.2       -4.8, -1.6

Created on 2021-04-08 by the reprex package (v2.0.0)

ddsjoberg commented 3 years ago

hey @BeauMeche ! I think this is all ready to go in the dev version. We'll be releasing to CRAN in the next few days. Thank you for the bug report. Please re-open the issue if needed.

library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.3.7.9022'

fit_1 <- rstanarm::stan_glm(formula = mpg ~ cyl + wt,
                            data = mtcars,
                            family = gaussian(),
                            refresh = 0)

tbl_regression(fit_1) %>%
  as_kable() # convert to kable to display on GitHub
Characteristic Beta 95% CI
cyl -1.5 -2.3, -0.67
wt -3.2 -4.7, -1.7

Created on 2021-04-10 by the reprex package (v2.0.0)

BeauMeche commented 3 years ago

The dev version works perfectly, I'll keep an eye out for the CRAN release. Thanks @ddsjoberg!