如何在R中找到迴歸線斜率的95%置信區間?
迴歸線斜率是迴歸分析中非常重要的部分,透過求解斜率,我們可以估算因變數的預期增減值。但是,置信區間提供了我們期望在樣本量相同的情況下95%的時間內出現的斜率值的範圍。為了找到迴歸線斜率的95%置信區間,我們可以使用帶有迴歸模型物件的`confint`函式。
示例
考慮以下資料框:
set.seed(1) x <-rnorm(20) y <-rnorm(20,2.5) df <-data.frame(x,y) df
輸出
x y 1 -0.62645381 3.4189774 2 0.18364332 3.2821363 3 -0.83562861 2.5745650 4 1.59528080 0.5106483 5 0.32950777 3.1198257 6 -0.82046838 2.4438713 7 0.48742905 2.3442045 8 0.73832471 1.0292476 9 0.57578135 2.0218499 10 -0.30538839 2.9179416 11 1.51178117 3.8586796 12 0.38984324 2.3972123 13 -0.62124058 2.8876716 14 -2.21469989 2.4461950 15 1.12493092 1.1229404 16 -0.04493361 2.0850054 17 -0.01619026 2.1057100 18 0.94383621 2.4406866 19 0.82122120 3.6000254 20 0.59390132 3.2631757
建立迴歸模型以根據x預測y:
示例
RegressionModel <-lm(y~x,data=df) summary(RegressionModel)
輸出
Call: lm(formula = y ~ x, data = df) Residuals: Min 1Q Median 3Q Max -1.69133 -0.43739 -0.07132 0.68033 1.63937 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 2.5331 0.1998 12.677 2.08e-10 *** x -0.2075 0.2195 -0.946 0.357 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.8738 on 18 degrees of freedom Multiple R-squared: 0.04732, Adjusted R-squared: -0.00561 F-statistic: 0.894 on 1 and 18 DF, p-value: 0.3569
找到迴歸線斜率的95%置信區間:
示例
confint(RegressionModel,'x',level=0.95) 2.5 % 97.5 % x -0.6687129 0.2536177 Lets’ have a look at another example: BloodPressure <-c(165,170,190,195,220) Weight <-c(50,75,64,60,62) data <-data.frame(BloodPressure,Weight) data
輸出
BloodPressure Weight 1 165 50 2 170 75 3 190 64 4 195 60 5 220 62
示例
RegM <-lm(BloodPressure~Weight,data=data) summary(RegM)
輸出
Call: lm(formula = BloodPressure ~ Weight, data = data) Residuals: 1 2 3 4 5 -21.783 -19.277 1.820 7.219 32.020 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 181.79551 88.73672 2.049 0.133 Weight 0.09975 1.41495 0.070 0.948 Residual standard error: 25.34 on 3 degrees of freedom Multiple R-squared: 0.001654, Adjusted R-squared: -0.3311 F-statistic: 0.00497 on 1 and 3 DF, p-value: 0.9482
示例
confint(RegM,'Weight',level=0.95) 2.5 % 97.5 % Weight -4.403255 4.602756
輸出
2.5 % 97.5 % Weight -4.403255 4.602756
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP