# I've found two equations for regression slope, but they give me two different answers. What am I missing?

According to my notes, in a statistical model where

$$Y_i=\beta_0 + \beta_1(x) + u$$

(where $u$ is the error term) the predicted slope is $$\hat{\beta}_1 = \frac{\operatorname{Cov}(X, y)}{\operatorname{Var}(X)}$$ According to this Khan academy video the slope equals $$\frac{\overline{X} \overline{Y} - \overline{XY}}{\overline{X}^2 - \overline{X^2}}$$ When I calculate the two given a basic data set the numbers are WAY off. Are these equations related, or am I way off here?

-
I'm trying to add formula-formatting to your equations using LaTeX, but I can't figure out the very last one... there must be a mistake in it, right? Could you review the formula you give after "...video the slope equals..." – Patrick Coulombe Jun 9 '14 at 0:45
If you click on the video link, it's the yellow formula in the yellow box in the lower right. – themindseye Jun 9 '14 at 0:58
You should show your working of an example – Glen_b Jun 9 '14 at 1:00
Theoretically should these equations yield the same result? – themindseye Jun 9 '14 at 1:08
The correct formulas do. See my answer. The question is what did you do. If we see an example, we might see your error. – Glen_b Jun 9 '14 at 1:24

Your question contains the following formulas:

1. $\hat\beta_1=\text{Cov}(x,y)/\text{Var}(x)$

2. $\hat\beta_1=\bar{x}\bar{y} - \overline{xy} / (\bar{x})^2-\overline{x^2}$

(or rather the question contained that until someone edited it to match the video, thus obscuring what I believe was likely the OP's original problem)

That second formula is incorrectly transcribed.

The formula given in the video is $\frac{\bar{x}\bar{y} - \overline{xy}}{(\bar{x})^2-\overline{x^2}}$, which is not the same as what you wrote.

Following order of operations, your formula corresponds to $\bar{x}\bar{y} - [\overline{xy} / (\bar{x})^2]-\overline{x^2}$.

The correct way to write it "in line" would be: $[\bar{x}\bar{y} - \overline{xy}] / [(\bar{x})^2-\overline{x^2}]$.

So that's at least one source of potential problem.

If you do them both right, they give the same answer (up to rounding).

That correct version of the second form from the video is weird, because it flips the numerator and denominator about from the usual covariance/variance form, making it a ratio of two negative quantities. It's still right, but likely to be more confusing, and more likely to generate errors from dropped signs.

Here's an example showing the same (correct) result for both

Example (this is 8 random rows from the 'cars' data set in R - the first column displayed below contains the row numbers, which you can ignore):

     speed dist
(x)  (y)
34      18   76
6        9   10
35      18   84
40      20   48
12      12   14
15      12   28
48      24   93
36      19   36

1:
Var(x)   =  25.14286
Cov(x,y) = 133.0714
Cov(x,y)/Var(x) = 5.293

2:
xbar  =  16.5
ybar  =  48.625
xybar = 918.75
x2bar = 294.25

[xbar.ybar - xybar]/[xbar^2 - x2bar] = 5.293

-
Okay thanks. When I got two different answer I wasn't sure if I did it wrong, or if I was just barking up the wrong tree. Sorry for writing it wrong, I had a feeling I would do that that's why I put the link. – themindseye Jun 9 '14 at 1:28