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

warning from tidy_plus_plus for glm with subset argument #278

Open
richardjtelford opened this issue Dec 23, 2024 · 1 comment
Open

warning from tidy_plus_plus for glm with subset argument #278

richardjtelford opened this issue Dec 23, 2024 · 1 comment

Comments

@richardjtelford
Copy link

I'm getting a warning with gtsummary::tbl_regression() for a glm with a subset argument (initial bug report ddsjoberg/gtsummary/issues/2111)

The problem is coming from broom.helpers::tidy_plus_plus()

mod <- glm(mpg ~ gear, data = mtcars, subset = mpg < 30)
broom.helpers::tidy_plus_plus(mod)
#> Warning in tcm * w: longer object length is not a multiple of shorter object
#> length
#> # A tibble: 1 × 17
#>   term  variable var_label var_class var_type   var_nlevels contrasts
#>   <chr> <chr>    <chr>     <chr>     <chr>            <int> <chr>    
#> 1 gear  gear     gear      numeric   continuous          NA <NA>     
#> # ℹ 10 more variables: contrasts_type <chr>, reference_row <lgl>, label <chr>,
#> #   n_obs <dbl>, estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>, conf.low <dbl>, conf.high <dbl>

Created on 2024-12-23 with reprex v2.1.1

The underlying issue is that in broom.helpers:::model_get_n.glm()

` `` r
tcm <- model_compute_terms_contributions(model)
if (is.null(tcm)) {
return(NULL)
}
w <- model_get_weights(model)

the number of rows in `tcm` from `model_compute_terms_contributions()` reflects the original data.frame, whereas `w` reflects the size of the data.frame after subsetting. 

``` r
nrow(mtcars) # original number of rows
#> [1] 32
nrow(mtcars[mtcars$mpg < 30, ]) # n rows after subsetting
#> [1] 28

mod <- glm(mpg ~ gear, data = mtcars, subset = mpg < 30)
broom.helpers:::model_compute_terms_contributions.default(mod) |> 
  nrow()
#> [1] 32 # original number

broom.helpers::model_get_weights(mod) |> length()
#> [1] 28 # correct


nrow(mod$model)
#> [1] 28

nrow(mod$data)
#> [1] 32

Created on 2024-12-23 with reprex v2.1.1

broom.helpers:::model_compute_terms_contributions.default() is using model$data, perhaps it should be using model$model

Everything works as expected with a lm() model with the subset argument (I've not tried other models).

@larmarange
Copy link
Owner

Thanks for the report. I will have a look at it. Not sure exactly when (holiday season 😉)

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

No branches or pull requests

2 participants