如何在 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
广告