R:绘制套索贝塔系数
R: Plotting lasso beta coefficients
我用 R 写了这个套索代码,我得到了一些 beta 值:
#Lasso
library(MASS)
library(glmnet)
Boston=na.omit(Boston)
x=model.matrix(crim~.,Boston)[,-1]
rownames(x)=c()
y=as.matrix(Boston$crim)
lasso.mod =glmnet(x,y, alpha =1, lambda = 0.1)
beta=as.matrix(rep(0,19))
beta=coef(lasso.mod)
matplot(beta,type="l",lty=1,xlab="L1",ylab="beta",main="lasso")
我想在这样的情节中绘制贝塔:
但我不知道我可以使用 R 中的哪个 plot 函数来做到这一点。
我不明白你为什么不想使用 build-in glmnet 方法但是你
当然可以重现其结果(此处使用 ggplot)。
您仍然需要模型对象来提取 lambda 值...
编辑: 添加了 Coefs vs L1 范数
重现您的最小示例
library(MASS)
library(glmnet)
#> Le chargement a nécessité le package : Matrix
#> Le chargement a nécessité le package : foreach
#> Loaded glmnet 2.0-13
library(ggplot2)
library(reshape)
#>
#> Attachement du package : 'reshape'
#> The following object is masked from 'package:Matrix':
#>
#> expand
Boston=na.omit(Boston)
x=model.matrix(crim~.,Boston)[,-1]
y=as.matrix(Boston$crim)
lasso.mod =glmnet(x,y, alpha =1)
beta=coef(lasso.mod)
提取 coef 值并将它们转换为适合 ggplot 的长而整洁的形式
tmp <- as.data.frame(as.matrix(beta))
tmp$coef <- row.names(tmp)
tmp <- reshape::melt(tmp, id = "coef")
tmp$variable <- as.numeric(gsub("s", "", tmp$variable))
tmp$lambda <- lasso.mod$lambda[tmp$variable+1] # extract the lambda values
tmp$norm <- apply(abs(beta[-1,]), 2, sum)[tmp$variable+1] # compute L1 norm
使用 ggplot 绘图:coef 与 lambda
# x11(width = 13/2.54, height = 9/2.54)
ggplot(tmp[tmp$coef != "(Intercept)",], aes(lambda, value, color = coef, linetype = coef)) +
geom_line() +
scale_x_log10() +
xlab("Lambda (log scale)") +
guides(color = guide_legend(title = ""),
linetype = guide_legend(title = "")) +
theme_bw() +
theme(legend.key.width = unit(3,"lines"))
同底图glmnet方法:
# x11(width = 9/2.54, height = 8/2.54)
par(mfrow = c(1,1), mar = c(3.5,3.5,2,1), mgp = c(2, 0.6, 0), cex = 0.8, las = 1)
plot(lasso.mod, "lambda", label = TRUE)
使用 ggplot 绘图:系数与 L1 范数
# x11(width = 13/2.54, height = 9/2.54)
ggplot(tmp[tmp$coef != "(Intercept)",], aes(norm, value, color = coef, linetype = coef)) +
geom_line() +
xlab("L1 norm") +
guides(color = guide_legend(title = ""),
linetype = guide_legend(title = "")) +
theme_bw() +
theme(legend.key.width = unit(3,"lines"))
同底图glmnet方法:
# x11(width = 9/2.54, height = 8/2.54)
par(mfrow = c(1,1), mar = c(3.5,3.5,2,1), mgp = c(2, 0.6, 0), cex = 0.8, las = 1)
plot(lasso.mod, "norm", label = TRUE)
由 reprex package (v0.2.0) 创建于 2018-02-26。
我用 R 写了这个套索代码,我得到了一些 beta 值:
#Lasso
library(MASS)
library(glmnet)
Boston=na.omit(Boston)
x=model.matrix(crim~.,Boston)[,-1]
rownames(x)=c()
y=as.matrix(Boston$crim)
lasso.mod =glmnet(x,y, alpha =1, lambda = 0.1)
beta=as.matrix(rep(0,19))
beta=coef(lasso.mod)
matplot(beta,type="l",lty=1,xlab="L1",ylab="beta",main="lasso")
我想在这样的情节中绘制贝塔:
但我不知道我可以使用 R 中的哪个 plot 函数来做到这一点。
我不明白你为什么不想使用 build-in glmnet 方法但是你
当然可以重现其结果(此处使用 ggplot)。
您仍然需要模型对象来提取 lambda 值...
编辑: 添加了 Coefs vs L1 范数
重现您的最小示例
library(MASS)
library(glmnet)
#> Le chargement a nécessité le package : Matrix
#> Le chargement a nécessité le package : foreach
#> Loaded glmnet 2.0-13
library(ggplot2)
library(reshape)
#>
#> Attachement du package : 'reshape'
#> The following object is masked from 'package:Matrix':
#>
#> expand
Boston=na.omit(Boston)
x=model.matrix(crim~.,Boston)[,-1]
y=as.matrix(Boston$crim)
lasso.mod =glmnet(x,y, alpha =1)
beta=coef(lasso.mod)
提取 coef 值并将它们转换为适合 ggplot 的长而整洁的形式
tmp <- as.data.frame(as.matrix(beta))
tmp$coef <- row.names(tmp)
tmp <- reshape::melt(tmp, id = "coef")
tmp$variable <- as.numeric(gsub("s", "", tmp$variable))
tmp$lambda <- lasso.mod$lambda[tmp$variable+1] # extract the lambda values
tmp$norm <- apply(abs(beta[-1,]), 2, sum)[tmp$variable+1] # compute L1 norm
使用 ggplot 绘图:coef 与 lambda
# x11(width = 13/2.54, height = 9/2.54)
ggplot(tmp[tmp$coef != "(Intercept)",], aes(lambda, value, color = coef, linetype = coef)) +
geom_line() +
scale_x_log10() +
xlab("Lambda (log scale)") +
guides(color = guide_legend(title = ""),
linetype = guide_legend(title = "")) +
theme_bw() +
theme(legend.key.width = unit(3,"lines"))
同底图glmnet方法:
# x11(width = 9/2.54, height = 8/2.54)
par(mfrow = c(1,1), mar = c(3.5,3.5,2,1), mgp = c(2, 0.6, 0), cex = 0.8, las = 1)
plot(lasso.mod, "lambda", label = TRUE)
使用 ggplot 绘图:系数与 L1 范数
# x11(width = 13/2.54, height = 9/2.54)
ggplot(tmp[tmp$coef != "(Intercept)",], aes(norm, value, color = coef, linetype = coef)) +
geom_line() +
xlab("L1 norm") +
guides(color = guide_legend(title = ""),
linetype = guide_legend(title = "")) +
theme_bw() +
theme(legend.key.width = unit(3,"lines"))
同底图glmnet方法:
# x11(width = 9/2.54, height = 8/2.54)
par(mfrow = c(1,1), mar = c(3.5,3.5,2,1), mgp = c(2, 0.6, 0), cex = 0.8, las = 1)
plot(lasso.mod, "norm", label = TRUE)
由 reprex package (v0.2.0) 创建于 2018-02-26。