PLOTTYPE <- "p" library(MASS) data(mcycle) attach(mcycle) library(splines) library(modreg) postscript("mcycle.ps") par(mfrow=c(2,2)) ### POLYNOMIALS ### plot(times,accel, type=PLOTTYPE, main="Polynomials") lines(times,fitted(lm(accel~times)),lty=1) lines(times,fitted(lm(accel~poly(times,3)))) lines(times,fitted(lm(accel~poly(times,5)))) lines(times,fitted(lm(accel~poly(times,7)))) ### CUBIC SPLINES ### plot(times,accel, type=PLOTTYPE, main="Cubic Splines") for (num in c(5,10,20)) { desmat <- ns(times, knots=quantile(times, (1:num)/(num+1)), intercept=T) tmp <- lm(accel~desmat-1) lines(times, desmat%*%coef(tmp)) } ### LOESS ### plot(times, accel, type=PLOTTYPE, main="Loess") tmp <- loess(accel~times) lines(times, fitted(tmp),) tmp <- loess(accel~times, span=0.3) lines(times, fitted(tmp)) tmp <- loess(accel~times, span=0.3, family="symmetric") lines(times, fitted(tmp)) ### KERNEL SMOOTHING ### plot(times, accel, type=PLOTTYPE, main="Kernel Smoothing") for (bw in c(10, 5,1)) { tmp <- ksmooth(times, accel, kernel="normal", bandwidth=bw) lines(tmp) } graphics.off()