2

我试图弄清楚如何在R使用该gtsummary函数tbl_regression时为混合效果模型创建良好的输出,但收到错误消息。我正在使用simstudy包模拟一些数据,然后估计一个模型。

library(simstudy)
library(lme4)
library(gtsummary)

### data definition

defC <- defData(varname = "c", formula = 0, variance = "1", dist="normal", id = "site")
  
defS <- defDataAdd(varname = "y", 
    formula = "1 * rx  + c", 
    variance ="10", dist = "normal")

### data generation 

set.seed(3231)
  
dc <- genData(50, defC, id = "site")
dc <- trtAssign(dc, grpName = "rx")

ds <- genCluster(dc, "site", 30, "id")
ds <- addColumns(defS, ds)

### model estimation

fit <- lmer(y ~ rx +  (1 | site), data = ds)
summary(fit)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: y ~ rx + (1 | site)
#>    Data: ds
#> 
#> REML criterion at convergence: 7767.8
#> 
#> Scaled residuals: 
#>     Min      1Q  Median      3Q     Max 
#> -3.3891 -0.6497  0.0002  0.6803  3.1299 
#> 
#> Random effects:
#>  Groups   Name        Variance Std.Dev.
#>  site     (Intercept) 1.038    1.019   
#>  Residual             9.907    3.147   
#> Number of obs: 1500, groups:  site, 50
#> 
#> Fixed effects:
#>             Estimate Std. Error t value
#> (Intercept)  0.05941    0.23393   0.254
#> rx           0.73473    0.33082   2.221
#> 
#> Correlation of Fixed Effects:
#>    (Intr)
#> rx -0.707

我可以tidy直接使用结果broom.mixed...

broom.mixed::tidy(fit, scales = c("vcov", "sdcor"))
#> # A tibble: 4 × 6
#>   effect   group    term             estimate std.error statistic
#>   <chr>    <chr>    <chr>               <dbl>     <dbl>     <dbl>
#> 1 fixed    <NA>     (Intercept)        0.0594     0.234     0.254
#> 2 fixed    <NA>     rx                 0.735      0.331     2.22 
#> 3 ran_pars site     var__(Intercept)   1.04      NA        NA    
#> 4 ran_pars Residual var__Observation   9.91      NA        NA

如果我想使用tbl_regression随机效应的标准偏差输出表格,它可以正常工作。

tbl_regression(fit, tidy_fun = broom.mixed::tidy)
Characteristic  Beta    95% CI1
rx  0.73    0.09, 1.4
site.sd__(Intercept)    1.0 
Residual.sd__Observation    3.1 

但是,当我尝试报告随机效应的方差时,我收到一条错误消息(即使我知道这tidy是有效的。

### gt output

tbl_regression(fit, 
    tidy_fun = function(x) broom.mixed::tidy(x, scales = c("vcov", "sdcor"))
)
#> x There was an error calling `tidy_fun()`. Most likely, this is because the
#> function supplied in `tidy_fun=` was misspelled, does not exist, is not
#> compatible with your object, or was missing necessary arguments (e.g. `conf.level=` or `conf.int=`). See error message below.
#> Error: Error in tidy_fun(model, ...): unused arguments (conf.int = TRUE, conf.level = 0.95)

任何想法我做错了什么?如果您需要更多信息,请告诉我。

4

1 回答 1

2

tbl_regression()在我们到达该部分之前,您提供的代码会返回错误。因此,我使用了 lme4 包中包含的 sleepstudy 数据集作为示例。

您遇到的问题是您如何使用scales=参数定义 tidy 函数。tidy 函数必须接受模型对象、指数参数和 conf.int 参数。如果您将...其作为参数包含在内,那么您将没有问题。

library(gtsummary)
#> #Uighur

fit <- lme4::lmer(Reaction ~ Days + (1 | Subject), lme4::sleepstudy)

tbl_regression(
  fit,
  tidy_fun = 
    function(x, ...) broom.mixed::tidy(x, scales = c("vcov", "sdcor"), ...)
) %>%
  as_kable() # converting to kable to display on SO
特征 贝塔 95% 置信度
10 8.9, 12
Subject.var__(截取) 1,378
Residual.var__Observation 960

reprex 包(v2.0.1)于 2021 年 10 月 12 日创建

于 2021-10-13T01:28:08.540 回答