gugl58
11/8/2017 - 2:18 PM

getCVListValues

From a list of CV-Steps, get a single element of all of them

#' Function to "unlist" the previously learned cv.model
#'
#' @param cv.learnmodel.listelement 
#' e.g. cv.coxmodel[["OS"]] for overall survival 
#' @param which.element 
#' which element do you want to extract for all CV-steps? 
#' e.g. "coefNOzero" or "cutoff"
#' 
#' @return
#' @export
#'
#' @examples
#' melted.coefs <- getCV.list.values(cv.modellist[[survtime]], which.element = "coefNOzero")
getCV.list.values <- function(cv.learnmodel.listelement, which.element="coefNOzero"){
	inst.load.packages("reshape2", silent = TRUE)
	tmp <- lapply(cv.learnmodel.listelement
				  , function(x){
				  	tmp3 <- data.frame(x[[which.element]])
				  	if(ncol(tmp3) == 1){
					  	colnames(tmp3) <- which.element
				  	}
				  	if(is.null(x[[which.element]])){
				  		tmp3 <- matrix(-1, nrow=0, ncol = 2)
				  	}else if(is.null(dim(x[[which.element]]))){
				  		tmp3$names <- names(x[[which.element]])
				  	}else if(nrow(x[[which.element]]) == 0){
				  		tmp3 <- matrix(-1, nrow=0, ncol = 2)
				  	}else{
				  		tmp3$names <- rownames(x[[which.element]])
				  	}
				  		# colnames(tmp3) <- c(which.element, "names")
				  	return(tmp3)
				  })
	
	tmp.melted <- do.call(rbind, tmp)
	if(is.null(tmp.melted) || nrow(tmp.melted) == 0){
		tmp.melted <- data.frame("value"=0, "names"="InAllCVSteps_noModelFound"
								 ,"variable"=which.element, "L1"="None")
	}else{
		tmp.melted$L1 <- sub("\\..*", "", rownames(tmp.melted)) 	# CVstep1,2,3,4..., allSamples
		tmp.melted$variable <- which.element
		rownames(tmp.melted) <- NULL
		colnames(tmp.melted)[1] <- "value"
		
		tmp2 <- unique(tmp.melted$names)
		tmp.melted$names <- factor(tmp.melted$names, levels = tmp2[order(nchar(tmp2), tmp2)])
		tmp2 <- unique(tmp.melted$L1)
		tmp.melted$L1 <- factor(tmp.melted$L1, levels = tmp2[order(nchar(tmp2), tmp2)]) #  this sorts the unique cv-step names numerically.
	}
	
	
	return(tmp.melted)
}