r ggplot.cv.glmnet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r ggplot.cv.glmnet相关的知识,希望对你有一定的参考价值。

ggplot.cv.glmnet <- function(cvobj, sign.lambda=1, n.coefbreaks=15){
	# check if all needed elements for plotting are inside cvobj:
	needed.names <- c("lambda", "nzero", "cvm", "cvlo", "cvup", "lambda.min", "lambda.1se", "name")
	if(any(! needed.names %in% names(cvobj)))
		stop(paste0(needed.names, " are all needed! One/some are missing in the supplied cvobj."))
	
	# get nzero.coefs for some lambda values
	tmp.lambdavals <- sign.lambda*log(cvobj$lambda)
	tmp.nzero.breaks <- labeling::extended(range(tmp.lambdavals)[1]
										   , range(tmp.lambdavals)[2], m = n.coefbreaks)
	which.closest.lambda.to.breaks <- unlist(lapply(tmp.nzero.breaks, 
													function(x)which.min(abs(tmp.lambdavals - x))))
	closest.lambda.to.breaks <- tmp.lambdavals[which.closest.lambda.to.breaks]
	closest.lambda.to.breaks.labels <- cvobj$nzero[which.closest.lambda.to.breaks]
	# until here "get nzero.coefs..."
	
	if(sign.lambda > 0){
		plot0 <- ggplot(tmp.df, aes(x=log(lambda), y=cvm))
	}else{
		plot0 <- ggplot(tmp.df, aes(x=-log(lambda), y=cvm))
	} 
	
	plot0 <- plot0+ 
		geom_errorbar(aes(ymin = cvlo, ymax = cvup), col="gray60")+ 
		geom_point(col="red") +
		ylab(cvobj$name)+
		geom_vline(xintercept = sign.lambda * log(cvobj$lambda.min), linetype="dashed")+
		geom_vline(xintercept = sign.lambda * log(cvobj$lambda.1se), linetype="dashed")+
		annotate("text", label = "Lmin", x = sign.lambda * log(cvobj$lambda.min), y = -Inf, vjust=0)+
		annotate("text", label = "L1se", x = sign.lambda * log(cvobj$lambda.1se), y = -Inf, vjust=0)+
		scale_x_continuous(sec.axis = sec_axis(~., breaks = closest.lambda.to.breaks
											   , labels=closest.lambda.to.breaks.labels))
	return(plot0)
}

以上是关于r ggplot.cv.glmnet的主要内容,如果未能解决你的问题,请参考以下文章

——R的数据组织

+-r, +-s 的所有排列

shinydashboard ui.R 和 server.R 未读取 Global.R

R语言计算回归模型R方(R-Squared)实战

r语言中r-studio怎么调用

R电子书资料《学习R》+《R语言实战第2版》+《R数据科学》学习推荐