hatMat                package:sfsmisc                R Documentation

_H_a_t _M_a_t_r_i_x _o_f _a _S_m_o_o_t_h_e_r

_D_e_s_c_r_i_p_t_i_o_n:

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

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

       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. y.hat, of length compatible to 'x' (and
          'y').

     ...: optionally further arguments to the smoother function
          'pred.sm'.

_V_a_l_u_e:

     The hat matrix H (if 'trace = FALSE' as per default) or a number,
     tr(H), the _trace_ of H, i.e., sum(i) H[i,i].

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

_A_u_t_h_o_r(_s):

     Martin Maechler maechler@stat.math.ethz.ch

_R_e_f_e_r_e_n_c_e_s:

     Hastie and Tibshirani (1990). _Generalized Additive Models_.
     Chapman & Hall.

_S_e_e _A_l_s_o:

     'smooth.spline', etc

_E_x_a_m_p_l_e_s:

     require(stats) # 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

     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

     ## Loess() example:
     Hl <- hatMat(x1, pr = ploes)
     cat(sprintf("df = %.2f\n", sum(diag(Hl))))
     image(Hl)

