R plot.glmnet增加变量标签的大小
目前,我正在使用glmnet软件包运行lasso回归(在下面的示例中,它被保存到“fits”变量中。然后,当我绘制fits变量时,它会正确显示,但系数标签非常小。有没有办法增加这些变量的大小 可复制的例子如下R plot.glmnet增加变量标签的大小,r,plot,glmnet,R,Plot,Glmnet,目前,我正在使用glmnet软件包运行lasso回归(在下面的示例中,它被保存到“fits”变量中。然后,当我绘制fits变量时,它会正确显示,但系数标签非常小。有没有办法增加这些变量的大小 可复制的例子如下 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
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")
require(glmnet)
#使用5个变量设置示例DF
种子集(123)
sampleDF由于标签大小看起来像是硬编码的(全局更改为cex
将更改其他绘图功能),因此您可以更改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, ...))
查看'cex.axis,cex.lab',它们只是基本的R图形参数。它们改变了轴文本,但系数线标签似乎没有改变。我猜它们是以不同的方式从glmnet软件包中放在那里的?如果你在plot.glmnet
然后plotCoef
(最后第三行)它看起来像是标签大小ishard codedAh找到的,看起来像是硬编码到plotCoef命令中。感谢您的帮助。很好的示例和绘图显示了差异。还帮助我了解了如何查看现有函数内部并替换硬编码部分。谢谢。不客气。(我使用了as.list(body(myPlot))
获取[[14]]
)@TrevorNederlof;重新考虑一下——定义一个新函数来绘制标签可能更容易
newplotter(fits,label=TRUE,xvar="lambda", cex=1.2)