构建回归模型的 95% 置信区间

时间:2021-01-03 14:23:29

标签: r model regression confidence-interval

我需要一些帮助。我有下表解释了工作场所的人员流动:

..............B(hat) se

性别 -0.01 0.55

年龄 -0.01 0.03

工作满意度 -0.12 0.08

因此,对于那些换工作的人来说,工作场所的流动率被编码为 1,而工作满意度的衡量标准是从 0 = 非常不满意到 10 = 非常满意。

我应该为工作满意度设定 95% 的置信区间。但是,我不太确定该怎么做。 到目前为止,我已经做到了:

c(-0.12 - qnorm(0.975) * 0.08, -0.012 + qnorm(0.975) * 0.08)

由此我得到以下结果: [1] -0.2767971 0.1447971

我不太确定这是否是正确的方法。

有没有人有任何意见可以帮助我? :)

提前致谢!

2 个答案:

答案 0 :(得分:2)

如果您(天真地)假设正态分布(即具有无限自由度的 t 分布),您的代码就可以正常工作(除了打字错误)。

all.equal(qnorm(1 - .05/2), qt(1 - .05/2, df=Inf))
# [1] TRUE

`colnames<-`(t(apply(d, 1, function(x) 
  x[1] + x[2]*(qt(1 - .05/2, df=Inf)*c(-1, 1)))), paste0(c(2.5, 97.5), "%"))
#                         2.5%      97.5%
# Gender           -1.08798019 1.06798019
# Age              -0.06879892 0.04879892
# Job satisfaction -0.27679712 0.03679712

但是,您有 n=112 个观测值,m=3 个系数和 k=1 个常数,因此 n - m - k 自由度。因此,使用具有 108 个自由度的 t 分布可能是更好的选择。

(DOF <- 112 - 3 - 1)
# [1] 108

qt(1 - .05/2, df=DOF)
# [1] 1.982173

`colnames<-`(t(apply(d, 1, function(x) 
  x[1] + x[2]*(qt(1 - .05/2, df=DOF)*c(-1, 1)))), paste0(c(2.5, 97.5), "%"))
#                        2.5%      97.5%
# Gender           -1.1001954 1.08019542
# Age              -0.0694652 0.04946520
# Job satisfaction -0.2785739 0.03857388

要获得完整的摘要,您可以添加 t-statistics 和 p-values

signif(cbind(d, t=d[,1]/d[,2], p=2*pt(-abs(d[,1]/d[,2]), df=DOF),
             `colnames<-`(
               t(apply(d, 1, function(x) 
                 x[1] + x[2]*(qt(1 - .05/2, df=DOF)*c(-1, 1)))), 
               paste0(c(2.5, 97.5), "%"))), 
       2)
#                  B.hat.   se      t    p   2.5% 97.5%
# Gender            -0.01 0.55 -0.018 0.99 -1.100 1.100
# Age               -0.01 0.03 -0.330 0.74 -0.069 0.049
# Job satisfaction  -0.12 0.08 -1.500 0.14 -0.280 0.039

数据:

d <- structure(list(B.hat. = c(-0.01, -0.01, -0.12), se = c(0.55, 
0.03, 0.08)), class = "data.frame", row.names = c("Gender", "Age", 
"Job satisfaction"))

答案 1 :(得分:0)

为此我会使用 broom::tidy()。像这样:

library(tidyverse)
library(broom)
data(mtcars)

head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mod <- lm(mpg~hp,mtcars)

summary(mod)
#> 
#> Call:
#> lm(formula = mpg ~ hp, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.7121 -2.1122 -0.8854  1.5819  8.2360 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
#> hp          -0.06823    0.01012  -6.742 1.79e-07 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.863 on 30 degrees of freedom
#> Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
#> F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07

broom::tidy(mod,conf.int = T)
#> # A tibble: 2 x 7
#>   term        estimate std.error statistic  p.value conf.low conf.high
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
#> 1 (Intercept)  30.1       1.63       18.4  6.64e-18  26.8      33.4   
#> 2 hp           -0.0682    0.0101     -6.74 1.79e- 7  -0.0889   -0.0476

reprex package (v0.3.0) 于 2021 年 1 月 3 日创建

欢迎使用 StackOverflow!