| splineX | R Documentation |
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).
splineX(
x,
knots,
degree = NULL,
type = c("ppolys", "ns", "bs"),
intercept = TRUE
)
x |
Numeric vector of predictor values. |
knots |
Numeric vector of interior knots. |
degree |
Integer polynomial degree for |
type |
One of |
intercept |
Logical; include intercept column where applicable. Default: 'TRUE'. |
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).
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
bs, ns
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.