I am trying to extend the lwr()
function of the package McSptial
, which fits weigthed regressions as non-parametric estimation. In the core of the lwr()
function, it inverts a matrix using solve()
instead of a QR decomposition, resulting in numerical instability. I would like to change it but can't figure out how to get the hat matrix (or other derivatives) from the QR decomposition afterward.
With data :
set.seed(0); xmat <- matrix(rnorm(500), nrow=50) ## model matrix
y <- rowSums(rep(2:11,each=50)*xmat) ## arbitrary values to let `lm.wfit` work
w <- runif(50, 1, 2) ## weights
The lwr()
function goes as follows :
xmat2 <- w * xmat
xx <- solve(crossprod(xmat, xmat2))
xmat1 <- tcrossprod(xx, xmat2)
vmat <- tcrossprod(xmat1)
I need the value of, for instance :
sum((xmat[1,] %*% xmat1)^2)
sqrt(diag(vmat))
For the moment I use reg <- lm.wfit(x=xmat, y=y, w=w)
but cannot manage to get back what seems to me to be the hat matrix (xmat1
) out of it.
This old question is a continuation of another old question I have just answered: Compute projection / hat matrix via QR factorization, SVD (and Cholesky factorization?). That answer discusses 3 options for computing hat matrix for an ordinary least square problem, while this question is under the context of weighted least squares. But result and method in that answer will be the basis of my answer here. Specifically, I will only demonstrate the QR approach.
OP mentioned that we can use
lm.wfit
to compute QR factorization, but we could do so usingqr.default
ourselves, which is the way I will show.Before I proceed, I need point out that OP's code is not doing what he thinks.
xmat1
is not hat matrix; instead,xmat %*% xmat1
is.vmat
is not hat matrix, although I don't know what it is really. Then I don't understand what these are:The second one looks like the diagonal of hat matrix, but as I said,
vmat
is not hat matrix. Well, anyway, I will proceed with the correct computation for hat matrix, and show how to get its diagonal and trace.Consider a toy model matrix
X
and some uniform, positive weightsw
:We first obtain
X1
(X_tilde in the latex paragraph) by row rescaling toX
:Then we perform QR factorization to
X1
. As discussed in my linked answer, we can do this factorization with or without column pivoting.lm.fit
orlm.wfit
hencelm
is not doing pivoting, but here I will use pivoted factorization as a demonstration.Note we did not go on computing
tcrossprod(Q)
as in the linked answer, because that is for ordinary least squares. For weighted least squares, we wantQ1
andQ2
:If we only want the diagonal and trace of the hat matrix, there is no need to do a matrix multiplication to first get the full hat matrix. We can use
In linear regression,
d
is the influence of each datum, and it is useful for producing confidence interval (usingsqrt(d)
) and standardised residuals (usingsqrt(1 - d)
). The trace, is the effective number of parameters or effective degree of freedom for the model (hence I call itedf
). We see thatedf = 10
, because we have used 10 parameters:X
has 10 columns and it is not rank-deficient.Usually
d
andedf
are all we need. In rare cases do we want a full hat matrix. To get it, we need an expensive matrix multiplication:Hat matrix is particularly useful in helping us understand whether a model is local / sparse of not. Let's plot this matrix (read
?image
for details and examples on how to plot a matrix in the correct orientation):We see that this matrix is completely dense. This means, prediction at each datum depends on all data, i.e., prediction is not local. While if we compare with other non-parametric prediction methods, like kernel regression, loess, P-spline (penalized B-spline regression) and wavelet, we will observe a sparse hat matrix. Therefore, these methods are known as local fitting.