如何让modelsummary()对无统计量的brms模型跳过统计量展示而非报错?
解决modelsummary混合模型统计量展示报错问题
问题场景
当同时使用modelsummary()展示线性回归模型(lm)和贝叶斯模型(brms)时,若为线性模型添加标准误、t值、p值等统计量,会因贝叶斯模型无对应统计量而报错。
模型代码
models <- list( "Linear" = lm(outcome ~ week * food, data = df ), "Bayesian" = brm(outcome ~ s(week, k = 4, fx = TRUE, by = food) + food, data = df, family = "zero_one_inflated_beta") )
报错代码及信息
执行以下代码尝试添加统计量:
modelsummary(models, estimate = "estimate[{conf.low}, {conf.high}]", statistic = c("Std.Error" = "std.error", "t-value" = "statistic", "p-value" = "p.value"))
触发错误:
Error: std.error is not available. The estimate and statistic arguments must correspond to column names in the output of this command: get_estimates(model)
解决方案
通过为不同模型单独指定统计量的方式,让贝叶斯模型跳过统计量展示:
方法1:直接在statistic参数中按模型设置
modelsummary(models, estimate = "estimate[{conf.low}, {conf.high}]", statistic = list( "Linear" = c("Std.Error" = "std.error", "t-value" = "statistic", "p-value" = "p.value"), "Bayesian" = NULL ))
方法2:使用statistic_override参数自定义
# 定义线性模型需要的统计量 lm_statistics <- list( "Std.Error" = ~ get_estimates(.)$std.error, "t-value" = ~ get_estimates(.)$statistic, "p-value" = ~ get_estimates(.)$p.value ) # 贝叶斯模型不设置统计量 brms_statistics <- NULL modelsummary(models, estimate = "estimate[{conf.low}, {conf.high}]", statistic_override = list("Linear" = lm_statistics, "Bayesian" = brms_statistics))
两种方法都能实现:线性模型展示指定的统计量,贝叶斯模型仅展示估计值与置信区间,避免报错。
内容的提问来源于stack exchange,提问作者nerd
相关产品推荐
相关产品推荐

