Find Least Squares Regression Line

910 Views Asked by At

I have a problem where I need to find the least squares regression line. I have found $\beta_0$ and $\beta_1$ in the following equation

$$y = \beta_0 + \beta_1 \cdot x + \epsilon$$

So I have both the vectors $y$ and $x$.

I know that $\hat{y}$ the vector predictor of $y$ is $x \cdot \beta$ and that the residual vector is $\epsilon = y - \hat{y}$.

I know also that the least squares regression line looks something like this $$\hat{y} = a + b \cdot x$$ and that what I need to find is $a$ and $b$, but I don't know exactly how to do it. Currently I am using Matlab, and I need to do it in Matlab. Any idea how should I proceed, based on the fact that I am using Matlab?

Correct me if I did/said something wrong anyway.

1

There are 1 best solutions below

8
On BEST ANSWER

First define

X = [ones(size(x)) x];

then type

regress(y,X)

Observations:

  • the first step is to include a constant in the regression (otherwise you would be imposing $a=0$).

  • the output will be a vector with the OLS estimates $(a,b)$.