Debian Codegraph @

man/hatMat.Rd

raw
1 \name{hatMat}
2 \alias{hatMat}
3 \title{Hat Matrix of a Smoother}
4 \description{
5 Compute the hat matrix or smoother matrix, of \sQuote{any} (linear) smoother,
6 smoothing splines, by default.
7 }
8 \usage{
9 hatMat(x, trace= FALSE,
10 pred.sm = function(x, y, ...)
11 predict(smooth.spline(x, y, ...), x = x)$y,
12 \dots)
13 }
14 \arguments{
15 \item{x}{numeric vector or matrix.}
16 \item{trace}{logical indicating if the whole hat matrix, or only its
17 trace, i.e. the sum of the diagonal values should be computed.}
18 \item{pred.sm}{a function of at least two arguments \code{(x,y)}
19 which returns fitted values, i.e. \eqn{\hat{y}}{y.hat}, of length
20 compatible to \code{x} (and \code{y}).}
21 \item{\dots}{optionally further arguments to the smoother function
22 \code{pred.sm}.}
23 }
24 \value{
25 The hat matrix \eqn{H} (if \code{trace = FALSE} as per default) or
26 a number, \eqn{tr(H)}, the \emph{trace} of \eqn{H}, i.e.,
27 \eqn{\sum_i H_{ii}}{sum(i) H[i,i]}.
28
29 Note that \code{dim(H) == c(n, n)} where \code{n <- length(x)} also in
30 the case where some x values are duplicated (aka \emph{ties}).
31 }
32 \references{
33 Hastie and Tibshirani (1990).
34 \emph{Generalized Additive Models}.
35 Chapman & Hall.
36 }
37 \author{Martin Maechler \email{[email protected]}}
38 \seealso{\code{\link{smooth.spline}}, etc.
39 Note the demo, \code{demo("hatmat-ex")}.
40 }
41 \examples{
42 require(stats) # for smooth.spline() or loess()
43
44 x1 <- c(1:4, 7:12)
45 H1 <- hatMat(x1, spar = 0.5) # default : smooth.spline()
46
47 matplot(x1, H1, type = "l", main = "columns of smoother hat matrix")
48
49 ## Example 'pred.sm' arguments for hatMat() :
50 pspl <- function(x,y,...) predict(smooth.spline(x,y, ...), x = x)$y
51 pksm <- function(x,y,...) ksmooth(sort(x),y, "normal", x.points=x, ...)$y
52 ## Rather than ksmooth():
53 if(require("lokern"))
54 pksm2 <- function(x,y,...) glkerns(x,y, x.out=x, ...)$est
55
56 % pRmean <- function(x,y,...) run.mean(y, ...)
57 % pRline <- function(x,y,...) run.line(x,y, ...)$y
58
59 ## Explaining 'trace = TRUE'
60 all.equal(sum(diag((hatMat(c(1:4, 7:12), df = 4)))),
61 hatMat(c(1:4, 7:12), df = 4, trace = TRUE), tol = 1e-12)
62
63 ## ksmooth() :
64 Hk <- hatMat(x1, pr = pksm, bandwidth = 2)
65 cat(sprintf("df = \%.2f\\n", sum(diag(Hk))))
66 image(Hk)
67 Matrix::printSpMatrix(as(round(Hk, 2), "sparseMatrix"))
68
69 ##---> see demo("hatmat-ex") for more (and larger) examples
70 %% should test these also for x with ties
71 }
72 \keyword{smooth}
73 \keyword{regression}