[R] glmnet: converting coefficients back to original scale
Mark Seeto
markseeto at gmail.com
Sat Apr 4 12:09:49 CEST 2015
Thanks for your reply Mehmet. I've found that the problem was that I
didn't scale the lambda value. My original example did not follow the
instruction not to give a single lambda value, but that in itself
wasn't the problem. Example shown below.
library(glmnet)
library(MASS)
set.seed(1)
n <- 20
d <- data.frame(x1 = rnorm(n, 1, 1),
x2 = rnorm(n, 10, 2),
y = rnorm(n, 1, 2))
# Sample means
mx1 <- mean(d$x1)
mx2 <- mean(d$x2)
my <- mean(d$y)
# Scaling factors
sx1 <- sd(d$x1)*sqrt((n-1)/n)
sx2 <- sd(d$x2)*sqrt((n-1)/n)
sy <- sd(d$y)*sqrt((n-1)/n)
# Scaled variables
d$x1s <- (d$x1 - mx1)/sx1
d$x2s <- (d$x2 - mx2)/sx2
d$ys <- (d$y - my)/sy
# Centred y
d$yc <- d$y - my
lam <- 1 # lambda value for lm.ridge
lmr1 <- lm.ridge(y ~ x1 + x2, data=d, lambda=lam)
lmr2 <- lm.ridge(yc ~ x1s + x2s, data=d, lambda=lam)
coef(lmr1)
my - coef(lmr2)["x1s"]*mx1/sx1 - coef(lmr2)["x2s"]*mx2/sx2
# same as coef(lmr1)[1]
coef(lmr2)["x1s"]/sx1 # same as coef(lmr1)["x1"]
coef(lmr2)["x2s"]/sx2 # same as coef(lmr1)["x2"]
glmnet1 <- glmnet(as.matrix(d[, c("x1", "x2")]), d[, "y"], alpha=0)
glmnet2 <- glmnet(as.matrix(d[, c("x1s", "x2s")]), d[, "ys"], alpha=0)
# Note: glmnet1$lambda is glmnet2$lambda*sy
ind <- 80 # index of lambda values to look at
coef(glmnet1)[, ind]
my - coef(glmnet2)["x1s", ind]*mx1*sy/sx1 -
coef(glmnet2)["x2s", ind]*mx2*sy/sx2
# same as coef(glmnet1)["(Intercept)", ind]
coef(glmnet2)["x1s", ind]*sy/sx1
# same as coef(glmnet1)["x1", ind]
coef(glmnet2)["x2s", ind]*sy/sx2
# same as coef(glmnet1)["x2", ind]
On Sat, Apr 4, 2015 at 6:03 AM, Suzen, Mehmet <mehmet.suzen at physics.org> wrote:
> This is interesting, can you post your lm.ridge solution as well? I
> suspect in glmnet, you need to use model.matrix with intercept, that
> could be the reason.
>
> -m
More information about the R-help
mailing list