Debian Codegraph @

R/hatMat.R

raw
1 hatMat <- function(x, trace = FALSE,
2 pred.sm = function(x,y,...)
3 predict(smooth.spline(x,y, ...), x = x)$y,
4 ...)
5 {
6 ## Purpose: Return Hat matrix of a smoother -- very general (but slow)
7 ## -------------------------------------------------------------------------
8 ## Author: Martin Maechler, Date: 7 Mar 2001, 11:12
9 stopifnot(is.logical(trace), length(trace) == 1)
10 n <- NROW(x)
11 if(is.unsorted(x) && !missing(pred.sm))
12 warning("'x' is not sorted increasingly:\n ",
13 " this may be inefficient and lead to wrong results")
14 y <- pred.sm(x, numeric(n), ...)
15 if(!is.numeric(y) || length(y) !=n)
16 stop("`pred.sm' does not return a numeric length n vector")
17 H <- if(trace) 0 else matrix(as.numeric(NA), n,n)
18 for (i in 1:n) {
19 y <- numeric(n) ; y[i] <- 1 # y := e_i ; (e_i)_j = 1_[i=j]
20 y <- pred.sm(x, y, ...)
21 if(trace) H <- H + y[i] else H[,i] <- y
22 }
23 H
24 }
25