R example output: spline


R output



R : Copyright 2004, The R Foundation for Statistical Computing
Version 2.0.1 (2004-11-15), ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> invisible(options(echo = TRUE))
> # Load in the two functions from last example -- ignore printouts
>
> source('http://www-stat.stanford.edu/~jtaylo/courses/stats203/R/inference+polynomial/Ftest.R')
>
> # Read in the data
>
> voltage <- read.table('http://www-stat.stanford.edu/~jtaylo/courses/stats203/data/voltagedrop.table', header=T, sep=',')
> attach(voltage)
>
> # Use the splines library
>
> library(splines)
>
> # 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))
F df.N df.D pval
1 207.2876 2 35 0
>
>
> # 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!
>
> proc.time()
[1] 0.56 0.04 0.66 0.00 0.00
>

Plots


Plot #1: Voltage drop vs. time