# 多项式回归

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

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

summary(reg1)

Coefficients:
Estimate Std. Error t value Pr(&gt;|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(&gt;|t|)
(Intercept) 42.98 2.15 19.988 &lt; 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

# 二次样条


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(&gt;|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(&gt;|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

01-22 46
01-21 73
01-20 51
01-19 139
01-18 87
01-15 71
01-13 161
01-12 170
01-08 891
01-07 178
01-06 156
01-05 124
01-05 135
01-04 464
01-04 156
01-04 122