Correlation Test Between Two Variables in R
## What is correlation test
Correlation test is used to evaluate an association (dependence) between two variables. Correlation analysis can be performed using different methods. There are Pearson’s product-moment correlation coefficient, Kendall’s tau or Spearman’s rho. These method are described in the following sections.
R function for correlation analysis
The R function cor() can be used to compute the correlation coefficient between two variables, x and y. A simplified format of the function is :
# x and y are numeric vectors
cor(x, y, method = c("pearson", "kendall", "spearman"))
- The pearson correlation method computes a parametric correlation.
- kendall and spearman correlation methods are non-parametric rank-based correlation test.
Compute the coefficient of correlation between two variables using R
data for correlation test
Two variables, x and y, are used in the following examples:
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
x and y are numeric vectors and they must have the same length.
Pearson correlation coefficient
cor(x,y, method="pearson")
[1] 0.5712
Correlation method can be pearson, spearman or kendall
The pearson correlation coefficient measure the linear dependence between two variables.
If method is “kendall” or “spearman”, Kendall’s tau or Spearman’s rho statistic is used to estimate a rank-based measure of association. These are more robust and have been recommended if the data do not come from a bivariate normal distribution.
Note that “spearman” basically computes cor(rank(x), rank(y))
If your data contain missing values, use the following R code to handle missing values by case-wise deletion.
cor(x, y, use = "complete.obs")
p-value of correlation coefficient (Significance levels)
The function cor.test() can be used to compute the significance level for correlation. It tests for association between paired samples using pearson, kendall or spearman methods.
The simplified format is :
# x and y are numeric vectors with the same length
cor.test(x, y, method=c("pearson", "kendall", "spearman"))
The value returned by the function is a list containing, among others, the following components :
statistic | the value of the test statistic. |
p.value | the p-value of the correlation test. |
estimate | correlation coefficient : “cor” (for pearson), “tau” (for kendall) or “rho” (for spearman) |
Pearson correlation test
The test statistic follows a t distribution with length(x)-2 degrees of freedom if the samples follow independent normal distributions.
res<-cor.test(x,y, method="pearson")
res
Pearson's product-moment correlation
data: x and y
t = 1.841, df = 7, p-value = 0.1082
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.1497 0.8956
sample estimates:
cor
0.5712
cor is the correlation coefficient.
The correlation coefficient between x and y are 0.5712 and the p-value is 0.1082.
Kendall rank correlation test
The Kendall rank correlation coefficient or Kendall’s tau statistic is used to estimate a rank-based measure of association. This test may be used if the data do not necessarily come from a bivariate normal distribution.
res<-cor.test(x,y, method="kendall")
res
Kendall's rank correlation tau
data: x and y
T = 26, p-value = 0.1194
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau
0.4444
tau is the Kendall correlation coefficient.
The correlation coefficient between x and y are 0.4444 and the p-value is 0.1194.
Spearman’ rank correlation coefficient
Spearman’s rho statistic is also used to estimate a rank-based measure of association. This test may be used if the data do not come from a bivariate normal distribution.
res<-cor.test(x,y, method="spearman")
res
Spearman's rank correlation rho
data: x and y
S = 48, p-value = 0.0968
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
0.6
rho is the Spearman’s correlation coefficient.
The correlation coefficient between x and y are 0.6 and the p-value is 0.0968.
See also
Infos
This analysis was performed using R (ver. 3.1.0).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Standford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet