gpt4 book ai didi

r - plot.glmnet 增加变量标签的大小

转载 作者:行者123 更新时间:2023-12-04 02:20:28 24 4
gpt4 key购买 nike

目前我正在使用 glmnet 包来运行套索回归(在下面的示例中,它被保存到“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=.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

# 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, ...))

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

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

enter image description here

关于r - plot.glmnet 增加变量标签的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com