Splines
crs, regression splines, crssigtest, constrained splines, crsiv, spline examples
Use crs when regression splines, shape restrictions, or spline-specific workflows are the natural tool rather than kernel estimators.
First stop
If you simply want to verify that the package is installed and working, this is the quickest check.
library(crs)
example(crs)If you want a small downloadable script rather than the package example harness, start with crs_quickstart.R.
What is on this page?
- example scripts for categorical regression splines,
- constrained spline illustrations,
- a short Spline Primer,
- links to the package vignette and related paper,
- a clean split from the kernel material in
npandnpRmpi.
Example scripts
- Radial function in OpenGL: radial_rgl.R
- Sine function in OpenGL: sine_rgl.R
- Constrained spline mean estimation: radial_constrained_mean.R
- Constrained first partial derivative: radial_constrained_first_partial.R
- Constrained second partial derivative: radial_constrained_second_partial.R
The *_rgl.R scripts require rgl in addition to crs.
Start here if you want the concepts first
If you want a short conceptual introduction before diving into scripts, start with Spline Primer.
If you want practical advice on search size, memory, smoothness, and knot selection, go next to Spline Search and Tuning.
A representative constrained example
The longer constrained scripts construct the unrestricted spline fit, formulate restrictions, solve the quadratic programming problem, and then refit the restricted surface. If you want that style of example, start with radial_constrained_mean.R.
Can crs mimic linear regression?
Yes. A simple way to think about this is that spline methods include simpler linear structures as special cases when the basis is restricted appropriately.
library(crs)
n <- 1000
x1 <- runif(n)
x2 <- runif(n)
z1 <- factor(rbinom(n, 1, 0.1))
z2 <- factor(rbinom(n, 1, 0.1))
y <- x1 + x2^2 + z1 + z2 + rnorm(n)
model_crs <- crs(
y ~ x1 + x2 + z1 + z2,
cv = "none",
kernel = FALSE,
degree = rep(1, 2),
segments = rep(1, 2)
)
summary(model_crs)References
- Package vignette: crs vignette on CRAN
- Nie, Z. and J.S. Racine (2012), “The crs Package: Nonparametric Regression Splines for Continuous and Categorical Predictors,” The R Journal, 4(2), 48-56.