回归线的斜率是回归分析的一个非常重要的部分,通过找到该斜率,我们可以估算出因变量期望增加或减少的值。但是置信区间提供了当样本量相同时我们期望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