I am learning how to derive the RSS formula and there is a step that is not making sense to me:
$\sum_{i=1}^n (x_i-\bar x)(y_i-\bar y) + \underbrace{\sum_{i=1}^n \bar x(y_i+\hat \beta x_i)}_{Q} - \underbrace{ \sum_{i=1}^n \bar x(\bar y-\hat\beta \bar x)}_M-\hat \beta \sum _{i=1}^n(\bar x-x_i)^2$
$Q=n\bar x\bigg(\frac{1}{n}\sum_{i=1}^ny_i-\hat \beta \frac{1}{n}\sum_{i=1}^n x_i\bigg)=n\bar x(\bar y-\hat \beta \bar x)=M$
And for this reason, Q and M cancel out. My question is that I do not understand step 2. I don't understand how distributing the summation term in the beginning, turns the inside components into "bar" terms...
I believe there's a typo, and $Q = \sum_{i=1}^n \bar{x}(y_i-\hat{\beta}x_i) $ in step 1.
From there, note that $\bar{x}$ is a constant, so it can be factored out of the sum:
$$ Q = \bar{x} \sum_{i=1}^n{(y_i+ \hat{\beta}x_i)} .$$
Then, we split the summation into two separate sums:
$$ Q = \bar{x} \left( \sum_{i=1}^n{y_i} + \sum_{i=1}^n {\hat{\beta}x_i} \right).$$
Then, the same thing happens with $\hat{\beta}$ since it's a constant too:
$$ Q = \bar{x} \left( \sum_{i=1}^n{y_i} - \hat{\beta}\sum_{i=1}^n {x_i} \right).$$
The final step comes from the fact that $\bar{x}=\frac{1}{n}\sum_i {x_i} \Longrightarrow n\bar{x} = \sum_i {x_i}$:
$$ Q = \bar{x} \left( n\bar{y} - \hat{\beta}n\bar{x} \right).$$
$$ \Rightarrow Q = n\bar{x} \left( \bar{y} - \hat{\beta}\bar{x} \right).$$