I'm new to Statistics and R. I'm currently looking through a book called "Discovering Statistics using R". Although the book implies you don't need any statistical background, some of the content isn't covered/explained properly (in my opinion), despite being recommended for beginners... It's a great book though, apart from covering the following:
I'm trying to look at the relationship between x1, x2 and the response variable y using linear regression.
x1=c( 5, 6, 7, 12, 4, 9, 2, 4, 1, 8)
x2=c( 1, 6, 10, 11, 1, 2, 4, 6, 1, 3)
y =c(8.5, 11, 12, 20, 9, 5.5, 11, 5, 2.3, 12)
The linear regression model relating x1, x2 with y is: y = β0 + β1x1 + β2x2
The matrix form is Y = Xβ (where Y is the matrix form of y, β′ = (β0, β1, β2) and X = (1, x1, x2).)
How do I go about finding the estimates of β using the following equation?
$$\widehat{β} = (X′X)^{−1} X′Y$$
If you could point me in the right direction or give another example/reference in R so I can figure this one out, that would be great.
Here is some R code that computes the coefficients using linear algebra $\widehat{β} = (X′X)^{−1} X′Y$, and then using R's built-in function
lm. As you point out, the first column of $X$ in the model $y=X\beta$ is a column of 1s.Note
MASSpackage before using it.%*%.t()