You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中使用modelsummary报告htest类对象时的报错求助

解决modelsummary无法识别自定义tidy.htest的问题

问题根源

modelsummary提取模型信息时,会优先调用broom包内置的tidy.htest方法,而非你在全局环境定义的同名函数。原生broom::tidy.htest返回结果不包含term列,这就是触发报错的核心原因。

解决方案

方法1:调用时直接指定自定义函数

执行modelsummary时,通过tidy和glance参数明确传入你定义的函数:

modelsummary(test, tidy = tidy.htest, glance = glance.htest)

方法2:注册自定义S3方法(持久生效)

将你的自定义函数注册到broom包的命名空间,让modelsummary自动识别并使用:

# 注册tidy方法
registerS3method("tidy", "htest", tidy.htest, envir = asNamespace("broom"))
# 注册glance方法
registerS3method("glance", "htest", glance.htest, envir = asNamespace("broom"))

# 之后直接调用即可
modelsummary(test)

验证步骤

手动调用broom::tidy(test),如果返回结果包含term列,说明自定义方法已成功注册,此时modelsummary就能正常生成表格。

内容的提问来源于stack exchange,提问作者Victor Hugo Schieck Terziani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:12:26