I am trying to fit a linear model using matrices to my data set even though I can use OLS and do it without matrices as a simple tutorial for myself to better understand both R
and matrix notation.
This is the model I am trying to fit:
$$\bf Y=X\boldsymbol\beta+\varepsilon$$
where $\bf Y$ is a $1\times n$ matrix, $\bf X$ is a $n\times k$ matrix (where $k$ is the number of $\beta$'s, which in this case is 2), $\boldsymbol \beta$ is a $k\times 1$ matrix and lastly our error term is $n\times 1$. I understand this portion.
When I simply use the lm()
command to fit my data, I get the following from the summary()
command:
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-4.0503 -1.4390 0.4921 1.0589 3.9446
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.9849 0.8219 3.632 0.00191 **
x 0.5612 0.1084 5.178 6.32e-05 ***
So the summary()
is telling me that the $\beta$ matrix is a $2\times 1$ matrix, with the first number (which is $\beta_0$) as 2.0949 and the second number (which is $\beta_1)$ as 0.1084. My question is this:
We know that the matrix $\beta$ is actually:
$$\boldsymbol\beta=(\bf{X}^T\bf{X})^{-1}(\bf{X}^T\bf{Y})$$
and when I try to simply carry out this calculate by hand using R using b=(t(x)*x)^-1*t(x)*y
, I get a $1\times 20$ vector (where $20$ of course is $n$, the number of observations). Why am I not getting a $2\times 1$ matrix like I should be getting?
R
code so it's impossible to tell what you're doing wrong. I believe there isR
code posted on this site doing exactly what you're trying, so searches on relevant keywords like r and regression as well as on likely parts of the code (such assolve
) might turn up some useful stuff for you. $\endgroup$^-1
in R, and in fact shouldn't explicitly invert $X^TX$ at all. (Also you don't do matrix multiplication with*
). You should solve $(X^TX)\hat\beta = (X^TY)$. See?solve
, which does both solution of linear systems and inversion. That's still not the best function to use if you're trying to be accurate (QR decomposition is probably the most common way these days), but it will do for getting the ideas down. $\endgroup$