I'm trying to write a functions for multiple regression analysis (y = Xb + e
) using a singular value decomposition for matrices. y
and X
must be the input and regression coefficients vector b
, the residual vector e
and variance accounted for R2
as output. Beneath is what I have so far and I'm totally stuck. The labels
part of the weight also gives me an error. What is this labels
part? Can anybody give me some tips to help me proceed?
Test <- function(X, y) {
x <- t(A) %*% A
duv <- svd(x)
x.inv <- duv$v %*% diag(1/duv$d) %*% t(duv$u)
x.pseudo.inv <- x.inv %*% t(A)
w <- x.pseudo.inv %*% labels
return(b, e, R2)
}
You are off the road... Singular value decomposition is applied to model matrix
X
rather than normal matrixX'X
. The following is the correct procedure:So when writing an R function, we should do
Let's have a test
On the other hand, we can use
.lm.fit
to check correctness:which is exactly the same on coefficients and residuals: