R/selector.correlation.R

Defines functions rank.correlation linear.correlation correlation.body

Documented in linear.correlation rank.correlation

### 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"))
}

Try the FSelector package in your browser

Any scripts or data that you put into this service are public.

FSelector documentation built on Aug. 23, 2023, 1:08 a.m.