Bsplines.R

From Organic Design wiki
Revision as of 02:49, 5 September 2007 by Sven (talk | contribs) (R code for plotting B-splines)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Code snipits and programs written in R, S or S-PLUS

  1. p38

pos <- function(x) ifelse(x>0, x, 0) x <- 1:7 y <- c(8,3,8,5,9,14,11) knot <- 4 plot(x,y) X <- cbind(1,x,x^2, x^3, pos(x-knot)^3) fit <- lm(y~X-1) xx <- seq(1,7, length=200) XX <- cbind(1,xx, xx^2, xx^3, pos(xx-knot)^3) lines(xx, predict(fit, as.data.frame(XX))) # Bug here abline(v=knot, lty=2) X

  1. p41

knots <- c(0,1:3,5,7,8,10,11) atx <- seq(0:11, by=0.1) for(ord in 1:4) { B <- bsplineS(x=atx, norder=ord, breaks=knots, ) matplot(atx, B[,1], type="l", ylim=0:1, lty=2) matlines(atx, B[,-1], col=1, lty=1) title(paste("B splines of order", ord)) abline(v=knots, lty=2) }