Quadratic spline

Is there a way to tune a quadratic spline (instead of a cubic) to some data?

I have this data, and I didn't seem to find a suitable function in R to do this.

+4
source share
1 answer

Expanding a little higher in the comments, you can use the basic B-spline (implemented in the splines::bs() function) by setting degree=2 , rather than the default degree=3 :

 library(splines) ## Some example data set.seed(1) x <- 1:10 y <- rnorm(10) ## Fit a couple of quadratic splines with different degrees of freedom f1 <- lm(y ~ bs(x, degree = 2)) # Defaults to 2 - 1 = 1 degree of freedom f9 <- lm(y ~ bs(x, degree = 2, df=9)) ## Plot the splines x0 <- seq(1, 10, by = 0.1) plot(x, y, pch = 16) lines(x0, predict(f1, data.frame(x = x0)), col = "blue") lines(x0, predict(f9, data.frame(x = x0)), col = "red") 

enter image description here

+10
source

All Articles