原文链接: 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 ‘ ’ 1Residual standard error: 15.38 on 48 degrees of freedomMultiple 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 ‘ ’ 1Residual standard error: 16.62 on 18 degrees of freedomMultiple 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       upr1 42.62976 34.75450  50.505022 84.87677 68.92746 100.82607> predict(reg,       fit      lwr      upr1 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 ‘ ’ 1Residual standard error: 15.39 on 47 degrees of freedomMultiple 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 ‘ ’ 1Residual standard error: 15.31 on 47 degrees of freedomMultiple 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 ‘ ’ 1Residual standard error: 15 on 46 degrees of freedomMultiple 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指标