全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SPSS论坛
1928 1
2014-04-13
I applied some data to find the best variables solution of regression model using ridge regression in R. I have used lm.ridge and glmnet (when alpha=0), but the results are very different especially when lambda=0. It suppose that both parameter estimators have the same values. So, what is the problem here? best regards
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-4-13 05:47:19
glmnet standardizes the y variable and uses the mean squared errors instead of sum of squared errors. So you need to make the appropriate adjustments to match their outputs.

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
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群