plot.glmnet 增加变量标签的大小 [英] plot.glmnet increase size of variable labels

查看:104
本文介绍了plot.glmnet 增加变量标签的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用 glmnet 包来运行套索回归(在下面的示例中,它被保存到fits"变量中.然后当我绘制 fits 变量时,它会正确出现,但系数标签非常小.有什么想法可以增加这些的大小吗?

Currently I am using the glmnet package to run a lasso regression (which in the example below is being saved to the "fits" variable. Then when I plot the fits variable it comes up properly but the coefficient labels are very small. Any ideas how I can increase the size of these?

下面可重现的示例...

Reproducible example below...

require(glmnet)

#setup sample DF with 5 variables
set.seed(123)
sampleDF <- data.frame("V1"=rnorm(100,mean=0,sd=.10),"V2"=rnorm(100,mean=0,sd=.10),"V3"=rnorm(100,mean=0,sd=.10),"V4"=rnorm(100,mean=0,sd=.10),"V5"=rnorm(100,mean=0,sd=.10))

#break data into yVector & xMatrix to put into glmnet
yVector <- sampleDF[,1]
xMatrix <- as.matrix(sampleDF[,2:ncol(sampleDF)])

#use k-fold cross validation to find the min lambda
cv.glmmod <- cv.glmnet(xMatrix,yVector,alpha=1,nfolds=nrow(xMatrix),grouped=FALSE)
best_lambda <- cv.glmmod$lambda.min

#run glmnet
fits <- glmnet(xMatrix, yVector, family="gaussian", alpha=1, nlambda=100)

#plot results
plot(fits,label=TRUE,xvar="lambda")

推荐答案

因为看起来标签大小是硬编码的(并且对 cex 的全局更改将更改其他绘图功能),您可以更改 plot.glmnet

As it looks as if the label size is hard coded (and global changes to cex will change the other plot features) you can change plot.glmnet

# copy the plot function
myPlot <- plotCoef

# replace relevant part
body(myPlot)[[14]] <- quote(if (label) {
  nnz = length(which)
  xpos = max(index)
  pos = 4
  if (xvar == "lambda") {
    xpos = min(index)
    pos = 2
  }
  xpos = rep(xpos, nnz)
  ypos = beta[, ncol(beta)]
  text(xpos, ypos, paste(which), pos = pos, ...) # only changed this with ...
})

# copy first level of plot and replace plotCoef  with myPlot
newplotter <- plot.glmnet

body(newplotter)[[3]] <- quote(myPlot(x$beta, lambda = x$lambda, 
                                     df = x$df, dev = x$dev.ratio, 
                                        label = label, xvar = xvar, ...))

这应该增加文本(注意情节需要足够宽)

This should increase the text (note the plot need to be wide enough)

newplotter(fits,label=TRUE,xvar="lambda", cex=1.2)

这篇关于plot.glmnet 增加变量标签的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆