I wanted to compute a simple regression using the lm
and plain matrix algebra. However, my regression coefficients obtained from matrix algebra are only half of tho
lm
is not performing standardization. If you want to obtain the same result by lm
, you need:
X1 <- cbind(1, X) ## include intercept
solve(crossprod(X1), crossprod(X1,Y))
# [,1]
#[1,] 4.2222222
#[2,] 1.0000000
#[3,] -0.3333333
#[4,] 1.0000000
#[5,] 0.6666667
#[6,] 2.3333333
#[7,] 1.3333333
I don't want to repeat that we should use crossprod
. See the "follow-up" part of Ridge regression with glmnet gives different coefficients than what I compute by “textbook definition”?