![]() This is all explained in more detail in the R documentation, or the nigh-uncountable intro to R materials online, such as this one.ī<-solve(t(X)%*%X)%*%t(X)%*%y is the literal representation of the normal equations, while b<-solve(crossprod(X), crossprod(X,y)) is faster and more idiomatic, and matches the output of lm(). Otherwise, it does element-wise multiplication and requires your arrays to be conformable according to R's handling of vectors. R uses the operator %*% for matrix multiplication.Raising X to the $-1$ power inverts each element of X, which can occasionally be useful, but is not what we want here. You've made two mistakes in your R code for b. Why am I not getting a $2\times 1$ matrix like I should be getting? $$\boldsymbol\beta=(\bf)$$Īnd 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). We know that the matrix $\beta$ is actually: 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. When I simply use the lm() command to fit my data, I get the following from the summary() command: Call: ![]() ![]() ![]() 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 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |