Splines

Practical entry page for crs spline workflows, constrained fits, and spline-specific examples.
Keywords

crs, regression splines, crssigtest, constrained splines, crsiv, spline examples

crs estimates a regression function \(g(x)=E(Y\mid X=x)\) with spline bases, including mixed-data, derivative, instrumental-variable, and shape-constrained workflows.

As of crs 0.15-46, spline plotting uses the same named-control style as np and npRmpi. For ordinary fitted curves and two-continuous-predictor surfaces, use plot(model, ...) directly rather than manually building evaluation grids. The companion crshat() helper gives constrained spline examples the same one-call hat-operator style used by the kernel packages. This release also adds formula/data and selected-state S3 workflows for crsiv() and crsivderiv().

First stop

Use the built-in example to verify the installation before moving to the longer scripts.

## Load the package and run the built-in example harness
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.

Example scripts

The *_rgl.R scripts require rgl in addition to crs and are best viewed in RStudio, where the interactive surface appears in the Viewer pane rather than the Plot pane.

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 constrained scripts estimate \(g(x)\) or one of its partial derivatives, use crshat(..., output = "constraint") to form the corresponding restriction operator, solve the quadratic program, and refit the restricted surface. Start with radial_constrained_mean.R.

Instrumental spline regression: crsiv

For spline-based instrumental regression, crs provides crsiv and crsivderiv. The current training-grid interface accepts y ~ z | w and, where supported, y ~ z | w | x; fitted objects support structured summaries, fitted values, residuals, and selected-state prediction.

Source file: crs_iv_quickstart.R

## Estimate a spline structural function g(z) with one instrument w.

library(crs)

## Build a small IV example with one endogenous regressor and one instrument.
set.seed(42)
n <- 100
v <- rnorm(n, sd = 0.27)
eps <- rnorm(n, sd = 0.05)
u <- -0.5 * v + eps
w <- rnorm(n)
z <- 0.2 * w + v
y <- z^2 + u
iv_dat <- data.frame(y, z, w)

## Fit through the current formula/data interface.
fit_iv <- crsiv(y ~ z | w,
  data = iv_dat,
  method = "Landweber-Fridman",
  cv = "exhaustive",
  nmulti = 1,
  cv.threshold = 0)

## Inspect the selected structural fit and its training residuals.
summary(fit_iv)
head(fitted(fit_iv))
head(residuals(fit_iv))

If you want a longer package demo, the package sources also ship crsiv.R and crsiv_exog.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.

## Build a simple spline fit that mimics a linear regression structure
library(crs)

n <- 1000
x1 <- runif(n)
x2 <- runif(n)
z1 <- rbinom(n, 1, 0.1)
z2 <- rbinom(n, 1, 0.1)
y <- x1 + x2^2 + z1 + z2 + rnorm(n)
z1 <- factor(z1)
z2 <- factor(z2)

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 getting started 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.
Back to top