Computes (pairwise, weighted) Pearsons correlations, covariances and observation counts. Pairwise correlations and covariances can be computed together with observation counts and p-values, and output as 3D array (default) or list of matrices. pwcor
and pwcov
offer an elaborate print method.
Notes: weights::wtd.cors
is imported for weighted pairwise correlations (written in C for speed). For weighted correlations with bootstrap SE's see weights::wtd.cor
(but bootstrap can be slow). Weighted correlations for complex surveys are implemented in jtools::svycor
. An equivalent and faster implementation of pwcor
(without weights) is provided in Hmisc::rcorr
(written in Fortran)
pwcor(X, …, w = NULL, N = FALSE, P = FALSE, array = TRUE, use = "pairwise.complete.obs")pwcov(X, …, w = NULL, N = FALSE, P = FALSE, array = TRUE, use = "pairwise.complete.obs")
pwnobs(X)
# S3 method for pwcor
print(x, digits = 2L, sig.level = 0.05, show = c("all","lower.tri","upper.tri"),
spacing = 1L, …)
# S3 method for pwcov
print(x, digits = 2L, sig.level = 0.05, show = c("all","lower.tri","upper.tri"),
spacing = 1L, …)
a matrix or data.frame, for pwcor
and pwcov
all columns must be numeric. All functions are faster on matrices, so converting is advised for large data (see qM
).
an object of class 'pwcor' / 'pwcov'.
numeric. A vector of (frequency) weights.
logical. TRUE
also computes pairwise observation counts.
logical. TRUE
also computes pairwise p-values (same as cor.test
and Hmisc::rcorr
).
logical. If N = TRUE
or P = TRUE
, TRUE
(default) returns output as 3D array whereas FALSE
returns a list of matrices.
integer. The number of digits to round to in print.
numeric. P-value threshold below which a '*'
is displayed above significant coefficients if P = TRUE
.
character. The part of the correlation / covariance matrix to display.
integer. Controls the spacing between different reported quantities in the printout of the matrix: 0 - compressed, 1 - single space, 2 - double space.
a numeric matrix, 3D array or list of matrices with the computed statistics. For pwcor
and pwcov
the object has a class 'pwcor' and 'pwcov', respectively.
# NOT RUN {
mna <- na_insert(mtcars)
pwcor(mna)
pwcov(mna)
pwnobs(mna)
pwcor(mna, N = TRUE)
pwcor(mna, P = TRUE)
pwcor(mna, N = TRUE, P = TRUE)
aperm(pwcor(mna, N = TRUE, P = TRUE))
print(pwcor(mna, N = TRUE, P = TRUE), digits = 3, sig.level = 0.01, show = "lower.tri")
pwcor(mna, N = TRUE, P = TRUE, array = FALSE)
print(pwcor(mna, N = TRUE, P = TRUE, array = FALSE), show = "lower.tri")
# }
Run the code above in your browser using DataLab