glmnet estandariza la variable y y usa los errores cuadrados medios en lugar de la suma de los errores cuadrados. Por lo tanto, debe realizar los ajustes adecuados para que coincidan con sus resultados.
library(ElemStatLearn)
library(glmnet)
library(MASS)
dof2lambda <- function(d, dof) {
obj <- function(lam, dof) (dof - sum(d ^ 2 / (d ^ 2 + lam))) ^ 2
sapply(dof, function(x) optimize(obj, c(0, 1e4), x)$minimum)
}
lambda2dof <- function(d, lam) {
obj <- function(dof, lam) (dof - sum(d ^ 2 / (d ^ 2 + lam))) ^ 2
sapply(lam, function(x) optimize(obj, c(0, length(d)), x)$minimum)
}
dat <- prostate
train <- subset(dat, train, select = -train)
test <- subset(dat, !train, select = -train)
train.x <- as.matrix(scale(subset(train, select = -lpsa)))
train.y <- as.matrix(scale(train$lpsa))
d <- svd(train.x)$d
dof <- seq(1, 8, 0.1)
lam <- dof2lambda(d, dof)
ridge1 <- lm.ridge(train.y ~ train.x, lambda = lam)
ridge2 <- glmnet(train.x, train.y, alpha = 0, lambda = lam / nrow(train.x))
matplot(dof, t(ridge1$coef), type = 'l')
matplot(lambda2dof(d, ridge2$lambda * nrow(train.x)), t(ridge2$beta), type = 'l')