回归分析可谓统计学的核心。回归分析是指一个或多个自变量(Xi)来预测因变量(Yi)的方法。
其基础思想是最少二乘法(OLS:ordinary least square):...............(1)
要得到拟合得最好的(1),要使其残差平方和(RSS:residual sum of squares/sum squared residual)要达到min: ....................(2);
且数据也有以下四点统计假设:
【违背了以上假设,你的统计显著性检验结果和所得的置信区间就很可能不精确了。】
而以上四点,可以由R包的回归诊断plot(fit)来检验:
【回归诊断技术向你提供了评价回归模型适用性的必要工具,能帮助发现并纠正问题。】
下面使用R常用的数据集mtcar作为例子来深入了解一下:
> fit1<-lm(mpg~wt,data = mtcars)
> par(mfrow=c(2,2))
> plot(fit1)
这四副图从上往下,从左往右依次是:
残差与拟合图,看到一个曲线关系,这暗示着你可能需要对回归模型加上一个二次项。
正态Q-Q图:若满足正态假设,针对QLS的统计假设中的正态性,图上的点应该落在呈45度角的直线上;若不是如此,那么就违反了正态性的假设
尺寸位置图,满足同方差性(若满足不变方差假设)要求在水平线周围的点是随机分布的
提供了你可能关注的单个观测点的信息。从图形可以鉴别出离群点、高杠杆值点和强影响点
-而 独立性 你无法从这些图中分辨出因变量的值是否相互独立,只能从收集的数据中来验证。
通过模型的构建,主要目的是:解释和预测,最终会希望得到:
得到一个合理的模型具体有以下四个步骤:
首先分为两种模型来进行:
> fit<-lm(mpg~wt,data=mtcars)
> summary(fit)
Call:
lm(formula = mpg ~ wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.5432 -2.3647 -0.1252 1.4096 6.8727
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
wt -5.3445 0.5591 -9.559 1.29e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
#其中
##T 检验中查看解释变量的显著性;
##R-squared 查看方程的拟合程度;
##R 检验查看方程的整体显著性。
> plot(mtcars$wt,mtcars$mpg)
> abline(fit)
看的出来拟合效果不太好,注意到散点图的边界有曲线弯曲,尝试添加多一个来拟合,如下:
> fit2<-lm(mpg~wt+I(wt^2),data=mtcars)
> summary(fit2)
Call:
lm(formula = mpg ~ wt + I(wt^2), data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-3.483 -1.998 -0.773 1.462 6.238
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 49.9308 4.2113 11.856 1.21e-12 ***
wt -13.3803 2.5140 -5.322 1.04e-05 ***
I(wt^2) 1.1711 0.3594 3.258 0.00286 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.651 on 29 degrees of freedom
Multiple R-squared: 0.8191, Adjusted R-squared: 0.8066
F-statistic: 65.64 on 2 and 29 DF, p-value: 1.715e-11
比较fit1和fit2的R2,可以看出来fit2的拟合程度更高,大于0.8.,而再看一下拟合的曲线效果:
> plot(mtcars$wt,mtcars$mpg)
> lines(mtcars$wt,fitted(fit2))
看得出拟合的曲线与点更为贴近了。
多元回归分析(Multiple Regression Analysis)是多变量分析的基础,也是理解监督类分析方法的入口,追求得出一个简约(Parsimony)的模型和更高的解释度的模型。
其主要得出:
(1)确定几个特定的变量之间是否存在相关关系,如果存在的话,找出它们之间合适的数学表达式;
(2)根据一个或几个变量的值,预测或控制另一个变量的取值,并且可以知道这种预测或控制能达到什么样的精确度;
(3)进行因素分析。例如在对于共同影响一个变量的许多变量(因素)之间,找出哪些是重要因素,哪些是次要因素,这些因素之间又有什么关系等等。
其主要步骤是 :
1.探索特征之间的关系:
> cor(mtcars)
mpg cyl disp hp drat wt
mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.68117191 -0.8676594
cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.69993811 0.7824958
disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.71021393 0.8879799
hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.44875912 0.6587479
drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.00000000 -0.7124406
wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.71244065 1.0000000
qsec 0.4186840 -0.5912421 -0.4336979 -0.7082234 0.09120476 -0.1747159
vs 0.6640389 -0.8108118 -0.7104159 -0.7230967 0.44027846 -0.5549157
am 0.5998324 -0.5226070 -0.5912270 -0.2432043 0.71271113 -0.6924953
gear 0.4802848 -0.4926866 -0.5555692 -0.1257043 0.69961013 -0.5832870
carb -0.5509251 0.5269883 0.3949769 0.7498125 -0.09078980 0.4276059
qsec vs am gear carb
mpg 0.41868403 0.6640389 0.59983243 0.4802848 -0.55092507
cyl -0.59124207 -0.8108118 -0.52260705 -0.4926866 0.52698829
disp -0.43369788 -0.7104159 -0.59122704 -0.5555692 0.39497686
hp -0.70822339 -0.7230967 -0.24320426 -0.1257043 0.74981247
drat 0.09120476 0.4402785 0.71271113 0.6996101 -0.09078980
wt -0.17471588 -0.5549157 -0.69249526 -0.5832870 0.42760594
qsec 1.00000000 0.7445354 -0.22986086 -0.2126822 -0.65624923
vs 0.74453544 1.0000000 0.16834512 0.2060233 -0.56960714
am -0.22986086 0.1683451 1.00000000 0.7940588 0.05753435
gear -0.21268223 0.2060233 0.79405876 1.0000000 0.27407284
carb -0.65624923 -0.5696071 0.05753435 0.2740728 1.00000000
2.可视化特征之间的关系——散点图矩阵
> library(car)
> mtcars1<-mtcars[,1:7]
#1.pairs 图:
> pairs(mtcars1)
#2.类似pairs图,但其在非对角线区域增加绘制变量间的散点图,并添加平滑(loess) 和线性拟合曲线。对角线区域绘制每个变量的密度图和轴须图。
> scatterplotMatrix(mtcars1)
3.基于数据训练模型
fit1<-lm(mpg~.,data = mtcars)
fit2<-lm(mpg~.,data = mtcars1)
fit3<-lm(mpg~cyl+(disp+hp+drat+wt+qsec)^2 , data=mtcars)
4.评估模型的性能:
> summary(fit1)
Call:
lm(formula = mpg ~ ., data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-3.4506 -1.6044 -0.1196 1.2193 4.6271
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 12.30337 18.71788 0.657 0.5181
cyl -0.11144 1.04502 -0.107 0.9161
disp 0.01334 0.01786 0.747 0.4635
hp -0.02148 0.02177 -0.987 0.3350
drat 0.78711 1.63537 0.481 0.6353
wt -3.71530 1.89441 -1.961 0.0633 .
qsec 0.82104 0.73084 1.123 0.2739
vs 0.31776 2.10451 0.151 0.8814
am 2.52023 2.05665 1.225 0.2340
gear 0.65541 1.49326 0.439 0.6652
carb -0.19942 0.82875 -0.241 0.8122
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.65 on 21 degrees of freedom
Multiple R-squared: 0.869, Adjusted R-squared: 0.8066
F-statistic: 13.93 on 10 and 21 DF, p-value: 3.793e-07
> summary(fit2)
Call:
lm(formula = mpg ~ ., data = mtcars1)
Residuals:
Min 1Q Median 3Q Max
-3.9682 -1.5795 -0.4353 1.1662 5.5272
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 26.30736 14.62994 1.798 0.08424 .
cyl -0.81856 0.81156 -1.009 0.32282
disp 0.01320 0.01204 1.097 0.28307
hp -0.01793 0.01551 -1.156 0.25846
drat 1.32041 1.47948 0.892 0.38065
wt -4.19083 1.25791 -3.332 0.00269 **
qsec 0.40146 0.51658 0.777 0.44436
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.557 on 25 degrees of freedom
Multiple R-squared: 0.8548, Adjusted R-squared: 0.82
F-statistic: 24.53 on 6 and 25 DF, p-value: 2.45e-09
> summary(fit3)
Call:
lm(formula = mpg ~ cyl + (disp + hp + drat + wt + qsec)^2, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-3.1478 -1.4607 -0.1145 1.3270 3.7900
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.280e+02 2.944e+02 0.435 0.670
cyl 7.018e-01 1.526e+00 0.460 0.652
disp -1.815e-01 4.921e-01 -0.369 0.717
hp 1.473e-01 7.788e-01 0.189 0.852
drat -2.086e+01 5.946e+01 -0.351 0.731
wt -2.055e+01 4.379e+01 -0.469 0.646
qsec -2.969e+00 1.561e+01 -0.190 0.852
disp:hp 2.224e-05 6.761e-04 0.033 0.974
disp:drat 1.755e-02 5.039e-02 0.348 0.732
disp:wt 8.796e-03 3.043e-02 0.289 0.776
disp:qsec 5.493e-03 2.289e-02 0.240 0.814
hp:drat -1.789e-02 1.243e-01 -0.144 0.887
hp:wt 2.293e-02 5.628e-02 0.407 0.689
hp:qsec -1.240e-02 1.295e-02 -0.957 0.354
drat:wt 1.656e+00 5.974e+00 0.277 0.785
drat:qsec 8.408e-01 3.244e+00 0.259 0.799
wt:qsec 2.466e-01 1.406e+00 0.175 0.863
Residual standard error: 2.626 on 15 degrees of freedom
Multiple R-squared: 0.9081, Adjusted R-squared: 0.8102
F-statistic: 9.269 on 16 and 15 DF, p-value: 4.39e-05
`当预测变量不止一个时,回归系数的含义为,一个预测变量增加一个单位,其他预测变量保 持不变时,因变量将要增加的数量。
`添加相互项:两个预测 变量的交互项显著,说明响应变量与其中一个预测变量的关系依赖于另外一个预测变量的水平。
·fit4利用逐步分析回归 ,因观察fit1~3,回归方程的系数都没有通过显著性检验:
逐步回归分析是以AIC信息统计量为准则,通过选择最小的AIC信息统计量,来达到删除或增加变量的目的。
R语言中用于逐步回归分析的函数 step() drop1() add1()
> fit4<-step(fit3,scale=0)
Start: AIC=71.54
mpg ~ cyl + (disp + hp + drat + wt + qsec)^2
Df Sum of Sq RSS AIC
- disp:hp 1 0.0075 103.44 69.544
- hp:drat 1 0.1429 103.57 69.586
- wt:qsec 1 0.2121 103.64 69.607
- disp:qsec 1 0.3969 103.83 69.664
- drat:qsec 1 0.4633 103.89 69.685
- drat:wt 1 0.5296 103.96 69.705
- disp:wt 1 0.5762 104.01 69.719
- disp:drat 1 0.8367 104.27 69.799
- hp:wt 1 1.1443 104.58 69.894
- cyl 1 1.4595 104.89 69.990
- hp:qsec 1 6.3171 109.75 71.439
103.43 71.542
Step: AIC=69.54
mpg ~ cyl + disp + hp + drat + wt + qsec + disp:drat + disp:wt +
disp:qsec + hp:drat + hp:wt + hp:qsec + drat:wt + drat:qsec +
wt:qsec
Df Sum of Sq RSS AIC
- wt:qsec 1 0.2533 103.69 67.622
- hp:drat 1 0.4273 103.87 67.676
- disp:drat 1 0.8390 104.28 67.802
- drat:qsec 1 0.9276 104.37 67.830
- drat:wt 1 0.9320 104.37 67.831
- disp:qsec 1 1.1050 104.54 67.884
- hp:wt 1 1.1933 104.63 67.911
- disp:wt 1 1.4077 104.85 67.976
- cyl 1 1.5100 104.95 68.008
103.44 69.544
- hp:qsec 1 10.7185 114.16 70.699
Step: AIC=67.62
mpg ~ cyl + disp + hp + drat + wt + qsec + disp:drat + disp:wt +
disp:qsec + hp:drat + hp:wt + hp:qsec + drat:wt + drat:qsec
Df Sum of Sq RSS AIC
- hp:drat 1 0.6343 104.33 65.817
- drat:wt 1 0.7574 104.45 65.855
- drat:qsec 1 0.9284 104.62 65.907
- hp:wt 1 0.9751 104.67 65.922
- cyl 1 1.3134 105.01 66.025
- disp:drat 1 1.4871 105.18 66.078
- disp:wt 1 2.0643 105.76 66.253
- disp:qsec 1 2.6284 106.32 66.423
103.69 67.622
- hp:qsec 1 11.2347 114.93 68.914
Step: AIC=65.82
mpg ~ cyl + disp + hp + drat + wt + qsec + disp:drat + disp:wt +
disp:qsec + hp:wt + hp:qsec + drat:wt + drat:qsec
Df Sum of Sq RSS AIC
- drat:wt 1 0.3278 104.65 63.918
- cyl 1 0.8581 105.19 64.079
- disp:drat 1 0.9812 105.31 64.117
- disp:wt 1 1.5808 105.91 64.299
- drat:qsec 1 3.5700 107.90 64.894
- disp:qsec 1 3.9891 108.32 65.018
- hp:wt 1 4.9122 109.24 65.290
104.33 65.817
- hp:qsec 1 10.6482 114.97 66.927
Step: AIC=63.92
mpg ~ cyl + disp + hp + drat + wt + qsec + disp:drat + disp:wt +
disp:qsec + hp:wt + hp:qsec + drat:qsec
Df Sum of Sq RSS AIC
- cyl 1 0.5400 105.19 62.082
- disp:wt 1 1.2963 105.95 62.312
- disp:qsec 1 4.3458 109.00 63.220
- hp:wt 1 4.6682 109.32 63.314
104.65 63.918
- disp:drat 1 6.8536 111.51 63.948
- drat:qsec 1 9.7048 114.36 64.755
- hp:qsec 1 10.4606 115.11 64.966
Step: AIC=62.08
mpg ~ disp + hp + drat + wt + qsec + disp:drat + disp:wt + disp:qsec +
hp:wt + hp:qsec + drat:qsec
Df Sum of Sq RSS AIC
- disp:wt 1 0.7629 105.96 60.314
- disp:qsec 1 4.0606 109.25 61.294
- disp:drat 1 6.5062 111.70 62.003
- hp:wt 1 6.6559 111.85 62.046
105.19 62.082
- drat:qsec 1 9.7674 114.96 62.924
- hp:qsec 1 10.0669 115.26 63.007
Step: AIC=60.31
mpg ~ disp + hp + drat + wt + qsec + disp:drat + disp:qsec +
hp:wt + hp:qsec + drat:qsec
Df Sum of Sq RSS AIC
- disp:qsec 1 5.084 111.04 59.813
- disp:drat 1 6.681 112.64 60.270
105.96 60.314
- hp:qsec 1 9.530 115.49 61.070
- drat:qsec 1 9.636 115.59 61.099
- hp:wt 1 37.501 143.46 68.010
Step: AIC=59.81
mpg ~ disp + hp + drat + wt + qsec + disp:drat + hp:wt + hp:qsec +
drat:qsec
Df Sum of Sq RSS AIC
- disp:drat 1 2.338 113.38 58.480
- hp:qsec 1 4.557 115.60 59.101
- drat:qsec 1 4.559 115.60 59.101
111.04 59.813
- hp:wt 1 33.019 144.06 66.144
Step: AIC=58.48
mpg ~ disp + hp + drat + wt + qsec + hp:wt + hp:qsec + drat:qsec
Df Sum of Sq RSS AIC
- disp 1 0.036 113.42 56.490
- drat:qsec 1 2.425 115.81 57.157
- hp:qsec 1 5.994 119.37 58.129
113.38 58.480
- hp:wt 1 43.523 156.90 66.876
Step: AIC=56.49
mpg ~ hp + drat + wt + qsec + hp:wt + hp:qsec + drat:qsec
Df Sum of Sq RSS AIC
- drat:qsec 1 2.393 115.81 55.159
- hp:qsec 1 6.022 119.44 56.146
113.42 56.490
- hp:wt 1 47.616 161.03 65.708
Step: AIC=55.16
mpg ~ hp + drat + wt + qsec + hp:wt + hp:qsec
Df Sum of Sq RSS AIC
- drat 1 0.661 116.47 53.341
- hp:qsec 1 5.221 121.03 54.570
115.81 55.159
- hp:wt 1 56.873 172.68 65.943
Step: AIC=53.34
mpg ~ hp + wt + qsec + hp:wt + hp:qsec
Df Sum of Sq RSS AIC
- hp:qsec 1 4.571 121.04 52.573
116.47 53.341
- hp:wt 1 63.799 180.27 65.319
Step: AIC=52.57
mpg ~ hp + wt + qsec + hp:wt
Df Sum of Sq RSS AIC
121.04 52.573
- qsec 1 8.720 129.76 52.799
- hp:wt 1 65.018 186.06 64.331
> drop1(fit4)
Single term deletions
Model:
mpg ~ hp + wt + qsec + hp:wt
Df Sum of Sq RSS AIC
121.04 52.573
qsec 1 8.720 129.76 52.799
hp:wt 1 65.018 186.06 64.331
> summary(fit4)
Call:
lm(formula = mpg ~ hp + wt + qsec + hp:wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-3.8243 -1.3980 0.0303 1.1582 4.3650
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 40.310410 7.677887 5.250 1.56e-05
hp -0.106181 0.026263 -4.043 0.000395
wt -8.681516 1.292525 -6.717 3.28e-07
qsec 0.503163 0.360768 1.395 0.174476
hp:wt 0.027791 0.007298 3.808 0.000733
(Intercept) ***
hp ***
wt ***
qsec
hp:wt ***
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.117 on 27 degrees of freedom
Multiple R-squared: 0.8925, Adjusted R-squared: 0.8766
F-statistic: 56.05 on 4 and 27 DF, p-value: 1.094e-12
可以看出最终得到的拟合区别:
> anova(fit1,fit2,fit3,fit4)
Analysis of Variance Table
Model 1: mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb
Model 2: mpg ~ cyl + disp + hp + drat + wt + qsec
Model 3: mpg ~ cyl + (disp + hp + drat + wt + qsec)^2
Model 4: mpg ~ hp + wt + qsec + hp:wt
Res.Df RSS Df Sum of Sq F Pr(>F)
1 21 147.49
2 25 163.48 -4 -15.982 0.5795 0.6821
3 15 103.43 10 60.045 0.8708 0.5772
4 27 121.04 -12 -17.610 0.2128 0.9949
fit4的结果最好。
再看看fit4的拟合图:
检验:
1、相关性分析,相关系数高于0.8,表明存在多重共线性;但相关系数低,并不能表示不存在多重共线性;
2、vif检验;
3、条件系数检验;