簡體   English   中英

如何使用掃帚在整潔的輸出中包含多個模型的置信區間?

[英]How to include confidence intervals from multiple models in tidy output using broom?

我正在嘗試使用broom::tidy從許多線性模型中的 tidy tibble 輸出一些結果,包括置信區間,但輸出似乎只包括第一個模型的置信區間。

線性模型具有相同的預測變量,但具有不同的響應。

考慮以下示例:

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)

函數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

請注意,截距和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

正如預期的那樣,這里的邊界不同。 這也是我期望從tidy(my_models, conf.int = TRUE)得到的結果。 由於包括y1作為響應的模型的邊界在兩個函數中是相同的,我假設tidy只輸出第一個模型的置信區間。 所以我想知道我在這里做錯了什么?

這是舊版本掃帚中多響應線性模型的報告問題

library(broom)
packageVersion("broom")
[1] ‘0.5.4’

mod <- lm(cbind(mpg, disp) ~ wt, mtcars)
tidy(mod, 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 mpg      (Intercept)    37.3      1.88      19.9  8.24e-19    33.5      41.1 
2 mpg      wt             -5.34     0.559     -9.56 1.29e-10    -6.49     -4.20
3 disp     (Intercept)  -131.      35.7       -3.67 9.33e- 4    33.5      41.1 
4 disp     wt            112.      10.6       10.6  1.22e-11    -6.49     -4.20

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

library(broom)
packageVersion("broom")
[1] ‘0.7.0’

mod <- lm(cbind(mpg, disp) ~ wt, mtcars)
tidy(mod, 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 mpg      (Intercept)    37.3      1.88      19.9  8.24e-19    33.5      41.1
2 mpg      wt             -5.34     0.559     -9.56 1.29e-10    -6.49     -4.20
3 disp     (Intercept)  -131.      35.7       -3.67 9.33e- 4  -204.      -58.2
4 disp     wt            112.      10.6       10.6  1.22e-11    90.8     134.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM