使用R,我试图修改我使用cv.glmnet执行脊回归得到的标准图。
我执行岭回归
lam = 10 ^ seq (-2,3, length =100)
cvfit = cv.glmnet(xTrain, yTrain, alpha = 0, lambda = lam)
我可以通过执行以下
来绘制系数对log lambdaplot(cvfit $glmnet.fit, "lambda")
如何根据实际的lambda值(而不是log lambda)绘制系数,并在图上标记每个预测值?
答案 0 :(得分:1)
您可以这样操作,将值存储在$beta
下的$lambda
和glmnet.fit
下:
library(glmnet)
xTrain = as.matrix(mtcars[,-1])
yTrain = mtcars[,1]
lam = 10 ^ seq (-2,3, length =30)
cvfit = cv.glmnet(xTrain, yTrain, alpha = 0, lambda = lam)
betas = as.matrix(cvfit$glmnet.fit$beta)
lambdas = cvfit$lambda
names(lambdas) = colnames(betas)
使用ggplot解决方案,我们尝试将其长时间旋转并使用log10 x比例尺和ggrepel进行绘制以添加标签:
library(ggplot2)
library(tidyr)
library(dplyr)
library(ggrepel)
as.data.frame(betas) %>%
tibble::rownames_to_column("variable") %>%
pivot_longer(-variable) %>%
mutate(lambda=lambdas[name]) %>%
ggplot(aes(x=lambda,y=value,col=variable)) +
geom_line() +
geom_label_repel(data=~subset(.x,lambda==min(lambda)),
aes(label=variable),nudge_x=-0.5) +
scale_x_log10()
在基数R中,也许像这样,我认为不利之处是您不能很好地看到标签:
pal = RColorBrewer::brewer.pal(nrow(betas),"Set3")
plot(NULL,xlim=range(log10(lambdas))+c(-0.3,0.3),
ylim=range(betas),xlab="lambda",ylab="coef",xaxt="n")
for(i in 1:nrow(betas)){
lines(log10(lambdas),betas[i,],col=pal[i])
}
axis(side=1,at=(-2):2,10^((-2):2))
text(x=log10(min(lambdas)) - 0.1,y = betas[,ncol(betas)],
labels=rownames(betas),cex=0.5)
legend("topright",fill=pal,rownames(betas))