# Which one is the correct formula to found b variable from OLS?

I got a formula that is from the slides of class that is:

So, because I couldn't fully understand I wanted to look the formula in the web, but I didn't get that one. But instend, its this the formula that it appear.

This is a little more easy to comprehend. I've tried to understand the first one, but couldn't. When I use the same values get differents outcomes.

I've used 4 points

And got 0.3386882129 For the first formula and 0.247972973 for the second one.

Some my dubt is, this are two formulas for different things, Or could it be that the second formula is misspelled?

• Are the two expressions different from each other? Expand the former. Commented Feb 7, 2023 at 16:22
• @User1865345 yes, one come from a College slide, meanwhile the othe one I just search up "Ols B formula". It supposed to have the same meaning Commented Feb 7, 2023 at 16:24
• @RodParedes What User1865345 meant is that, if you expand $\sum (x - \bar{x})(y - \bar{y})$ and do the algebra, you will reach $\sum xy - n\bar{x}\bar{y}$ (though, a more precise and complete formula should include subscripts $i$). By the way, you must have made some mistake in your numerical example. Commented Feb 7, 2023 at 16:29

These give the same answer when I do it, and they should.

set.seed(2023)
N <- 5
b1 <- function(x, y){
n <- length(x)
return(
(sum(x * y) - n * mean(x) * mean(y))
/
(sum(x^2) - n * (mean(x))^2)
)
}
b2 <- function(x, y){

return(
(sum((x - mean(x)) * (y - mean(y))))
/
(sum((x - mean(x))^2))
)

}
x <- c(3, 55, 15, 17)
y <- c(1.4, 2.2, 4.5, 5)
b1(x, y) # = -0.01119501
b2(x, y) # = -0.01119501

In both equations, the numerator represents the covariance between $$X$$ and $$Y$$, and the denominator represents the variance of $$X$$ (assuming the omitted subscripts work in the usual way, which could be where the OP found a discrepancy between the two equations).

Working this out from the definition of covariance could be a worthwhile exercise to do once.

Both your calculations should give $$\dfrac{36.7}{148}\approx 0.247973$$.

You should check your arithmetic for the first which more in detail should be $$\dfrac{167.7-4\times 10 \times 3.275}{548 - 4 \times 10 \times 10}$$

In general $$\sum (x_i - \bar{x})(y_i - \bar{y})$$ $$= \sum x_i y_i - \sum x_i \bar{y} - \bar{x} \sum y_i + \sum \bar{x} \bar{y}$$ $$= \sum x_i y_i - n \bar{x} \bar{y} -n \bar{x} \bar{y} +n \bar{x} \bar{y}$$ $$= \sum x_i y_i - n \bar{x} \bar{y}$$

and similarly $$\sum (x_i - \bar{x})^2$$ $$= \sum x_i ^2 - n \bar{x}^2$$ .