Skip to content
Prev 68018 / 398506 Next

refitting lm() with same x, different y

William Valdar wrote:
Actually you do not need to calculate the residuals to be able to 
calculate RSS.  If you write Q = [Q1 Q2] where Q1 is the first p columns 
and Q2 is the remaining n - p columns and R1 for the first p rows of R 
then your expression for RSS can be extended as

   RSS = || Q'y - Rb || = || Q1'y - R1 b || + || Q2'y ||

because the last n - p rows of R are zero.  At the least squares value 
of b the first term is zero when X has full column rank.  Thus

   rss <- sum(qr.qty(X.qr, y)[-(1:p)]^2)

qr.qty should be slightly faster than qr.resid because it performs only 
one (virtual) multiplication by Q or Q'.  I doubt that the difference 
would be noticeable in practice.