Residual sum of squares, two different equations, why are they equal? One definition of the Residual Sum of Squares is:
$$
S_r = (y-X\hat{\beta})^T(y-X\hat{\beta})
$$
And I think I understand it. 
Now I have seen a different definition:
$$
S_r = y^Ty- \hat{\beta}^TX^TX\hat{\beta}
$$
I think they supposed to be equal but I can't see why.
I can write (I leave out the \hat on $\beta$ to make the typing easier):
$$
\begin{aligned}
S_r &= y^T(y-X\beta) - \beta^TX^T(y-X\beta)\\
&= y^Ty - y^TX\beta - \beta^TX^T y + \beta^TX^TX\beta
\end{aligned}
$$
and then to make both equations equal I would need to see that $\beta^TX^TX\beta = \beta^TX^T y$ which I don't.
How, do you show that the equations are equal?
 A: The most compact way to see the equality is to use the orthogonal projection matrix $P = X(X'X)^{-1}X'$ and the residual-maker matrix $M = I-P$. Both these matrices are symmetric, $P'=P,\;\; M'=M$ and idempotent $PP=P,\;\; MM = M$. 
We have
$$X\hat{\beta} = Py, \;\;y-X\hat{\beta}= My$$
Then 
$$S_r = (y-X\hat{\beta})'(y-X\hat{\beta}) = (My)'(My) = y'M'My = y'My$$
$$ = y'(I-P)y = y'y - y'Py = y'y-(Py)'Py=y'y- \hat{\beta}'X'X\hat{\beta}$$
A: In math, generally there are more than one way to prove a equation. I think the previous Answers are correct, but differ from your approach. Your approach is correct also, but you need one more step.
At first, you missed hat on $\beta$ in your last equation. Using the fact that $\hat \beta =(X^TX)^{-1}X^Ty$, we have 
$\hat\beta^TX^Ty=y^TX(X^TX)^{-1}X^Ty = y^TX(X^TX)^{-1}(X^TX)(X^TX)^{-1}X^Ty = \hat\beta^TX^TX\hat\beta$
So finished your steps.
A: As @whuber said in the comment, it is essentially Pythagorean Theorem.
Where Let error
$$e=y-X\hat\beta$$
and prediction 
$$
p=X\hat\beta
$$
The first equation is 
$$
e^Te=\|e\|^2
$$
and second equation is $y^Ty-p^Tp=\|y\|^2-\|p\|^2$. Because vector $e$ and vector $p$ are perpendicular, we have $\|y\|^2-\|p\|^2=\|e\|^2$. 
Details can also be found in Gilbert Strangs's linear algebra book. Sample chapter here

