Spearman Correlation Testing in R Programming

30 Jun, 2022
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.
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 is a non-parametric correlation also known as rank-based correlation coefficients. 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:
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.
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 
Python3
# R program to illustrate# Spearman Correlation Testing# Using cor() # Taking two numeric# Vectors with same lengthx = c(15, 18, 21, 15, 21)y = c(25, 25, 27, 27, 27) # Calculating# Correlation coefficient# Using cor() methodresult = cor(x, y, method = "spearman") # Print the resultcat("Spearman correlation coefficient is:", result)
Output:
Spearman correlation coefficient is: 0.4564355
# Using cor.test() method 
Python3
# R program to illustrate# Spearman Correlation Testing# Using cor.test() # Taking two numeric# Vectors with same lengthx = c(15, 18, 21, 15, 21)y = c(25, 25, 27, 27, 27) # Calculating# Correlation coefficient# Using cor.test() methodresult = cor.test(x, y, method = "spearman") # Print the resultprint(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 Spearman correlation coefficient it’s named as rho (Cor.coeff = 0.4564).
Example 2: Data: Download the CSV file here. 
Python3
# R program to illustrate# Spearman Correlation Testing # Import data into RStudiodf = read.csv("Auto.csv") # Taking two column# Vectors with same lengthx = df$mpgy = df$weight  # Calculating# Correlation coefficient# Using cor() methodresult = cor(x, y, method = "spearman") # Print the resultcat("Spearman correlation coefficient is:", result) # Using cor.test() methodres = 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 
Akanksha_Rai
vinayedula
data-science
Picked
R Machine-Learning
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Printing Output of an R Program
R Programming Language - Introduction
How to Change Axis Scales in R Plots?