Skip to content

Instantly share code, notes, and snippets.

@coppeliaMLA
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coppeliaMLA/9410790 to your computer and use it in GitHub Desktop.
Save coppeliaMLA/9410790 to your computer and use it in GitHub Desktop.
Example of a cubic spline
x<-seq(1,10, by=0.1)
y<-sin(x/4)+rnorm(91, 0,0.05) #Sin fucntion plus noise
plot(x,y)
#Knots at 2, 4 and 6
x2<-x^2
x3<-x^3
k1<-(x>2)*(x-2)^3
k2<-(x>4)*(x-4)^3
k3<-(x>6)*(x-6)^3
csp<-lm(y~x+x2+x3+k1+k2+k3)
p<-data.frame(x, x2, x3, k1, k2, k3)
lines(x,predict(csp, new.data=p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment