Stat 203 Lecture 14
heatcap
dataheatcap
heatcap
heatcap
(Intercept) Temp I(Temp^2)
(Intercept) 1.0000000 -0.9984975 0.9941781
Temp -0.9984975 1.0000000 -0.9985344
I(Temp^2) 0.9941781 -0.9985344 1.0000000
heatcap
(Intercept) Temp I(Temp^2)
(Intercept) 1.0000000 -0.9984975 0.9941781
Temp -0.9984975 1.0000000 -0.9985344
I(Temp^2) 0.9941781 -0.9985344 1.0000000
hc_mod1 <- lm( Cp ~ poly(Temp, 1), data=heatcap) # Linear
hc_mod2 <- lm( Cp ~ poly(Temp, 2), data=heatcap) # Quadratic
hc_mod3 <- lm( Cp ~ poly(Temp, 3), data=heatcap) # Cubic
hc_mod4 <- lm( Cp ~ poly(Temp, 4), data=heatcap) # Quartic
zapsmall( summary(hc_mod3,correlation=TRUE)$correlation )
(Intercept) poly(Temp, 3)1 poly(Temp, 3)2 poly(Temp, 3)3
(Intercept) 1 0 0 0
poly(Temp, 3)1 0 1 0 0
poly(Temp, 3)2 0 0 1 0
poly(Temp, 3)3 0 0 0 1
A spline represents the relationship between \(y\) and \(x\) as a series of polynomials joined together at locations called knots, satisfying:
R
Using the splines
package, splines may be fitted with
ns()
: the natural cubic spline, with second derivatives forced to zero at the endpoints of the given intervalbs()
: a B(asis)-splinelibrary(splines); library(GLMsData); data(heatcap)
lm.poly <- lm( Cp ~ poly(Temp, 3), data=heatcap )
lm.ns <- lm( Cp ~ ns(Temp, df=3), data=heatcap )
lm.bs <- lm( Cp ~ bs(Temp, df=3), data=heatcap )
# Comparison:
extractAIC(lm.poly); extractAIC(lm.ns); extractAIC(lm.bs)
[1] 4.0000 -117.1234
[1] 4.0000 -119.2705
[1] 4.0000 -117.1234
Dilemma: