分段回歸( piecewise regression ),顧名思義,回歸式是“分段”拟合的。其靈活用于響應變量随自變量值的改變而存在多種響應狀态的情況,二者間難以通過一種回歸模型預測或解釋時,不妨根據響應狀态找到合适的斷點位置,然後将自變量劃分為有限的區間,并在不同區間内分别建構回歸描述二者關系。 分段回歸最簡單最常見的類型就是分段線性回歸( piecewise linear regression ),即各分段内的局部回歸均為線性回歸。
本文我們試圖預測車輛的制動距離,同時考慮到車輛的速度。
1. > summary(reg)
2.
3. Call:
4.
5. Residuals:
6. Min 1Q Median 3Q Max
7. -29.069 -9.525 -2.272 9.215 43.201
8.
9. Coefficients:
10. Estimate Std. Error t value Pr(>|t|)
11. (Intercept) -17.5791 6.7584 -2.601 0.0123 *
12. speed 3.9324 0.4155 9.464 1.49e-12 ***
13. ---
14. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
15.
16. Residual standard error: 15.38 on 48 degrees of freedom
17. Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
18. F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
要手動進行多個預測,可以使用以下代碼(循環允許對多個值進行預測)
1. for(x in seq(3,30)){
2.
3. + Yx=b0+b1*x
4. + V=vcov(reg)
5. + IC1=Yx+c(-1,+1)*1.96*sqrt(Vx)
6. + s=summary(reg)$sigma
7. + IC2=Yx+c(-1,+1)*1.96*s
然後在一個随機選擇的20個觀測值的基礎上進行線性回歸。
lm(dist~speed,data=cars[I,])
目的是使觀測值的數量對回歸品質的影響可視化。
1.
2. Residuals:
3. Min 1Q Median 3Q Max
4. -23.529 -7.998 -5.394 11.634 39.348
5.
6. Coefficients:
7. Estimate Std. Error t value Pr(>|t|)
8. (Intercept) -20.7408 9.4639 -2.192 0.0418 *
9. speed 4.2247 0.6129 6.893 1.91e-06 ***
10. ---
11. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
12.
13. Residual standard error: 16.62 on 18 degrees of freedom
14. Multiple R-squared: 0.7252, Adjusted R-squared: 0.71
15. F-statistic: 47.51 on 1 and 18 DF, p-value: 1.91e-06
16.
17. > for(x in seq(3,30,by=.25)){
18.
19. + Yx=b0+b1*x
20. + V=vcov(reg)
21. + IC=Yx+c(-1,+1)*1.96*sqrt(Vx)
22. + points(x,Yx,pch=19
可以使用R函數進行預測,具有置信區間
- fit lwr upr
- 1 42.62976 34.75450 50.50502
- 2 84.87677 68.92746 100.82607
- > predict(reg,
- fit lwr upr
- 1 42.62976 6.836077 78.42344
當有多個解釋變量時,“可視化”回歸就變得更加複雜了
- > image(VX2,VX3,VY)
- > contour(VX2,VX3,VY,add=TRUE)
這是一個回歸三維曲面圖
> persp(VX2,VX3,VY,ticktype=detailed)
我們将更詳細地讨論這一點,但從這個線性模型中可以很容易地進行非線性回歸。我們從距離對數的線性模型開始
- > abline(reg1)
因為我們在這裡沒有任何關于距離的預測,隻是關于它的對數......但我們稍後會讨論它
lm(sqrt(dist)~speed,data=cars)
還可以轉換解釋變量。你可以設定斷點(門檻值)。我們從一個訓示變量開始
1.
2. Residuals:
3. Min 1Q Median 3Q Max
4. -29.472 -9.559 -2.088 7.456 44.412
5.
6. Coefficients:
7. Estimate Std. Error t value Pr(>|t|)
8. (Intercept) -17.2964 6.7709 -2.555 0.0139 *
9. speed 4.3140 0.5762 7.487 1.5e-09 ***
10. speed > s TRUE -7.5116 7.8511 -0.957 0.3436
11. ---
12. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
13.
14. Residual standard error: 15.39 on 47 degrees of freedom
15. Multiple R-squared: 0.6577, Adjusted R-squared: 0.6432
16. F-statistic: 45.16 on 2 and 47 DF, p-value: 1.141e-11
但是你也可以把函數放在一個分段的線性模型裡,同時保持連續性。
1.
2. Residuals:
3. Min 1Q Median 3Q Max
4. -29.502 -9.513 -2.413 5.195 45.391
5.
6. Coefficients:
7. Estimate Std. Error t value Pr(>|t|)
8. (Intercept) -7.6519 10.6254 -0.720 0.47500
9. speed 3.0186 0.8627 3.499 0.00103 **
10. speed - s 1.7562 1.4551 1.207 0.23350
11. ---
12. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
13.
14. Residual standard error: 15.31 on 47 degrees of freedom
15. Multiple R-squared: 0.6616, Adjusted R-squared: 0.6472
16. F-statistic: 45.94 on 2 and 47 DF, p-value: 8.761e-12
在這裡,我們可以想象幾個分段
1. posi=function(x) ifelse(x>0,x,0)
2.
3.
4. Coefficients:
5. Estimate Std. Error t value Pr(>|t|)
6. (Intercept) -7.6305 16.2941 -0.468 0.6418
7. speed 3.0630 1.8238 1.679 0.0998 .
8. positive(speed - s1) 0.2087 2.2453 0.093 0.9263
9. positive(speed - s2) 4.2812 2.2843 1.874 0.0673 .
10. ---
11. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
12.
13. Residual standard error: 15 on 46 degrees of freedom
14. Multiple R-squared: 0.6821, Adjusted R-squared: 0.6613
15. F-statistic: 32.89 on 3 and 46 DF, p-value: 1.643e-11