Correlation is a statistical measure that indicates how strongly two variables are related. It involves the relationship between multiple variables as well. For instance, if one is interested to know whether there is a relationship between the heights of fathers and sons, a correlation coefficient can be calculated to answer this question. Generally, it lies between -1 and +1. It is a scaled version of covariance and provides direction and strength of a relationship. There are mainly two types of correlation:
- Parametric Correlation – Pearson correlation(r): It measures a linear dependence between two variables (x and y) 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, are known as non-parametric correlation.
Pearson Rank Correlation Coefficient Formula
Pearson Rank Correlation is a parametric correlation. The Pearson correlation coefficient is probably the most widely used measure for linear relationships between two normal distributed variables and thus often just called “correlation coefficient”. The formula for calculating the Pearson Rank Correlation is as follows:
r: pearson correlation coefficient
x and y: two vectors of length n
mx and my: corresponds to the means of x and y, respectively.
- r takes value between -1 (negative correlation) and 1 (positive correlation).
- r = 0 means no correlation.
- Can not be applied to ordinal variables.
- The sample size should be moderate (20-30) for good estimation.
- Outliers can lead to misleading values means not robust with outliers.
Implementation in R
R Language provides two methods to calculate the pearson 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 = “pearson”)
cor.test(x, y, method = “pearson”)
x, y: numeric vectors with the same length
method: correlation method
Pearson correlation coefficient is: 0.5357143
Pearson's product-moment correlation data: x and y t = 1.4186, df = 5, p-value = 0.2152 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.3643187 0.9183058 sample estimates: cor 0.5357143
In the output above:
- T is the value of the test statistic (T = 1.4186)
- p-value is the significance level of the test statistic (p-value = 0.2152).
- alternative hypothesis is a character string describing the alternative hypothesis (true correlation is not equal to 0).
- sample estimates is the correlation coefficient. For Pearson correlation coefficient it’s named as cor (Cor.coeff = 0.5357).
Data: Download the CSV file here.
Person correlation coefficient is: -0.8782815 Pearson's product-moment correlation data: x and y t = -31.709, df = 298, p-value < 2.2e-16 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.9018288 -0.8495329 sample estimates: cor -0.8782815
- Python - Pearson Correlation Test Between Two Variables
- Spearman Correlation Testing in R Programming
- Kendall Correlation Testing in R Programming
- Python - Pearson's Chi-Square Test
- Covariance and Correlation in R Programming
- Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
- Visualize correlation matrix using correlogram in R Programming
- Visualize Correlation Matrix using symnum function in R Programming
- Redundancy and Correlation in Data Mining
- Exploring Correlation in Python
- Python - Kendall Rank Correlation Coefficient
- Spearman's Rank Correlation
- Hypothesis Testing in R Programming
- Unit Testing in R Programming
- Understanding Hypothesis Testing
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Set or View the Graphics Palette in R Programming - palette() Function
- tidyr Package in R Programming
- Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
- Intersection of Two Objects in R Programming - intersect() Function