Open In App

Spearman Correlation Testing in R Programming

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Correlation is a key statistical concept used to measure the strength and direction of the relationship between two variables. Unlike Pearson’s correlation, which assumes a linear relationship and continuous data, Spearman’s rank correlation coefficient is a non-parametric measure that assesses how well the relationship between two variables can be described using a monotonic function. This makes it a powerful tool when dealing with ordinal data or non-linear relationships.

What is Spearman Correlation?

Spearman’s correlation, often denoted as Spearman’s rho (ρ), measures the strength and direction of the monotonic relationship between two ranked variables. It ranges from -1 to +1:

  • +1: A perfect positive monotonic relationship.
  • 0: No monotonic relationship.
  • -1: A perfect negative monotonic relationship.

Unlike Pearson’s correlation, Spearman does not assume that the data are normally distributed or linear, which makes it suitable for ordinal data and non-linear relationships. Spearman Correlation is a non-parametric correlation also known as rank-based correlation coefficient. The formula for calculating Spearman Correlation is as follows:


[Tex][ \rho = 1 – \frac{6 \sum d_i^2}{n(n^2 – 1)} ] [/Tex]

Where:

  • [Tex]\rho[/Tex] is the Spearman Correlation coefficient
  • di​ is the difference between the ranks of corresponding variables.
  • n is the number of observations.


Implementation of Spearman Correlation Testing 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.

cor(x, y, method = “spearman”)

cor.test(x, y, method = “spearman”)

Parameters:

  • x, y: numeric vectors with the same length
  • method: correlation method

Now we will discuss both the methods to calculate the Spearman Correlation Testing in R Programming Language.

1: Calculate the Spearman Correlation Using cor() method 

Now we will Calculate the Spearman Correlation Using cor() method .

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

2: Calculate the Spearman Correlation Using cor.test() method 

Now we will Calculate the Spearman Correlation Using cor.test() method .

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

  • 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 Spearman correlation coefficient it’s named as rho (Cor.coeff = 0.4564).

Conclusion

Spearman correlation is a flexible, non-parametric alternative to Pearson correlation, useful for analyzing ordinal data or non-linear relationships. R provides easy-to-use functions such as cor() and cor.test() to calculate Spearman’s rho and assess its statistical significance. Additionally, packages like ggpubr and corrplot can help visualize the relationships between variables.



Next Article

Similar Reads