Matrix of Correlations and Generalized Spearman Rank Correlation
rcorr Computes a matrix of Pearson's
r or Spearman's
rho rank correlation coefficients for all possible pairs of
columns of a matrix. Missing values are deleted in pairs rather than
deleting all rows of
x having any missing variables. Ranks are
computed using efficient algorithms (see reference 2), using midranks
spearman2 computes the square of Spearman's rho rank correlation
and a generalization of it in which
x can relate
y. This is done by computing the Spearman
multiple rho-squared between
(rank(x), rank(x)^2) and
x is categorical, a different kind of Spearman correlation
used in the Kruskal-Wallis test is computed (and
spearman2 can do
the Kruskal-Wallis test). This is done by computing the ordinary
k-1 dummy variables and
also be a formula, in which case each predictor is correlated separately
y, using non-missing observations for that predictor.
plot methods allow one to easily print or plot
the results of
spearman2(formula). The adjusted
also computed, using the same formula used for the ordinary adjusted
F test uses the unadjusted R2. For
a dot chart is drawn which by default shows, in sorted order, the
spearman computes Spearman's rho on non-missing values of two
spearman.test is a simple version of
rcorr(x, y, type=c("pearson","spearman"))
## S3 method for class 'rcorr': print(x, ...)
## S3 method for class 'default': spearman2(x, y, p=1, minlev=0, exclude.imputed=TRUE, ...)
## S3 method for class 'formula': spearman2(x, p=1, data, subset, na.action, minlev=0, exclude.imputed=TRUE, ...)
## S3 method for class 'spearman2.formula': print(x, ...)
## S3 method for class 'spearman2.formula': plot(x, what=c('Adjusted rho2','rho2','P'), sort.=TRUE, main, xlab, ...)
spearman.test(x, y, p=1)
- a numeric matrix with at least 5 rows and at least 2 columns (if
yis absent). For
spearman2, the first argument may be a vector of any type, including character or factor. The first argument may also be a formula, in which cas
- specifies the type of correlations to compute. Spearman correlations are the Pearson linear correlations computed on the ranks of non-missing elements, using midranks for ties.
- a numeric vector or matrix which will be concatenated to
yis omitted for
xmust be a matrix.
- for numeric variables, specifies the order of the Spearman
rho^2to use. The default is
p=1to compute the ordinary
p=2to compute the quadratic rank generalization to allow non-monotonicity
- data, subset, na.action
- the usual options for models. Default for
na.actionis to retain all values, NA or not, so that NAs can be deleted in only a pairwise fashion.
- minimum relative frequency that a level of a categorical predictor
should have before it is pooled with other categories (see
spearman2. The default,
minlev=0causes no pooling.
- set to
FALSEto include imputed values (created by
impute) in the calculations.
- specifies which statistic to plot
sort.=FALSEto suppress sorting variables by the statistic being plotted
- main title for plot. Default title shows the name of the response variable.
- x-axis label. Default constructed from
- other arguments that are passed to
Uses midranks in case of ties, as described by Hollander and Wolfe.
P-values are approximated by using the
rcorrreturns a list with elements
r, the matrix of correlations,
nthe matrix of number of observations used in analyzing each pair of variables, and
P, the asymptotic P-values. Pairs with fewer than 2 non-missing values have the r values set to NA. The diagonals of
nare the number of non-NAs for the single variable corresponding to that row and column.
spearman2.default(the function that is called for a single
x, i.e., when there is no formula) returns a vector of statistics for the variable.
spearman2.formulareturns a matrix with rows corresponding to predictors.
Hollander M. and Wolfe D.A. (1973). Nonparametric Statistical Methods. New York: Wiley.
Press WH, Flannery BP, Teukolsky SA, Vetterling, WT (1988): Numerical Recipes in C. Cambridge: Cambridge University Press.
x <- c(-2, -1, 0, 1, 2) y <- c(4, 1, 0, 1, 4) z <- c(1, 2, 3, 4, NA) v <- c(1, 2, 3, 4, 5) rcorr(cbind(x,y,z,v)) spearman2(x, y) plot(spearman2(z ~ x + y + v, p=2))