Solving normal equation gives different coefficients from using `lm`?

前端 未结 1 1886
余生分开走
余生分开走 2021-01-24 17:40

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

相关标签:
1条回答
  • 2021-01-24 18:06

    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”?

    0 讨论(0)
提交回复
热议问题