Correlation Test Between Two Variables in R

This article has been updated, you are now consulting an old release of this article!

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

Note that online software is also available here to compute correlation coefficient between two variables without any installation.

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.

Infos

This analysis was performed using R (ver. 3.1.0).

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

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!