Revision 12acd76ec1d613505e4bb20b3012e8d8507a310a authored by Lars Kotthoff on 16 May 2018, 20:38:09 UTC, committed by cran-robot on 16 May 2018, 20:38:09 UTC
1 parent 970c4e6
Raw File
selector.correlation.R
### correlation.body
# regression
# continous data
correlation.body <- function(formula, data, type = c("pearson", "spearman")) {
	type = match.arg(type)
	
	new_data = get.data.frame.from.formula(formula, data)
	
	lapply(new_data, function(vec) {
		if(is.factor(vec))
			stop("All data must be continous.")
	})
	
	class_data = new_data[[1]]
	new_data = new_data[-1] #new_data without class attr
	
	class_data_complete = complete.cases(class_data)
	results = abs(sapply(new_data, function(attr_data) {
			complete = complete.cases(attr_data) & class_data_complete
			if(!any(complete))
				return(NA)
			vec1 = class_data[complete]
			vec2 = attr_data[complete]
			if(sd(vec1) == 0 || sd(vec2) == 0)
				return(NA)
			return(cor(vec1, vec2, method = type))
		}))
	
	attr_names = dimnames(new_data)[[2]]
	return(data.frame(attr_importance = results, row.names = attr_names))
}

linear.correlation <- function(formula, data) {
	return(correlation.body(formula, data, "pearson"))
}

rank.correlation <- function(formula, data) {
	return(correlation.body(formula, data, "spearman"))
}
back to top