require(stats) # for smooth.spline() or loess()
x1 <- c(1:4, 7:12)
H1 <- hatMat(x1, spar = 0.5) # default : smooth.spline()
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
pksm <- function(x,y,...) ksmooth(sort(x),y, "normal", x.points=x, ...)$y
## Rather than ksmooth():
if(require("lokern"))
pksm2 <- function(x,y,...) glkerns(x,y, x.out=x, ...)$est
## Explaining 'trace = TRUE'
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)
## ksmooth() :
Hk <- hatMat(x1, pr = pksm, bandwidth = 2)
cat(sprintf("df = %.2f\n", sum(diag(Hk))))
image(Hk)
Matrix::printSpMatrix(as(round(Hk, 2), "sparseMatrix"))
##---> see demo("hatmat-ex") for more (and larger) examples
Run the code above in your browser using DataLab