如何使用扫帚将多个模型的置信区间包含在整齐的输出中?

ric*_*cke 5 r lm broom

我正在尝试使用 ,从许多线性模型中输出一些结果,包括置信区间broom::tidy,但输出似乎只包括第一个模型的置信区间。

线性模型具有相同的预测变量但不同的响应。

考虑以下示例:

library(tidyverse)
library(broom)

# Create toy dataframe.

df <- tibble(
  x = sample(100, replace = TRUE),
  y1 = runif(100),
  y2 = rnorm(100)
)


# Fit linear models, each with x as predictor and y1 and y2 respectively as responses.

my_models <- lm(
  cbind(y1, y2) ~ x,
  data = df
)


# Output results as a tidy tibble.

tidy(my_models, conf.int = TRUE)


# Check confidence intervals with other function.

confint(my_models)
Run Code Online (Sandbox Code Playgroud)

该函数tidy(my_models, conf.int = TRUE)返回以下内容:

> tidy(my_models, conf.int = TRUE)
# A tibble: 4 x 8
  response term          estimate std.error statistic       p.value  conf.low conf.high
  <chr>    <chr>            <dbl>     <dbl>     <dbl>         <dbl>     <dbl>     <dbl>
1 y1       (Intercept)  0.370      0.0572      6.47   0.00000000392  0.256      0.483  
2 y1       x            0.00176    0.000949    1.86   0.0663        -0.000121   0.00365
3 y2       (Intercept) -0.0252     0.215      -0.117  0.907          0.256      0.483  
4 y2       x            0.0000574  0.00357     0.0161 0.987         -0.000121   0.00365
Run Code Online (Sandbox Code Playgroud)

请注意,截距和置信区间的边界x 是两个模型(或响应)。我希望他们有所不同。

与函数的输出进行比较confint(my_models)

> confint(my_models)
                       2.5 %      97.5 %
y1:(Intercept)  0.2562157921 0.483051716
y1:x           -0.0001209424 0.003646348
y2:(Intercept) -0.4520961653 0.401713738
y2:x           -0.0070326154 0.007147456
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,这里的边界有所不同。这也是我所期望的结果tidy(my_models, conf.int = TRUE)。由于模型的边界(包括y1响应)在两个函数中是相同的,因此我假设tidy仅输出第一个模型的置信区间。所以我想知道我在这里做错了什么?

Stu*_*olf 3

这是旧版本扫帚中多响应线性模型的报告问题:

\n
library(broom)\npackageVersion("broom")\n[1] \xe2\x80\x980.5.4\xe2\x80\x99\n\nmod <- lm(cbind(mpg, disp) ~ wt, mtcars)\ntidy(mod, conf.int = TRUE)\n# A tibble: 4 x 8\n  response term        estimate std.error statistic  p.value conf.low conf.high\n  <chr>    <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>\n1 mpg      (Intercept)    37.3      1.88      19.9  8.24e-19    33.5      41.1 \n2 mpg      wt             -5.34     0.559     -9.56 1.29e-10    -6.49     -4.20\n3 disp     (Intercept)  -131.      35.7       -3.67 9.33e- 4    33.5      41.1 \n4 disp     wt            112.      10.6       10.6  1.22e-11    -6.49     -4.20\n
Run Code Online (Sandbox Code Playgroud)\n

升级到最新版本后就可以了:

\n
library(broom)\npackageVersion("broom")\n[1] \xe2\x80\x980.7.0\xe2\x80\x99\n\nmod <- lm(cbind(mpg, disp) ~ wt, mtcars)\ntidy(mod, conf.int = TRUE)\n# A tibble: 4 x 8\n  response term        estimate std.error statistic  p.value conf.low conf.high\n  <chr>    <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>\n1 mpg      (Intercept)    37.3      1.88      19.9  8.24e-19    33.5      41.1\n2 mpg      wt             -5.34     0.559     -9.56 1.29e-10    -6.49     -4.20\n3 disp     (Intercept)  -131.      35.7       -3.67 9.33e- 4  -204.      -58.2\n4 disp     wt            112.      10.6       10.6  1.22e-11    90.8     134.\n
Run Code Online (Sandbox Code Playgroud)\n