Open In App

Spearman Correlation Testing in R Programming

The strength of the association between two variables is known as the correlation test. For instance, if one is interested to know whether there is a relationship between the weights of mothers and daughters, a correlation coefficient can be calculated to answer this question. To know more about correlation please refer Correlation.

Methods for Correlation Analysis

There are mainly two types of correlation:



Spearman Correlation formula

Spearman Correlation is a non-parametric correlation also known as rank-based correlation coefficient. The formula for calculating Spearman Correlation is as follows:

where, rs: Spearman Correlation coefficient di: The difference in the ranks given to the two variables values for each item of the data, n: Total number of observation



Note:

Implementation in R

R Language provides two methods to calculate the correlation coefficient. By using the functions cor() or cor.test() it can be calculated. It can be noted that cor() computes the correlation coefficient whereas cor.test() computes test for association or correlation between paired samples. It returns both the correlation coefficient and the significance level(or p-value) of the correlation.

Syntax: cor(x, y, method = “spearman”) cor.test(x, y, method = “spearman”) Parameters: x, y: numeric vectors with the same length method: correlation method

Using cor() method 

# R program to illustrate
# Spearman Correlation Testing
# Using cor()
 
# Taking two numeric
# Vectors with same length
x = c(15, 18, 21, 15, 21)
y = c(25, 25, 27, 27, 27)
 
# Calculating
# Correlation coefficient
# Using cor() method
result = cor(x, y, method = "spearman")
 
# Print the result
cat("Spearman correlation coefficient is:", result)

                    

Output:

Spearman correlation coefficient is: 0.4564355

Using cor.test() method 

# R program to illustrate
# Spearman Correlation Testing
# Using cor.test()
 
# Taking two numeric
# Vectors with same length
x = c(15, 18, 21, 15, 21)
y = c(25, 25, 27, 27, 27)
 
# Calculating
# Correlation coefficient
# Using cor.test() method
result = cor.test(x, y, method = "spearman")
 
# Print the result
print(result)

                    

Output:

Spearman's rank correlation rho

data:  x and y
S = 10.871, p-value = 0.4397
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.4564355 

In the output above:

Example with Dataset

Download the CSV file here

# R program to illustrate
# Spearman Correlation Testing
 
# Import data into RStudio
df = read.csv("Auto.csv")
 
# Taking two column
# Vectors with same length
x = df$mpg
y = df$weight
 
 
# Calculating
# Correlation coefficient
# Using cor() method
result = cor(x, y, method ="spearman")
 
# Print the result
cat("Spearman correlation coefficient is:", result)
 
# Using cor.test() method
res = cor.test(x, y, method = "spearman")
print(res)

                    

Output:

Spearman correlation coefficient is: -0.9140708

    Spearman's rank correlation rho

data:  x and y
S = 8613223, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
       rho 
-0.9140708 

Article Tags :