The cor.test() function in R

The cor.test() function in R

Today, our article will give you a useful function for using statistics. It is the cor.test(). The function analyzes the association between two variables and represents a report to show the information about them.

What is the correlation test?

In R, the correlation test refers to the analysis of the association between two variables. The correlation coefficient is calculated using Pearson, Kendall, and Spearman formulas.

The cor.test() function in R

The cor.test() function helps to carry out the correlation test based on the available formulas.

Syntax:

cor.test(x, y, method, …)

Parameters:

  • x, y: Two variables to make the correlation test
  • method: Types of formulas to calculate the correlation coefficient. “pearson”, “kendall” or “spearman”
  • …: Other parameters. For more details, please visit the official website.

Examples of using the cor.test() function 

To feed the function, we will use the features in the Iris dataset as variables. First, take a look at the dataset.

Code:

head(iris)

Result:

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

The dataset has 4 main features: the length and width of the sepal and the length and width of the petal. In the examples below, we will carry out the correlation test with two features: Petal.Length and Petal.Width.

Measure the correlation coefficient by Pearson formula

Code:

with(iris, cor.test(Petal.Length, Petal.Width, method = 'pearson'))

Result:

	Pearson's product-moment correlation

data: Petal.Length and Petal.Width
t = 43.387, df = 148, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.9490525 0.9729853
sample estimates:
      cor 
0.9628654

The result shows that the test statistic t = 43.487, the degree of free df is 148, and the hypothesis testing p-value is 2.2e-16. The test also tells that the alternative hypothesis: true correlation is not equal to 0, and the correlation coefficient cor is 0.9628.

Measure the correlation coefficient by Kendall formula

Code:

with(iris, cor.test(Petal.Length, Petal.Width, method = 'kendall'))

Result:

Kendall's rank correlation tau

data: Petal.Length and Petal.Width
z = 13.968, p-value < 2.2e-16
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.8068907

When testing the samples with the Kendall formula, there is no confidence interval.

Measure the correlation coefficient by Spearman formula

Code:

with(iris, cor.test(Petal.Length, Petal.Width, method = 'spearman', exact = FALSE))

Result:

	Spearman's rank correlation rho

data: Petal.Length and Petal.Width
S = 35061, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.9376668

Also, the test shows no confidence interval when testing with the Spearman formula.

Summary

In summary, the cor.test() function analyzes the associations of two variables based on the Pearson, Kendall, and Spearman formulas. In statistics, the function helps filter special features to get a significant result.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *