天天看點

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

線上性模型的文章中,我們已經了解了如何在給出協變量x的向量時構造線性模型。但更一般而言,我們可以考慮協變量的變換,來使用線性模型。

我們首先讨論多項式回歸,進一步,我們會想到分段線性或分段多項式函數,可能還有附加的連續性限制,這些是樣條曲線回歸的基礎。

多項式回歸

談論多項式回歸時(在單變量情況下)

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

我們使用

  1.  coef = leg.poly(n=4)
  2.  [[1]]
  3.  1
  4.  [[2]]
  5.  x
  6.  [[3]]
  7.  -0.5 + 1.5*x^2
  8.  [[4]]
  9.  -1.5*x + 2.5*x^3
  10.  [[5]]
  11.  0.375 - 3.75*x^2 + 4.375*x^4

有許多正交多項式族(​​Jacobi多項式​​​,  ​​Laguerre多項式​​​,  ​​Hermite多項式​​等)。在R中有用于多項式回歸的标準多邊形函數。

當使用poly時,我們使用矩陣的 ​​QR分解​​。我們使用

  1.  poly - function (x, deg = 1) {
  2.  xbar = mean(x)
  3.  x = x - xbar
  4.  QR = qr(outer(x, 0:degree, "^"))
  5.  X = qr.qy(QR, diag(diag(QR$qr),

這兩個模型是等效的。

  1.  dist~speed+I(speed^2)+I(speed^3)
  2.  dist~poly(speed,3)
拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

我們有完全相同的預測

  1.  v1[u==15]
  2.  121
  3.  38.43919
  4.  v2[u==15]
  5.  121
  6.  38.43919

系數沒有相同的解釋,但是p值完全相同,兩個模型以相同的置信度拒絕三次多項式,

  1.  summary(reg1)
  2.  Coefficients:
  3.  Estimate Std. Error t value Pr(>|t|)
  4.  (Intercept) -19.50505 28.40530 -0.687 0.496
  5.  speed 6.80111 6.80113 1.000 0.323
  6.  I(speed^2) -0.34966 0.49988 -0.699 0.488
  7.  I(speed^3) 0.01025 0.01130 0.907 0.369
  8.  Residual standard error: 15.2 on 46 degrees of freedom
  9.  Multiple R-squared: 0.6732, Adjusted R-squared: 0.6519
  10.  F-statistic: 31.58 on 3 and 46 DF, p-value: 3.074e-11
  11.  summary(reg2)
  12.  Coefficients:
  13.  Estimate Std. Error t value Pr(>|t|)
  14.  (Intercept) 42.98 2.15 19.988 < 2e-16 ***
  15.  poly(speed, 3)1 145.55 15.21 9.573 1.6e-12 ***
  16.  poly(speed, 3)2 23.00 15.21 1.512 0.137
  17.  poly(speed, 3)3 13.80 15.21 0.907 0.369
  18.  ---
  19.  Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  20.  Residual standard error: 15.2 on 46 degrees of freedom
  21.  Multiple R-squared: 0.6732, Adjusted R-squared: 0.6519
  22.  F-statistic: 31.58 on 3 and 46 DF, p-value: 3.074e-11

B樣條曲線(B-spline curve)和GAM

樣條曲線在回歸模型中也很重要,尤其是當我們開始讨論 ​​廣義加性模型時​​。在單變量情況下,我通過引入(線性)樣條曲線,

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

模型是連續的(連續函數的權重總和是連續的)。我們可以進一步 

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

二次樣條

用于三次樣條。有趣的是,二次樣條不僅是連續的,而且它們的一階導數也是連續的(三次樣條是連續的)。這些模型易于解釋。例如,簡單的模型

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

是以下連續的分段線性函數,在節點s處分段。

拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

還應遵守以下解釋:對于xx較小的值,線性增加,斜率\beta_1β1\;對于xx較大的值,線性減小,斜率\ beta_1 + \beta_2β1+β2。是以,\beta_2β2被解釋為斜率的變化。

現在在R中使用bs函數(即标準​​B樣條)​​并可視化

  1.  x = seq(5,25,by=.25)
  2.  B = bs(x,knots=c(10,20),Boundary.knots=c(5,55),degre=1)
  3.  matplot(x,B,type="l",lty=1,lwd=2,col=clr6)
拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

提到的函數如下

  1.  par(mfrow=c(1,2))
  2.  matplot(x,B,type="l",lty=1,lwd=2)
  3.  matplot(x,B,type="l",col=clr)
拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸

多項式回歸中這兩個模型表示方法是等效的。例如

  1.  dist~speed+pos(speed,10)+pos(speed,20
  2.  dist~bs(speed,degree=1,knots=c(10,20)
拓端tecdat|R語言輔導中的多項式回歸、B樣條曲線(B-spline Curves)回歸
  1.  v1[u==15]
  2.  121
  3.  39.35747
  4.  v2[u==15]
  5.  121
  6.  39.35747

這兩個模型以及系數的解釋是等效的:

  1.  summary(reg1)
  2.  Coefficients:
  3.  Estimate Std. Error t value Pr(>|t|)
  4.  (Intercept) -7.6305 16.2941 -0.468 0.6418
  5.  speed 3.0630 1.8238 1.679 0.0998 .
  6.  pos(speed, 10) 0.2087 2.2453 0.093 0.9263
  7.  pos(speed, 20) 4.2812 2.2843 1.874 0.0673 .
  8.  ---
  9.  Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  10.  Residual standard error: 15 on 46 degrees of freedom
  11.  Multiple R-squared: 0.6821, Adjusted R-squared: 0.6613
  12.  F-statistic: 32.89 on 3 and 46 DF, p-value: 1.643e-11
  13.  summary(reg2)
  14.  Coefficients:
  15.  Estimate Std. Error t value Pr(>|t|)
  16.  (Intercept) 4.621 9.344 0.495 0.6233
  17.  bs(speed, degree = 1, knots = c(10, 20))1 18.378 10.943 1.679 0.0998 .
  18.  bs(speed, degree = 1, knots = c(10, 20))2 51.094 10.040 5.089 6.51e-06 ***
  19.  bs(speed, degree = 1, knots = c(10, 20))3 88.859 12.047 7.376 2.49e-09 ***
  20.  ---
  21.  Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  22.  Residual standard error: 15 on 46 degrees of freedom
  23.  Multiple R-squared: 0.6821, Adjusted R-squared: 0.6613
  24.  F-statistic: 32.89 on 3 and 46 DF, p-value: 1.643e-11

繼續閱讀