本文介绍了R:绘制套索β系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用R语言编写了这个套索代码,并得到了一些beta值:

I wrote this lasso code in R, and I got some beta values:

#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")

我想在这样的情节中绘制beta:

I want to plot the betas in a plot like this:

但是我不知道我可以用R中的什么绘图功能来做到这一点.

but I don't know what plot function in R I can use to do that.

推荐答案

我不明白为什么您不想使用内置的glmnet方法,但是您可以肯定地重现其结果(此处为ggplot).
您仍然需要模型对象来提取lambda值...

I don't understand why you don't want to use the build-in glmnet method but youcan certainly reproduce its results (here with ggplot).
You still need the model object to extract the lambda values...

添加了Coefs vs L1规范

added Coefs vs L1 norm

重现您的最小示例

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的长而整齐的形式

Extract the coef values and transform them in a long, tidy form suitable for 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 vs lambda

Plot with ggplot : coef vs 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方法相同:

The same with the base plot glmnet method :

# 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绘制的图:coef与L1规范

Plot with ggplot : coef vs L1 norm

# 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方法相同:

The same with the base plot glmnet method :

# 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软件包(v0.2.0)创建于2018-02-26.

Created on 2018-02-26 by the reprex package (v0.2.0).

这篇关于R:绘制套索β系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:10