splineX: Build spline design matrices (piecewise polynomials, natural...

View source: R/gareg_knots.R

splineXR Documentation

Build spline design matrices (piecewise polynomials, natural cubic, B-spline)

Description

Unified wrapper to generate spline covariates for three common cases:

  • type = "ppolys": Degree-d regression spline via truncated-power **piecewise polynomials** (uses internal tp_basis()).

  • type = "ns": Degree-3 **natural cubic spline**; enforces f''(a)=f''(b)=0 at the boundary.

  • type = "bs": Degree-d **B-spline** basis (unconstrained).

Usage

splineX(
  x,
  knots,
  degree = NULL,
  type = c("ppolys", "ns", "bs"),
  intercept = TRUE
)

Arguments

x

Numeric vector of predictor values.

knots

Numeric vector of interior knots.

degree

Integer polynomial degree for "ppolys" and "bs". Ignored for "ns" (always cubic). Must be provided for "ppolys" and "bs".

type

One of c("ppolys", "ns", "bs").

intercept

Logical; include intercept column where applicable. Default: 'TRUE'.

Details

Knots are sorted, no-duplicated, and any knots outside range(x) are dropped with a warning. For type = "ns", degree is ignored (natural splines are cubic).

Value

A numeric design matrix. Attributes are attached:

  • "knots" — the interior knots used

  • "boundary"range(x)

  • "degree" — effective degree (i.e., 3 for "ns")

  • "type" — the requested spline type

See Also

bs, ns

Examples

set.seed(1)
x <- sort(rnorm(100))
k <- quantile(x, probs = c(.25, .5, .75))

# 1) Piecewise polynomials (degree 3)
X_pp <- splineX(x, knots = k, degree = 3, type = "ppolys", intercept = TRUE)
dim(X_pp) # n x ((3+1) + 3) = n x 7

# 2) Natural cubic spline (cubic, degree ignored)
X_ns <- splineX(x, knots = k, type = "ns", intercept = TRUE)

# 3) B-spline basis (degree 3)
X_bs <- splineX(x, knots = k, degree = 3, type = "bs", intercept = TRUE)

# Fit without a duplicated intercept:
# fit <- lm(y ~ 0 + X_pp)


GAReg documentation built on March 29, 2026, 5:08 p.m.