![](/img/trans.png)
[英]How to calculate the 95% confidence interval for the slope in a linear regression model in R
[英]Construct 95% confidence interval for regression model
我需要一些幫助。 我有下表解釋了工作場所的營業額:
....B(帽子)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
我不太確定這是否是正確的方法。
有沒有人有任何可以幫助我的意見? :)
提前致謝!
如果您(天真地)假設正態分布(即具有無限自由度的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"))
我會為此使用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
由代表 package (v0.3.0) 於 2021 年 1 月 3 日創建
歡迎來到 StackOverflow!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.