Search code examples
rglmnet

how to do ridge regression with log-link in R


I want to do a ridge regression in R by using glmnet or lm.ridge.

I need to do this regression with log(Y)

cost ~ size + weight ⇒ log(cost) ~ size + weight

However, I found that there is no link like glm for glmnet or lm.ridge.

Any ideas for this issue?


Solution

  • Use the alpha input parameter (with 0 value) for the ?glmnet function. As the documentation says:

    alpha=1 is the lasso penalty, and alpha=0 the ridge penalty.

    Try something like the following:

    glmnet(x=cbind(size, weight), y=log(cost), alpha=0, family='gaussian')
    

    or may be with poission regression

    glmnet(x=cbind(size, weight), y=cost, alpha=0, family='poission')
    

    If your input data is not too huge, you can calculate the learnt weights by ridge regression from the training data directly by using the formula solve(t(X)%*%X + λ*I)%*%(t(X)%*%y), where X is your input variables matrix, y is response variable and I is the identity matrix, you can learn the best value of the lambda parameter using cross-validation from a held-out dataset.