R example: spline

# Load in the two functions from last example -- ignore printouts


# Read in the data

voltage <- read.table('http://www-stat.stanford.edu/~jtaylo/courses/stats203/data/voltagedrop.table', header=T, sep=',')

# Use the splines library


# Voltage drop vs. time

plot(time, drop, bg='red', pch=23, cex=2)

# Fit a cubic spline model

spline.lm <- lm(drop ~ bs(time, knots=c(6.5,13)))
lines(time, predict(spline.lm), lwd=2, col='yellow')

# Fit a reduced cubic model: important: this model is contained
# in the spline model.

cubic.lm <- lm(drop ~ poly(time, 3))
lines(time, predict(cubic.lm), lwd=2, lty=2, col='green')

print(Ftest(spline.lm, cubic.lm))

# Piecewise linear spline

pl.lm <- lm(drop ~ bs(time, degree=1, knots=seq(1,19,6)))
lines(time, predict(pl.lm), lwd=4, col='black', lty=3)

# This model is not contained: cannot use Ftest!