Learn R Programming

sfsmisc (version 0.9-4)

hatMat: Hat Matrix of a Smoother

Description

Compute the hat matrix or smoother matrix, of `any' (linear) smoother, smoothing splines, by default.

Usage

hatMat(x, trace= FALSE,
       pred.sm = function(x, y, ...)
                 predict(smooth.spline(x, y, ...), x = x)$y,
       ...)

Arguments

x
numeric vector or matrix.
trace
logical indicating if the whole hat matrix, or only its trace, i.e. the sum of the diagonal values should be computed.
pred.sm
a function of at least two arguments (x,y) which returns fitted values, i.e. $\hat{y}$, of length compatible to x (and y).
...
optionally further arguments to the smoother function pred.sm.

Value

  • The hat matrix $H$ (if trace = FALSE as per default) or a number, $tr(H)$, the trace of $H$, i.e., $\sum_i H_{ii}$.

    Note that dim(H) == c(n, n) where n <- length(x) also in the case where some x values are duplicated (aka ties).

References

Hastie and Tibshirani (1990). Generalized Additive Models. Chapman & Hall.

See Also

smooth.spline, etc

Examples

Run this code
require(modreg) # for smooth.spline() or loess()

x1 <- c(1:4, 7:12)
H1 <- hatMat(x1, spar = 0.5)
 
matplot(x1, H1, type = "l", main = "columns of smoother hat matrix")

## Example `pred.sm' arguments for hatMat() :
pspl <- function(x,y,...) predict(smooth.spline(x,y, ...), x = x)$y
ploes <-function(x,y,...) predict(loess(y ~ x, ...))
pksm <- function(x,y,...) ksmooth(x,y,"normal", x.points=x, ...)$y
pRmean <- function(x,y,...) run.mean(y, ...)
pRline <- function(x,y,...) run.line(x,y, ...)$y

all.equal(sum(diag((hatMat(c(1:4, 7:12), df = 4)))),
                    hatMat(c(1:4, 7:12), df = 4, trace = TRUE), tol = 1e-12)
## TRUE

Run the code above in your browser using DataLab