Total members 11891 |It is currently Wed Jul 17, 2024 11:22 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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)

#Read the data first. 
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);

## get correlation to subset of the data.
cor(data[3:length(data)], method="spearman")

## get correlation to all data. It creates matrix of correlations.
cor(data, method="spearman")

## Draw pie charts 
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.



_________________
Sami
PHD student - SAIL - School Of Computing
Queens' University
Canada


Author:
Site Admin
User avatar Posts: 33
Have thanks: 1 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 




Topic Tags

R Analysis






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com