原文連結: http://tecdat.cn/?p=21557
原文出處:拓端資料部落公衆号
分段回歸( piecewise regression ),顧名思義,回歸式是“分段”拟合的。其靈活用于響應變量随自變量值的改變而存在多種響應狀态的情況,二者間難以通過一種回歸模型預測或解釋時,不妨根據響應狀态找到合适的斷點位置,然後将自變量劃分為有限的區間,并在不同區間内分别建構回歸描述二者關系。 分段回歸最簡單最常見的類型就是分段線性回歸( piecewise linear regression ),即各分段内的局部回歸均為線性回歸。
本文我們試圖預測車輛的制動距離,同時考慮到車輛的速度。
> summary(reg)
Call:
Residuals:
Min 1Q Median 3Q Max
-29.069 -9.525 -2.272 9.215 43.201
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.5791 6.7584 -2.601 0.0123 *
speed 3.9324 0.4155 9.464 1.49e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
要手動進行多個預測,可以使用以下代碼(循環允許對多個值進行預測)
for(x in seq(3,30)){
+ Yx=b0+b1*x
+ V=vcov(reg)
+ IC1=Yx+c(-1,+1)*1.96*sqrt(Vx)
+ s=summary(reg)$sigma
+ IC2=Yx+c(-1,+1)*1.96*s
然後在一個随機選擇的20個觀測值的基礎上進行線性回歸。
lm(dist~speed,data=cars[I,])
目的是使觀測值的數量對回歸品質的影響可視化。
Residuals:
Min 1Q Median 3Q Max
-23.529 -7.998 -5.394 11.634 39.348
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -20.7408 9.4639 -2.192 0.0418 *
speed 4.2247 0.6129 6.893 1.91e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 16.62 on 18 degrees of freedom
Multiple R-squared: 0.7252, Adjusted R-squared: 0.71
F-statistic: 47.51 on 1 and 18 DF, p-value: 1.91e-06
> for(x in seq(3,30,by=.25)){
+ Yx=b0+b1*x
+ V=vcov(reg)
+ IC=Yx+c(-1,+1)*1.96*sqrt(Vx)
+ 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)
還可以轉換解釋變量。你可以設定斷點(門檻值)。我們從一個訓示變量開始
Residuals:
Min 1Q Median 3Q Max
-29.472 -9.559 -2.088 7.456 44.412
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.2964 6.7709 -2.555 0.0139 *
speed 4.3140 0.5762 7.487 1.5e-09 ***
speed > s TRUE -7.5116 7.8511 -0.957 0.3436
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.39 on 47 degrees of freedom
Multiple R-squared: 0.6577, Adjusted R-squared: 0.6432
F-statistic: 45.16 on 2 and 47 DF, p-value: 1.141e-11
但是你也可以把函數放在一個分段的線性模型裡,同時保持連續性。
Residuals:
Min 1Q Median 3Q Max
-29.502 -9.513 -2.413 5.195 45.391
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -7.6519 10.6254 -0.720 0.47500
speed 3.0186 0.8627 3.499 0.00103 **
speed - s 1.7562 1.4551 1.207 0.23350
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.31 on 47 degrees of freedom
Multiple R-squared: 0.6616, Adjusted R-squared: 0.6472
F-statistic: 45.94 on 2 and 47 DF, p-value: 8.761e-12
在這裡,我們可以想象幾個分段
posi=function(x) ifelse(x>0,x,0)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -7.6305 16.2941 -0.468 0.6418
speed 3.0630 1.8238 1.679 0.0998 .
positive(speed - s1) 0.2087 2.2453 0.093 0.9263
positive(speed - s2) 4.2812 2.2843 1.874 0.0673 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15 on 46 degrees of freedom
Multiple R-squared: 0.6821, Adjusted R-squared: 0.6613
F-statistic: 32.89 on 3 and 46 DF, p-value: 1.643e-11
正如目前所看到的,後兩個系數的顯著性測試并不意味着斜率為零,而是與左側區域(在兩個門檻值之前)的斜率顯著不同。
最受歡迎的見解
1.R語言多元Logistic邏輯回歸 應用案例
2.面闆平滑轉移回歸(PSTR)分析案例實作
3.matlab中的偏最小二乘回歸(PLSR)和主成分回歸(PCR)
4.R語言泊松Poisson回歸模型分析案例
5.R語言回歸中的Hosmer-Lemeshow拟合優度檢驗
6.r語言中對LASSO回歸,Ridge嶺回歸和Elastic Net模型實作
7.在R語言中實作Logistic邏輯回歸
8.python用線性回歸預測股票價格
9.R語言如何在生存分析與Cox回歸中計算IDI,NRI名額