Ridge regression with `glmnet` gives different coefficients than what I compute by “textbook definition”?

前端 未结 2 859
清酒与你
清酒与你 2020-12-03 23:42

I am running Ridge regression with the use of glmnet R package. I noticed that the coefficients I obtain from glmnet::glmnet function

2条回答
  •  清歌不尽
    2020-12-04 00:15

    Adding on top of Zheyuan's interesting post, did some more experiments to see that we can get the same results with intercept as well, as follows:

    # ridge with intercept glmnet
    ridge.fit.cv.int <- cv.glmnet(X, Y, alpha = 0, intercept = TRUE, family="gaussian")
    ridge.fit.lambda.int <- ridge.fit.cv.int$lambda.1se
    ridge.coef.with.int <- as.vector(as.matrix(coef(ridge.fit.cv.int, s = ridge.fit.lambda.int)))
    
    # ridge with intercept definition, use the same lambda obtained with cv from glmnet
    X.with.int <- cbind(1, X)
    ridge.coef.DEF.with.int <- drop(solve(crossprod(X.with.int) + ridge.fit.lambda.int * diag(n.tmp, p.tmp+1), crossprod(X.with.int, Y)))
    
    ggplot() + geom_point(aes(ridge.coef.with.int, ridge.coef.DEF.with.int))
    

    # comupte residuals
    RSS.fit.cv.int <- sum((Y.true - predict(ridge.fit.cv.int, newx=X))^2) # predict adds inter
    RSS.DEF.int <- sum((Y.true - X.with.int %*% ridge.coef.DEF.with.int)^2)
    
    RSS.fit.cv.int
    [1] 110059.9
    RSS.DEF.int
    [1] 110063.4
    

提交回复
热议问题