cor.test
Test for Correlation Between Paired Samples

### Description

Tests whether two vectors (paired samples) are uncorrelated using Pearson's product moment correlation coefficient, Kendall's tau-statistic, or Spearman's rank correlation.

### Usage

    cor.test(x, ...)
    ## Default S3 method:
    cor.test(x, y, alternative = c("two.sided", "less", "greater"),
        method = c("pearson", "kendall", "spearman"), exact = NULL,
        conf.level = 0.95, continuity = FALSE, ...)
    ## S3 method for class 'formula':
    cor.test(formula, data, subset, na.action, ...)

### Arguments

x, y | numeric vectors of the same length that contain the sample values. The length of the vectors must be greater than two for the "pearson" method and greater than one for the "kendall" and "spearman" methods. Missing (NAs) and infinite values (Inf, -Infs) are ignored.
alternative | a character string that specifies the alternative hypothesis for the test of correlation between x and y. Acceptable values are:
"two.sided" | non-zero
"greater" | greater than zero
"less" | less than zero
Only enough of the string to be unique is required.
method | a character string that specifies the correlation coefficient that is used in the test statistic. Acceptable values are:
"pearson" |
"kendall" |
"spearman" |
Only enough of the string to be unique is required.
exact | a logical flag to indicate if an exact p-value should be computed. It is used only with Kendall's tau-statistic or Spearman's rank correlation, and only when there are no tie cases. If exact=NULL (the default), an exact test is used if the observation size is less than 50 for Kendall's tau-statistic or if less than 1291 observations for Spearman's rank correlation. Otherwise an approximation method is used.
conf.level | a numeric value in the range [0, 1] that specifies the confidence level for the returned confidence interval. conf.level is used only in Pearson's product moment correlation coefficient and when the number of observations is greater than 3.
continuity | a logical scalar. If TRUE, a continuity correction is used for Kendall's tau-statistic or Spearman's rho-statistic when an exact p-value is not computed. Default value is FALSE.
formula | a formula to specify which variables to use in the computations. The formula follows the pattern ~ a + b to compute and test the correlation of a and b.
data | a data frame or a matrix that contains the objects named in formula. By default, this argument reads in values from the environment.
subset | a vector that specifies a subset from the data frame (data) to use in formula.
na.action | a character string that specifies how missing values (NAs) are handled. By default, an error is returned.
... | any other arguments that are appropriate for the particular call.

### Details

- When method="pearson" the (usual) Pearson's product moment correlation coefficient (r <- cor(x,y)) is computed, and divided by its standard error to produce a t-statistic with n-2 degrees of freedom, where n = length(x) = length(y). This statistic is given by t <- (sqrt(n-2)*r) / sqrt(1-r^2).

- When method="kendall" an exact test is computed if exact=TRUE and there are no ties. Otherwise, a normal approximation based on the value of tau is computed.

- When method="spearman" an exact test is computed if exact=TRUE and there are no ties. Otherwise a t-distribution approximation based on the value of the Spearman rank correlation is computed.

Value
returns a list of class "htest", containing the following components:

statistic | the value of the test statistic, a t-statistic, or a normalized z-statistic with a names attribute.
parameter | the degrees of freedom of the null distribution that is associated with statistic when this is a t-distribution.
p.value | the p-value under the null hypothesis that the correlation between x and y is zero.
estimate | the correlation coefficient with one of the following names attributes. "tau" for Kendall's statistic "cor" for Pearson's statistic "rho" for Spearman's statistic
null.value | the hypothesized value for the correlation between x and y, always 0 with anames attribute which is one of the following. "tau" for Kendall's statistic "correlation" for Pearson's statistic "rho" for Spearman's statistic
alternative | character string that returns the alternative hypothesis ("two.sided", "greater", or "less") as specified in the alternative argument.
method | a string containing the name of the estimator used for the correlation coefficient. One of the following. "Pearson's product-moment correlation" "Kendall's rank correlation tau" "Spearman's rank correlation"
data.name | a character string containing the actual names of the x and y vectors.
conf.int | confidence intervals for the correlation between x and y. Currently, it is only computed for the Pearson's correlation coefficient and only when the number of observations is greater than 3. The confidence level is recorded in the attribute conf.level.

Null hypothesis
x and y are uncorrelated.
Test assumptions
When method="pearson", the data are assumed to come from a bivariate Normal distribution. If this is not the case, the other two methods offer nonparametric alternatives.
Differences between Spotfire Enterprise Runtime for R and Open-source R
When method="kendall", exact=TRUE, and length(x)>8, the exact p-values computed in Spotfire Enterprise Runtime for R differ from open-source R by a small amount (usually less than 1e-3). This may be due to a different algorithm (that is, AS 71) is used in Spotfire Enterprise Runtime for R.
References
Best, D. J. and Roberts, D. E. 1975. Algorithm AS 89: The Upper Tail Probabilities of Spearman's rho. Applied Statistics. Volume 24. 377-379.
Best, D. J. and Gipps, P. G. 1974. Algorithm AS 71: The Upper Tail Probabilities of Kendall's tau. Applied Statistics. Volume 23. 98-100.
Hollander, M. and Wolfe, D. A. 1973. Nonparametric Statistical Methods. New York, NY: John Wiley & Sons. 185-194 (Kendall and Spearman tests).
See Also
cor.
Examples

    murder <- Sdatasets::state.x77[, "Murder"]
    illit <- Sdatasets::state.x77[, "Illiteracy"]
    cor.test(murder, illit, method="k")
    x <- rnorm(20, 7, 0.5)
    y <- rnorm(20, 13, 0.7)
    cor.test(x, y)
    cor.test(x, y, conf.level=0.9)
    cor.test(x, y, method="spearman", alternative="less")
    cor.test(x, y, method="kendall", exact=FALSE,
        alternative="greater", continuity=TRUE)
    # Transformations can be used
    cor.test(log(x), log(y), alt="gr")
    # With a formula
    cor.test(~ Fuel + Weight, data=Sdatasets::fuel.frame, subset=(Disp. > 152))

Package stats version 6.1.5-18
Package Index