Calculate Spearman correlation coefficient and its confidence interval. In
addition to the base R function [cor](x, method="spearman"),
frequency tables are
also accepted as arguments (i.e. actually weights are used).
Usage
spearmanCor(
x,
y = NULL,
conf.level = NA,
sides = c("two.sided", "left", "right"),
na.rm = FALSE
)Arguments
- x
either a contingency table, a two-column object (matrix, data.frame or list), or a vector of observations (together with
y)- y
optional second vector. If
xis not a vector,ymust beNULL.- conf.level
confidence level of the interval. If set to
NA(the default), only the point estimate is returned.- sides
character string specifying the sidedness of the confidence interval (one of
"two.sided"(default),"left"or"right"). SeeConfidenceIntervals().- na.rm
logical; whether to remove incomplete pairs. Applies to the vector interface; a frequency table must not contain missing counts.
Value
if conf.level = NA, a numeric scalar. Otherwise a named
numeric vector with elements:
estpoint estimate of Spearman's rank correlation
lcilower confidence interval bound
uciupper confidence interval bound
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. Both routes use midranks for ties and agree exactly;
see the examples.
The confidence intervals are calculated via
z-Transformation.
The number of observations entering the z-transformation is
length(x) in the vector interface and sum(x), the table total,
in the table interface. At least four observations are needed for an
interval.
sides names the side of the interval that carries the finite bound,
so "left" returns [lci, 1] and "right" returns
[-1, uci]. Since rho is bounded, the open side is reported at the
range boundary rather than as infinite.
See also
Other assoc.continuous:
corPart(),
corPolychor(),
findCorrX(),
hoeffdingD(),
keepSig(),
pearsonCor()
Examples
# Example from SAS documentation (PROC FREQ)
pain <- as.table(matrix(c(26, 6, 26, 7, 23,
9, 18, 14, 9, 23),
ncol=5,
dimnames=list(adverse=c("no", "yes"), dose=1:5)))
spearmanCor(pain)
#> [1] 0.3770609
spearmanCor(pain, conf.level=0.95)
#> est lci uci
#> 0.3770609 0.2361593 0.5024329
# must be the same as
with(lapply(
bedrock::untable(pain,
colnames = c("adverse","dose")),
ordered),
spearmanCor(adverse, dose, conf.level=0.95))
#> est lci uci
#> 0.3770609 0.2361593 0.5024329
