Skip to content
Related Articles

Related Articles

Improve Article

Spearman Correlation Testing in R Programming

  • Last Updated : 16 Jul, 2020

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:

  • Parametric Correlation – Pearson correlation(r) : It measures a linear dependence between two variables (x and y) and is known as a parametric correlation test because it depends on the distribution of the data.
  • Non-Parametric Correlation – Kendall(tau) and Spearman(rho): They are rank-based correlation coefficients, known as non-parametric correlation.

Spearman Correlation formula

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

{{\displaystyle r_s= 1 - \frac {6\sum d_i^2}{n(n^2 - 1)}

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:

  • rs takes a value between -1 (negative association) and 1 (positive association).
  • rs = 0 means there is no association.
  • If association is monotonically increasing then rs = 1.
  • If association is monotonically decreasing then rs = -1.
  • It can be used when association is nonlinear.
  • It can be applied for ordinal variables.

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

Example 1:

# 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:

  • S is the value of the test statistic (S = 10.871)
  • p-value is the significance level of the test statistic (p-value = 0.4397).
  • alternative hypothesis is a character string describing the alternative hypothesis (true rho is not equal to 0).
  • sample estimates is the correlation coefficient. For Spearmann correlation coefficient it’s named as rho (Cor.coeff = 0.4564).

Example 2:

Data: 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 



My Personal Notes arrow_drop_up
Recommended Articles
Page :