bbolker / broom.mixed

tidy methods for mixed models in R
228 stars 23 forks source link

Error in random component of `tidy.glmmTMB()` #136

Closed ddsjoberg closed 1 year ago

ddsjoberg commented 1 year ago

Hello! Thank you again for this incredibly useful package!

A gtsummary user reported an issue with a repeated random parameter being printed. I did some investigating, I think the error is in tidy.glmmTMB(). In the example below, I am interested in reporting the component = "cond" portions of the model. When I add this to the tidy call, the random components "cond" and "zi" are both reported. The zi component still appears, but with the incorrect estimate, but with the correct confidence interval.

Just in case, I installed the dev version of glmmTMB and tested on that version as well.

Thank you!

# remotes::install_github("glmmTMB/glmmTMB", subdir = 'glmmTMB')

library(glmmTMB)
packageVersion("glmmTMB")
#> [1] '1.1.4'

data(Salamanders)

zinbm2 = glmmTMB(count~spp + mined +(1|site), zi=~spp + mined + (1|site), Salamanders, family=nbinom2)

broom.mixed::tidy(zinbm2, conf.int = TRUE) |> 
  dplyr::filter(effect %in% "ran_pars") |> 
  dplyr::select(effect, component, group, term, estimate, conf.low, conf.high)
#> # A tibble: 2 × 7
#>   effect   component group term            estimate conf.low conf.high
#>   <chr>    <chr>     <chr> <chr>              <dbl>    <dbl>     <dbl>
#> 1 ran_pars cond      site  sd__(Intercept)    0.137   0.0279     0.675
#> 2 ran_pars zi        site  sd__(Intercept)    1.22    0.721      2.07
broom.mixed::tidy(zinbm2, conf.int = TRUE, component = "cond") |> 
  dplyr::filter(effect %in% "ran_pars") |> 
  dplyr::select(effect, component, group, term, estimate, conf.low, conf.high)
#> # A tibble: 2 × 7
#>   effect   component group term            estimate conf.low conf.high
#>   <chr>    <chr>     <chr> <chr>              <dbl>    <dbl>     <dbl>
#> 1 ran_pars cond      site  sd__(Intercept)    0.137   0.0279     0.675
#> 2 ran_pars cond      site  sd__(Intercept)    0.137   0.721      2.07

Created on 2022-10-31 with reprex v2.0.2

ddsjoberg commented 1 year ago

Thank you @bbolker 🍁

JPRRamalho commented 1 year ago

Good evening. Thank you for the prompt fix.

After installing the dev version and running the reprex provided above I am still having trouble getting the random parameters. Although when specifying component = "zi" both an estimate and the correct CI are provided, when component = "cond" is specified it provides the correct estimate but the CI of the "zi" random parameter. When no component is specified the CI of the "zi" random parameter is presented for the "cond" estimate and for the "zi" estimate there is no CI (NA).

Feel free to ask for any additional information, I will try to be of assistance within my knowledge limitations. Thank you in advance.

bbolker commented 1 year ago

Hmm, working on this. So far don't see any problems/behaving as expected on my machine ... could we have results of sessionInfo() or devtools::session_info() please?

library(magrittr); library(broom.mixed)
gg <- . %>% dplyr::filter(effect %in% "ran_pars") %>%
   dplyr::select(effect, component, group, term, estimate, conf.low, conf.high)
tidy(zinbm2, conf.int = TRUE) %>% gg
##     effect   component group term            estimate conf.low conf.high
##   1 ran_pars cond      site  sd__(Intercept)    0.137   0.0279     0.675
##   2 ran_pars zi        site  sd__(Intercept)    1.22    0.721      2.07 

tidy(zinbm2, component = "cond", conf.int = TRUE) %>% gg
##   effect   component group term            estimate conf.low conf.high
## 1 ran_pars cond      site  sd__(Intercept)    0.137   0.0279     0.675

tidy(zinbm2, component = "zi", conf.int = TRUE) %>% gg
##   effect   component group term            estimate conf.low conf.high
## 1 ran_pars zi        site  sd__(Intercept)     1.22    0.721      2.07

sapply(c("broom.mixed", "glmmTMB"), \(x) format(packageVersion(x)))
## broom.mixed     glmmTMB 
##   "0.2.9.5"     "1.1.4" 

Will try again with development version of glmmTMB in case that matters ...

JPRRamalho commented 1 year ago

Realized that was working with glmmTMB 1.1.3 version. After updating to 1.1.4 everything worked perfectly. I am sorry for all the trouble and thank you again for your time and dedication.

bbolker commented 1 year ago

Glad to hear it.