Calculate Spearman correlation coefficient and its confidence interval. In addition to the base R function cor(), frequency tables are also accepted as arguments (i.e. actually weights are used).

SpearmanRho(x, y = NULL, use = c("everything", "all.obs", "complete.obs", 
            "na.or.complete","pairwise.complete.obs"), 
            conf.level = NA)

Arguments

x

a numeric vector, an ordered factor, matrix or data frame. An ordered factor will be coerced to numeric.

y

NULL (default) or a vector, an ordered factor, matrix or data frame with compatible dimensions to x. An ordered factor will be coerced to numeric.

use

an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".

conf.level

confidence level of the interval. If set to NA (which is the default) no confidence interval will be calculated.

Details

The function calculates Spearman's rho statistic by means of cor(..., method="spearman") when two variables x and y are supplied. If a frequency table is provided an implementation based on SAS documentation is used.
The confidence intervals are calculated via z-Transformation.

Value

Either a single numeric value, if no confidence interval is required,

or a vector with 3 elements for estimate, lower and upper confidence intervall.

References

Conover W. J. (1999) Practical Nonparametric Statistics (3rd edition). Wiley

Author

Andri Signorell <andri@signorell.net>

See also

Examples

pain <- as.table(matrix(c(26,  6, 26, 7, 23, 
                           9, 18, 14, 9, 23), 
                           ncol=5, byrow=TRUE, 
        dimnames=list(adverse=c("no", "yes"), dose=1:5)))

SpearmanRho(pain)
#> [1] 0.10208

SpearmanRho(pain, conf.level=0.95)
#>         rho      lwr.ci      upr.ci 
#>  0.10208002 -0.05343858  0.25276393 
  
# must be the same as
with(Untable(pain), 
     SpearmanRho(adverse, dose, conf.level=0.95))
#>         rho      lwr.ci      upr.ci 
#>  0.10208002 -0.05343858  0.25276393