線上性模型的文章中,我們已經了解了如何在給出協變量x的向量時構造線性模型。但更一般而言,我們可以考慮協變量的變換,來使用線性模型。
我們首先讨論多項式回歸,進一步,我們會想到分段線性或分段多項式函數,可能還有附加的連續性限制,這些是樣條曲線回歸的基礎。
多項式回歸
談論多項式回歸時(在單變量情況下)
我們使用
- coef = leg.poly(n=4)
- [[1]]
- 1
- [[2]]
- x
- [[3]]
- -0.5 + 1.5*x^2
- [[4]]
- -1.5*x + 2.5*x^3
- [[5]]
- 0.375 - 3.75*x^2 + 4.375*x^4
有許多正交多項式族(Jacobi多項式, Laguerre多項式, Hermite多項式等)。在R中有用于多項式回歸的标準多邊形函數。
當使用poly時,我們使用矩陣的 QR分解。我們使用
- poly - function (x, deg = 1) {
- xbar = mean(x)
- x = x - xbar
- QR = qr(outer(x, 0:degree, "^"))
- X = qr.qy(QR, diag(diag(QR$qr),
這兩個模型是等效的。
- dist~speed+I(speed^2)+I(speed^3)
- dist~poly(speed,3)
我們有完全相同的預測
- v1[u==15]
- 121
- 38.43919
- v2[u==15]
- 121
- 38.43919
系數沒有相同的解釋,但是p值完全相同,兩個模型以相同的置信度拒絕三次多項式,
- summary(reg1)
- Coefficients:
- Estimate Std. Error t value Pr(>|t|)
- (Intercept) -19.50505 28.40530 -0.687 0.496
- speed 6.80111 6.80113 1.000 0.323
- I(speed^2) -0.34966 0.49988 -0.699 0.488
- I(speed^3) 0.01025 0.01130 0.907 0.369
- Residual standard error: 15.2 on 46 degrees of freedom
- Multiple R-squared: 0.6732, Adjusted R-squared: 0.6519
- F-statistic: 31.58 on 3 and 46 DF, p-value: 3.074e-11
- summary(reg2)
- Coefficients:
- Estimate Std. Error t value Pr(>|t|)
- (Intercept) 42.98 2.15 19.988 < 2e-16 ***
- poly(speed, 3)1 145.55 15.21 9.573 1.6e-12 ***
- poly(speed, 3)2 23.00 15.21 1.512 0.137
- poly(speed, 3)3 13.80 15.21 0.907 0.369
- ---
- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
- Residual standard error: 15.2 on 46 degrees of freedom
- Multiple R-squared: 0.6732, Adjusted R-squared: 0.6519
- F-statistic: 31.58 on 3 and 46 DF, p-value: 3.074e-11
B樣條曲線(B-spline curve)和GAM
樣條曲線在回歸模型中也很重要,尤其是當我們開始讨論 廣義加性模型時。在單變量情況下,我通過引入(線性)樣條曲線,
模型是連續的(連續函數的權重總和是連續的)。我們可以進一步
二次樣條
用于三次樣條。有趣的是,二次樣條不僅是連續的,而且它們的一階導數也是連續的(三次樣條是連續的)。這些模型易于解釋。例如,簡單的模型
是以下連續的分段線性函數,在節點s處分段。
還應遵守以下解釋:對于xx較小的值,線性增加,斜率\beta_1β1\;對于xx較大的值,線性減小,斜率\ beta_1 + \beta_2β1+β2。是以,\beta_2β2被解釋為斜率的變化。
現在在R中使用bs函數(即标準B樣條)并可視化
- x = seq(5,25,by=.25)
- B = bs(x,knots=c(10,20),Boundary.knots=c(5,55),degre=1)
- matplot(x,B,type="l",lty=1,lwd=2,col=clr6)
提到的函數如下
- par(mfrow=c(1,2))
- matplot(x,B,type="l",lty=1,lwd=2)
- matplot(x,B,type="l",col=clr)
多項式回歸中這兩個模型表示方法是等效的。例如
- dist~speed+pos(speed,10)+pos(speed,20
- dist~bs(speed,degree=1,knots=c(10,20)
- v1[u==15]
- 121
- 39.35747
- v2[u==15]
- 121
- 39.35747
這兩個模型以及系數的解釋是等效的:
- summary(reg1)
- 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 .
- pos(speed, 10) 0.2087 2.2453 0.093 0.9263
- pos(speed, 20) 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
- summary(reg2)
- Coefficients:
- Estimate Std. Error t value Pr(>|t|)
- (Intercept) 4.621 9.344 0.495 0.6233
- bs(speed, degree = 1, knots = c(10, 20))1 18.378 10.943 1.679 0.0998 .
- bs(speed, degree = 1, knots = c(10, 20))2 51.094 10.040 5.089 6.51e-06 ***
- bs(speed, degree = 1, knots = c(10, 20))3 88.859 12.047 7.376 2.49e-09 ***
- ---
- 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