如何使用gtsummary::tbl_regression()报告随机效应的方差估计值
错误原因
tbl_regression()在调用你传入的tidy_fun时,会默认传递conf.int = TRUE、conf.level = 0.95两个参数用于计算置信区间。你自定义的匿名函数仅定义了x一个入参,没有预留接收额外参数的入口,因此多余的参数传入时就会触发「未使用参数」的报错。
而你直接传入broom.mixed::tidy不报错是因为该函数原生支持conf.int、conf.level参数,也预留了...参数接收其他传入值,可以正常匹配tbl_regression()的传参逻辑。
解决方法
在自定义的tidy函数中加入...参数,用来接收所有多余的传入参数,同时将参数透传给broom.mixed::tidy即可,修改后的代码如下:
tbl_regression(fit, tidy_fun = function(x, ...) { broom.mixed::tidy(x, scales = c("vcov", "sdcor"), ...) } )
如果需要显式控制参数,也可以把用到的参数明确写在函数定义中:
tbl_regression(fit, tidy_fun = function(x, conf.int = TRUE, conf.level = 0.95) { broom.mixed::tidy( x, scales = c("vcov", "sdcor"), conf.int = conf.int, conf.level = conf.level ) } )
运行上述代码就能正常输出包含随机效应方差的回归表了。
内容的提问来源于stack exchange,提问作者kgoldfeld
相关产品推荐
相关产品推荐

