Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何从多元回归模型中提取置信区间?_R_Regression_Confidence Interval_Broom - Fatal编程技术网

R 如何从多元回归模型中提取置信区间?

R 如何从多元回归模型中提取置信区间?,r,regression,confidence-interval,broom,R,Regression,Confidence Interval,Broom,我提取两个不同组的回归结果,如下例所示。在tempdata.frame中,我得到了估计值、标准误差、统计值和p值。但是,我没有得到置信区间。有没有一个简单的方法来提取它们呢 df <- tibble( a = rnorm(1000), b = rnorm(1000), c = rnorm(1000), d = rnorm(1000), group = rbinom(n=1000, size=1, prob=0.5) ) df$group = as.factor(df

我提取两个不同组的回归结果,如下例所示。在
temp
data.frame中,我得到了估计值、标准误差、统计值和p值。但是,我没有得到置信区间。有没有一个简单的方法来提取它们呢

 df <- tibble(
  a = rnorm(1000),
  b = rnorm(1000),
  c = rnorm(1000),
  d = rnorm(1000),
  group = rbinom(n=1000, size=1, prob=0.5)
)

df$group = as.factor(df$group)

temp <- df %>%
  group_by(group) %>%
  do(model1 = tidy(lm(a ~ b + c + d, data = .))) %>%   
  gather(model_name, model, -group) %>%                        
  unnest() 
df%
do(模型1=tidy(lm(a~b+c+d,数据=)))%>
聚集(模型名称,模型,-组)%>%
unnest()

您正在对lm对象进行整理。如果选中,则有一个选项包括置信区间,
conf.int=TRUE

temp <- df %>%
  group_by(group) %>%
  do(model1 = tidy(lm(a ~ b + c + d, data = . ), conf.int=TRUE)) %>%   
  gather(model_name, model, -group) %>%                        
  unnest()

# A tibble: 8 x 9
  group model_name term  estimate std.error statistic p.value conf.low conf.high
  <fct> <chr>      <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
1 0     model1     (Int…  0.0616     0.0423    1.46    0.146   -0.0215    0.145 
2 0     model1     b      0.00178    0.0421    0.0424  0.966   -0.0808    0.0844
3 0     model1     c     -0.00339    0.0431   -0.0787  0.937   -0.0881    0.0813
4 0     model1     d     -0.0537     0.0445   -1.21    0.228   -0.141     0.0337
5 1     model1     (Int… -0.0185     0.0454   -0.408   0.683   -0.108     0.0707
6 1     model1     b      0.00128    0.0435    0.0295  0.976   -0.0842    0.0868
7 1     model1     c     -0.0972     0.0430   -2.26    0.0244  -0.182    -0.0126
8 1     model1     d      0.0734     0.0457    1.60    0.109   -0.0165    0.163 
temp%
分组依据(分组)%>%
do(model1=tidy(lm(a~b+c+d,数据=),conf.int=TRUE))%>%
聚集(模型名称,模型,-组)%>%
unnest()
#一个tibble:8x9
组模型\u名称术语估计标准错误统计p值配置低配置高
1 0型号1(Int…0.0616 0.0423 1.46 0.146-0.0215 0.145
2 0型号1 b 0.00178 0.0421 0.0424 0.966-0.0808 0.0844
3 0型号1 c-0.00339 0.0431-0.0787 0.937-0.0881 0.0813
4 0型号1 d-0.0537 0.0445-1.21 0.228-0.141 0.0337
5.1型号1(内部-0.0185 0.0454-0.408 0.683-0.108 0.0707
6 1型号1 b 0.00128 0.0435 0.0295 0.976-0.0842 0.0868
7 1型号1 c-0.0972 0.0430-2.26 0.0244-0.182-0.0126
8 1型号1 d 0.0734 0.0457 1.60 0.109-0.0165 0.163

如果您的
dplyr
版本高于1.0.0,您可以使用:

df %>%
 group_by(group) %>%
 summarise(tidy(lm(a ~ b + c + d), conf.int = TRUE), .groups = "drop") 
#> # A tibble: 8 x 8
#>   group term        estimate std.error statistic p.value conf.low conf.high
#>   <fct> <chr>          <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
#> 1 0     (Intercept)   0.0734    0.0468     1.57   0.117   -0.0185    0.165 
#> 2 0     b            -0.101     0.0461    -2.19   0.0292  -0.191    -0.0102
#> 3 0     c             0.0337    0.0464     0.726  0.468   -0.0575    0.125 
#> 4 0     d            -0.101     0.0454    -2.23   0.0265  -0.190    -0.0118
#> 5 1     (Intercept)  -0.0559    0.0468    -1.20   0.232   -0.148     0.0360
#> 6 1     b            -0.0701    0.0474    -1.48   0.140   -0.163     0.0230
#> 7 1     c             0.0319    0.0477     0.668  0.504   -0.0619    0.126 
#> 8 1     d            -0.0728    0.0466    -1.56   0.119   -0.164     0.0188
df%>%
分组依据(分组)%>%
总结(tidy(lm(a~b+c+d),conf.int=TRUE),.groups=“drop”)
#>#tibble:8 x 8
#>组项估计标准误差统计p值形态低形态高
#>                                    
#>10(截距)0.0734 0.0468 1.57 0.117-0.0185 0.165
#>20B-0.101 0.0461-2.19 0.0292-0.191-0.0102
#>3 0 c 0.0337 0.0464 0.726 0.468-0.0575 0.125
#>4.0D-0.101 0.0454-2.23 0.0265-0.190-0.0118
#>5 1(截距)-0.0559 0.0468-1.20 0.232-0.148 0.0360
#>61B-0.0701 0.0474-1.48 0.140-0.163 0.0230
#>7 1 c 0.0319 0.0477 0.668 0.504-0.0619 0.126
#>8.1 d-0.0728 0.0466-1.56 0.119-0.164 0.0188