CRS Hat Operator
crshat.RdConstruct or apply the linear hat operator for fixed-structure CRS mean regression fits. The helper is intended for fixed-\(X\) workflows such as fast plot bootstrapping and constrained-estimation diagnostics.
Usage
crshat(object, ...)
# S3 method for class 'crs'
crshat(object,
newdata = NULL,
y = NULL,
output = c("matrix", "apply", "constraint"),
deriv = 0,
deriv.index = 1,
rcond.min = 1e-08,
use.svd.fallback = TRUE,
...)Arguments
- object
A fitted mean-regression object of class
"crs".- newdata
Optional evaluation data. If omitted, the training data are used.
- y
Optional response vector or matrix. Required for
output="constraint"; when supplied withoutput="apply", the operator is applied directly without returning a dense hat matrix.- output
One of
"matrix","apply", or"constraint".- deriv
Non-negative integer derivative order.
deriv=0returns the fitted-mean hat operator. Positive values return the derivative hat operator for continuous predictorderiv.index.- deriv.index
Positive integer identifying the continuous predictor whose derivative operator is requested.
- rcond.min
Minimum reciprocal condition number used by the weighted least-squares primitive before falling back to QR/SVD policy.
- use.svd.fallback
Logical value controlling the weighted least-squares fallback policy.
- ...
Additional arguments, currently unused.
Details
For a fitted CRS mean-regression object, crshat() returns the linear
operator mapping the training response to fitted values at newdata. The
default output="matrix" returns a dense matrix with class
c("crshat", "matrix"). The output="apply" route applies the same
operator to y and is preferable when many right-hand sides are needed.
The helper preserves the fitted object's fixed spline/kernel structure, including basis family, degree, segments, pruning state, categorical-kernel bandwidths, weights, and the package's weighted least-squares rank policy. It does not run bandwidth or degree selection.
For deriv > 0, crshat() returns the derivative operator
\(H^{(s)}\) such that \(H^{(s)} y\) matches the corresponding CRS
gradient prediction at newdata. This is the CRS analogue of
npreghat's derivative-selector behavior in np. Derivative operators
are available for fixed-structure mean CRS fits across additive, tensor, and
GLP bases, including categorical-kernel fits. If the requested derivative
order exceeds the fitted spline degree for that predictor, the returned
operator is zero.
Quantile CRS objects remain intentionally unsupported because quantile estimation is not linear in the response.
Value
For output="matrix", a matrix of dimension
nrow(newdata) x nrow(training data). For output="apply", fitted
values produced by applying the operator to y. For
output="constraint", the transpose operator scaled by the supplied
one-column response vector.
Examples
set.seed(42)
x <- runif(30)
y <- sin(2 * pi * x) + rnorm(30, sd = 0.1)
fit <- crs(y ~ x, cv = "none", degree = 2, segments = 1,
display.warnings = FALSE, display.nomad.progress = FALSE)
nd <- data.frame(x = seq(0.1, 0.9, length.out = 5))
H <- crshat(fit, newdata = nd)
drop(H %*% fit$y)
#> [1] 1.0053166 0.3225651 -0.1685696 -0.4680874 -0.5759884
crshat(fit, newdata = nd, output = "apply")
#> [1] 1.0053166 0.3225651 -0.1685696 -0.4680874 -0.5759884
Hd <- crshat(fit, newdata = nd, deriv = 1, deriv.index = 1)
drop(Hd %*% fit$y)
#> [1] -3.89279983 -2.93471560 -1.97663136 -1.01854712 -0.06046289