如何在 R 中從迴歸模型中提取回歸係數、係數標準誤、t 分數和 p 值?
R 中的迴歸分析輸出提供了很多值,但如果我們相信我們的模型足夠好,我們可能只想提取係數、標準誤和 t 分數或 p 值,因為這些值最終很重要,特別是係數,因為它們幫助我們解釋模型。我們可以用差分運算子 $ 從迴歸模型摘要中提取這些值。
示例
考慮以下資料 −
> set.seed(99) > x1<-rpois(50,2) > x2<-rpois(50,10) > x3<-rpois(50,25) > x4<-rnorm(50,1) > x5<-rnorm(50,2.5) > x6<-rnorm(50,1.5) > x7<-runif(50,2,20) > y<-sample(1:1000,50,replace=TRUE)
建立迴歸模型 −
> Regression_Model<-lm(y~x1+x2+x3+x4+x5+x6+x7)
獲取模型輸出 &minus
> summary(Regression_Model) Call: lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7) Residuals: Min 1Q Median 3Q Max -580.06 -268.03 71.54 248.45 450.20 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 885.966696 336.412681 2.634 0.0118 * x1 -33.463082 34.748162 -0.963 0.3411 x2 -8.056429 13.866217 -0.581 0.5643 x3 -0.003585 9.641347 0.000 0.9997 x4 -62.751405 47.195104 -1.330 0.1908 x5 -53.421667 40.706602 -1.312 0.1965 x6 -46.645285 41.017385 -1.137 0.2619 x7 7.705532 8.543121 0.902 0.3722 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 309.4 on 42 degrees of freedom Multiple R-squared: 0.1242, Adjusted R-squared: -0.02181 F-statistic: 0.8506 on 7 and 42 DF, p-value: 0.5526
從模型中提取所有迴歸係數、係數標準誤、t 分數和 p 值 −
> summary(Regression_Model)$coefficients Estimate Std. Error t value Pr(>|t|) (Intercept) 885.966696369 336.412681 2.6335710454 0.01177664 x1 -33.463081817 34.748162 -0.9630173179 0.34105093 x2 -8.056428960 13.866217 -0.5810113022 0.56433788 x3 -0.003584907 9.641347 -0.0003718264 0.99970509 x4 -62.751404764 47.195104 -1.3296168453 0.19082124 x5 -53.421667389 40.706602 -1.3123588063 0.19652614 x6 -46.645285482 41.017385 -1.1372076842 0.26189795 x7 7.705532157 8.543121 0.9019575482 0.37222303
從模型中提取單個迴歸係數、係數標準誤、t 分數和 p 值 −
> summary(Regression_Model)$coefficients[1,2] [1] 336.4127 > summary(Regression_Model)$coefficients[1,1] [1] 885.9667 > summary(Regression_Model)$coefficients[1,4] [1] 0.01177664 > summary(Regression_Model)$coefficients[3,1] [1] -8.056429 > summary(Regression_Model)$coefficients[7,1] [1] -46.64529 > summary(Regression_Model)$coefficients[7,4] [1] 0.261898 > summary(Regression_Model)$coefficients[8,4] [1] 0.372223 > summary(Regression_Model)$coefficients[1,3] [1] 2.633571 > summary(Regression_Model)$coefficients[2,1] [1] -33.46308 > summary(Regression_Model)$coefficients[2,2] [1] 34.74816 > summary(Regression_Model)$coefficients[2,4] [1] 0.3410509 > summary(Regression_Model)$coefficients[4,4] [1] 0.9997051 > summary(Regression_Model)$coefficients[4,3] [1] -0.0003718264 > summary(Regression_Model)$coefficients[5,4] [1] 0.1908212 > summary(Regression_Model)$coefficients[5,1] [1] -62.7514 > summary(Regression_Model)$coefficients[5,2] [1] 47.1951 > summary(Regression_Model)$coefficients[6,1] [1] -53.42167 > summary(Regression_Model)$coefficients[6,4] [1] 0.1965261
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP