英文:
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("vincentarelbundock/modelsummary")
Restart R
completely for the changes to take effect.
Then, you can do things like:
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"),
# clean-up coefficient names
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 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论