This example shows how to use Spearman correlation in R with pie charts.
- Code:
setwd("D:/FirstPaper/Eclipse/")
require(foreign)
require(ggplot2)
require(MASS)
require(boot)
data <- read.csv("Femo_duplicates_X.csv",head=TRUE );
require(randomForest)
require(ROSE)
if(!require(caret)){
library(caret)
}
if(!require(pROC)){
library(pROC)
}
library (ROCR);
cor(data[3:length(data)], method="spearman")
cor(data, method="spearman")
require(corrgram)
corrgram(data[3:length(data)], method="spearman", upper.panel=panel.pie)
This script is written in R, a programming language and software environment for statistical computing and graphics. The script begins by loading several libraries, including "foreign," "ggplot2," "MASS," and "boot." These libraries provide various functions and tools that are used later in the script. The script then reads in a CSV file called "Femo_duplicates_X.csv" using the read.csv() function and assigns the data to a variable called "data." The argument "head=TRUE" tells R to use the first row of the CSV file as the column names. The script then loads the "randomForest" and "ROSE" libraries and checks to see if the "caret" and "pROC" libraries are already loaded. If not, it loads them.
The script then calculates the correlation between the variables in the data using the cor() function, which takes two arguments: the data and the method of correlation to be used (in this case, "spearman"). The first call to cor() calculates the correlation between the data in columns 3 through the last column of the data, while the second call calculates the correlation between all of the columns in the data, creating a matrix of correlations. Finally, the script uses the corrgram() function to draw pie charts of the correlations calculated in the previous step. The function takes three arguments: the data, the method of correlation to be used, and the type of upper panel to be used for the chart (in this case, "panel.pie").
Code snippet first reads the data from a .cvs file and loads it in a data frame structure. Then, I call the correlation calculation function. In the example above, due to the custom structure of my data I start at column 3. Please note that the cor function does a correlation calculation as a matrix. This means every column will have its own correlation with all the other columns. In the end, I show how to calculate and display the correlation results in a user-friendly look using pie charts.