gtsummary and add_glance: Error: No glance method for objects of class mira

gtsummary and add_glance: Error: No glance method for objects of class mira

我正在尝试使用 gtsummary 将 r 平方和观察值的数量添加到我的回归表中,并乘以来自 mice 的估算数据。如果我合并模型,我仍然可以使用 glance 提取 r 平方,但是 mipo 对象与 tbl_regression 不兼容,因为合并已经是整理过程的一部分。感谢任何对此问题的帮助。

library(tidyverse)
library(gtsummary)
library(mice)
library(broom)
library(broom.helpers)
set.seed(123)

mice::version(pkg = "tidyverse")
#> [1] "tidyverse 1.3.1.9000  /Library/Frameworks/R.framework/Versions/4.1/Resources/library"
mice::version(pkg = "gtsummary")
#> [1] "gtsummary 1.5.2  /Library/Frameworks/R.framework/Versions/4.1/Resources/library"
mice::version(pkg = "mice")
#> [1] "mice 3.14.3 2021-12-06 /Library/Frameworks/R.framework/Versions/4.1/Resources/library"
mice::version(pkg = "broom")
#> [1] "broom 0.7.12.9000  /Library/Frameworks/R.framework/Versions/4.1/Resources/library"
mice::version(pkg = "broom.helpers")
#> [1] "broom.helpers 1.6.0.9000  /Library/Frameworks/R.framework/Versions/4.1/Resources/library"
R.Version()$version.string
#> [1] "R version 4.1.1 (2021-08-10)"

# imputed data
data(nhanes)
imp <- mice(nhanes, m = 3, print = FALSE)
mod <- with(imp, lm(age ~ bmi + chl))

# is it broom ?
broom::glance(mod)
#> Error: No glance method for objects of class mira
broom::glance(pool(mod))
#>   nimp nobs r.squared adj.r.squared
#> 1    3   25 0.5245108     0.4799119

# regular tbl_regression
tbl_regression(mod) %>% as_kable() # kable so it prints nicely here
#> pool_and_tidy_mice(): Tidying mice model with
#> `mice::pool(x) %>% mice::tidy(exponentiate = FALSE, conf.int = TRUE, conf.level = 0.95)`
Characteristic Beta 95% CI p-value
bmi -0.12 -0.21, -0.03 0.012
chl 0.01 0.00, 0.02 0.008
# tbl_regression with add_glance_source_note
tbl_regression(mod) %>% add_glance_source_note(include = c(r.squared, nobs) )
#> pool_and_tidy_mice(): Tidying mice model with
#> `mice::pool(x) %>% mice::tidy(exponentiate = FALSE, conf.int = TRUE, conf.level = 0.95)`
#> Error: No glance method for objects of class mira

# tbl_regression with add_glance_table
tbl_regression(mod) %>% add_glance_table(include = c(r.squared, nobs) )
#> pool_and_tidy_mice(): Tidying mice model with
#> `mice::pool(x) %>% mice::tidy(exponentiate = FALSE, conf.int = TRUE, conf.level = 0.95)`
#> Error: No glance method for objects of class mira

# tbl_regression with pooled data
pool <- mice::pool(mod)
tbl_regression(pool)
#> x Please pass the 'mice' model to `tbl_regression()` before models
#> have been combined with `mice::pool()`. The default tidier, `pool_and_tidy_mice()`, will both pool and tidy the regression model.
#> mice::mice(trial, m = 2) %>%
#>   with(lm(age ~ marker + grade)) %>%
#>   tbl_regression()

感谢您提供详细的问题和可重现的示例。

您很好地诊断了问题。 tbl_regression() 需要未合并的结果,broom::glance() 需要合并的结果。您可以通过传递自定义 glance() 函数来获得所需内容。

示例如下!

library(tidyverse)
library(gtsummary)
library(mice)
#> 
#> Attaching package: 'mice'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind
set.seed(123)

# imputed data
data(nhanes)
imp <- mice(nhanes, m = 3, print = FALSE)
mod <- with(imp, lm(age ~ bmi + chl))

broom::glance(pool(mod))
#>   nimp nobs r.squared adj.r.squared
#> 1    3   25 0.5245108     0.4799119

tbl <-
  tbl_regression(mod) %>% 
  add_glance_source_note(
    include = c(r.squared, nobs),
    # need to modify the glance function slightly to handle the regression object
    glance_fun = function(x) broom::glance(pool(x))
  )
#> pool_and_tidy_mice(): Tidying mice model with
#> `mice::pool(x) %>% mice::tidy(exponentiate = FALSE, conf.int = TRUE, conf.level = 0.95)`

reprex package (v2.0.1)

创建于 2022-01-29