modelsummary 同时显示来自 lm 和 brms 的输出以及统计数据。

huangapple go评论75阅读模式
英文:

modelsummary display output from lm and brms concurrently with statistics

问题

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 = NULL)

问题在于,当我尝试使用以下代码获取线性模型的p值、t值和标准误差时,出现错误,错误信息为“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)”

modelsummary(models,
             estimate = "estimate[{conf.low}, {conf.high}]",
             statistic = c("Std.Error" = "std.error", 
                           "t-value" = "statistic", 
                           "p-value" = "p.value"))

如何使modelsummary()在没有统计信息的情况下(例如在brms模型中)忽略统计信息的显示,而不是抛出错误呢?

英文:
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")
)

The following code works when I run it

modelsummary(models,
             estimate = "{estimate}[{conf.low}, {conf.high}]",
             statistic = NULL)

The problem is that when I attempt to also get the p-value, t-value and standard error of the linear model with the following code, the error comes up as 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)

modelsummary(models,
             estimate = "estimate[{conf.low}, {conf.high}]",
             statistic = c("Std.Error" = "std.error", 
                           "t-value" = "statistic", 
                           "p-value" = "p.value"))

How can I enable modelsummary() to ignore the display of statistics when there is none like in the case of the brms model instead of throwing an error?

答案1

得分: 2

这是一个常见的用例,在CRAN版本的modelsummary中不受很好支持。而不是建议一个复杂的解决方法,我推送了一个更简单的解决方法到开发版本中。您可以使用以下方法立即安装它:

remotes::install_github("vincentarelbundock/modelsummary")

为使更改生效,完全重启R

然后,您可以执行类似以下的操作:

library(brms)
library(modelsummary)

mod1 <- lm(mpg ~ hp + qsec, data = mtcars)
mod2 <- brm(mpg ~ hp + qsec, data = mtcars)
models <- list(mod1, mod2)

modelsummary(
    models,
    statistic = c("std.error", "conf.int"),
    # 清除系数名称
    coef_rename = \(x) gsub("b_", "", x),
    coef_omit = "Intercept")
(1) (2)
hp -0.085 -0.084
(0.014)
[-0.113, -0.056] [-0.112, -0.055]
qsec -0.887 -0.867
(0.535)
[-1.980, 0.207] [-1.944, 0.239]
sigma 3.815
[3.000, 4.991]
Num.Obs. 32 32
R2 0.637 0.631
R2 Adj. 0.612 0.553
AIC 180.3
BIC 186.2
Log.Lik. -86.170
F 25.431
ELPD -91.1
ELPD s.e. 4.9
LOOIC 182.3
LOOIC s.e. 9.8
WAIC 181.9
RMSE 3.57 3.57
英文:

This is a common use-case which is not well supported in the CRAN version of modelsummary. Instead of suggesting a complicated hack, I pushed a change to the development version which makes this much easier. You can install it now with:

remotes::install_github(&quot;vincentarelbundock/modelsummary&quot;)

Restart R completely for the changes to take effect.

Then, you can do things like:

library(brms)
library(modelsummary)

mod1 &lt;- lm(mpg ~ hp + qsec, data = mtcars)
mod2 &lt;- brm(mpg ~ hp + qsec, data = mtcars)
models &lt;- list(mod1, mod2)

modelsummary(
    models,
    statistic = c(&quot;std.error&quot;, &quot;conf.int&quot;),
    # clean-up coefficient names
    coef_rename = \(x) gsub(&quot;b_&quot;, &quot;&quot;, x),
    coef_omit = &quot;Intercept&quot;)
(1) (2)
hp -0.085 -0.084
(0.014)
[-0.113, -0.056] [-0.112, -0.055]
qsec -0.887 -0.867
(0.535)
[-1.980, 0.207] [-1.944, 0.239]
sigma 3.815
[3.000, 4.991]
Num.Obs. 32 32
R2 0.637 0.631
R2 Adj. 0.612 0.553
AIC 180.3
BIC 186.2
Log.Lik. -86.170
F 25.431
ELPD -91.1
ELPD s.e. 4.9
LOOIC 182.3
LOOIC s.e. 9.8
WAIC 181.9
RMSE 3.57 3.57

huangapple
  • 本文由 发表于 2023年2月27日 13:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定