This question is a follow-up to a prior question.

Basically, I wanted to study under what conditions when we regress the residuals to x1, we will get R2 of 20%.

As a first step to attack this problem, my question is, how do I express R2 in matrix form?

Then I will try to express “R2 of regressing residuals to x1” using matrix form.

Also, how can I add regression weights into the expression?

**Answer**

We have

R2=1−∑e2i∑(yi−ˉy)2=1−e′e˜y′˜y,

where ˜y is a vector y demeaned.

Recall that ˆβ=(X′X)−1X′y, implying that e=y−Xˆβ=y−X(X′X)−1X′y. Regression on a vector of 1s, written as l, gives the mean of y as the predicted value and residuals from that model produce demeaned y values; ˜y=y−ˉy=y−l(l′l)−1l′y.

Let H=X(X′X)−1X′ and let M=l(l′l)−1l′, where l is a vector of 1’s. Also, let I be an identity matrix of the requisite size. Then we have

R2=1−e′e˜y′˜y=1−y′(I−H)′(I−H)yy′(I−M)′(I−M)y=1−y′(I−H)yy′(I−M)y,

where the second line comes from the fact that H and M (and I) are idempotent.

In the weighted case, let Ω be the weighting matrix used in the OLS objective function, e′Ωe. Additionally, let Hw=XΩ1/2(X′ΩX)−1Ω1/2X′ and Mw=lΩ1/2(l′Ωl)−1Ω1/2l′. Then,

R2=1−y′Ω1/2(I−Hw)Ω1/2yy′Ω1/2(I−Mw)Ω1/2y,

**Attribution***Source : Link , Question Author : Luna , Answer Author : Charlie*