#
You use the bs() function in the formula before lm , as you want OLS estimates. bs provides basic functions defined by nodes, degree of polynomial, etc.
mod <- lm(y ~ bs(x, knots = seq(0.1, 0.9, by = 0.1)))
You can consider this as a linear model.
> anova(mod) Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) bs(x, knots = seq(0.1, 0.9, by = 0.1)) 12 2997.5 249.792 65.477 < 2.2e-16 *** Residuals 387 1476.4 3.815 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Some pointers to the location of nodes. bs has an argument of Boundary.knots , by default Boundary.knots = range(x) - so when I specified the knots argument above, I did not include boundary nodes.
Read more ?bs .
Plotting a set spline
In the comments, I discuss how to draw an installed spline. One option is to streamline the data in terms of covariance. This is great for one covariate, but you don't need to work on 2 or more covariates. Another problem is that you can only evaluate the installed spline at the observed x values ββ- this is great if you have chosen the covariate tightly, but if not, the spline may look odd with long linear sections.
A more general solution is to use predict to generate predictions from the model for new covariate or covariate values. In the code below, I will show how to do this for the model above, predicting for 100 evenly spaced values ββin the x range.
pdat <- data.frame(x = seq(min(x), max(x), length = 100))
It creates

source share