
(Pairwise, Weighted) Correlations, Covariances and Observation Counts
pwcor_pwcov_pwnobs.Rd
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.
Usage
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 = .op[["digits"]], sig.level = 0.05,
show = c("all","lower.tri","upper.tri"), spacing = 1L, return = FALSE, ...)
# S3 method for pwcov
print(x, digits = .op[["digits"]], sig.level = 0.05,
show = c("all","lower.tri","upper.tri"), spacing = 1L, return = FALSE, ...)
Arguments
- X
a matrix or data.frame, for
pwcor
andpwcov
all columns must be numeric. All functions are faster on matrices, so converting is advised for large data (seeqM
).- x
an object of class 'pwcor' / 'pwcov'.
- w
numeric. A vector of (frequency) weights.
- N
logical.
TRUE
also computes pairwise observation counts.- P
logical.
TRUE
also computes pairwise p-values (same ascor.test
andHmisc::rcorr
).- array
logical. If
N = TRUE
orP = TRUE
,TRUE
(default) returns output as 3D array whereasFALSE
returns a list of matrices.- use
argument passed to
cor
/cov
. Ifuse != "pairwise.complete.obs"
,sum(complete.cases(X))
is used forN
, and p-values are computed accordingly.- digits
integer. The number of digits to round to in print.
- sig.level
numeric. P-value threshold below which a
'*'
is displayed above significant coefficients ifP = TRUE
.- show
character. The part of the correlation / covariance matrix to display.
- spacing
integer. Controls the spacing between different reported quantities in the printout of the matrix: 0 - compressed, 1 - single space, 2 - double space.
- return
logical.
TRUE
returns the formatted object from the print method for exporting. The default is to returnx
invisibly.- ...
other arguments passed to
cor
orcov
. Only sensible ifP = FALSE
.
Value
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.
Note
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
(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).
Examples
mna <- na_insert(mtcars)
pwcor(mna)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 1 -.86 -.85 -.81 .62 -.89 .28 .65 .60 .42 -.49
#> cyl -.86 1 .90 .88 -.64 .76 -.55 -.80 -.55 -.41 .53
#> disp -.85 .90 1 .86 -.66 .91 -.36 -.71 -.66 -.49 .43
#> hp -.81 .88 .86 1 -.45 .71 -.61 -.80 -.56 -.25 .70
#> drat .62 -.64 -.66 -.45 1 -.65 .13 .36 .64 .69 -.03
#> wt -.89 .76 .91 .71 -.65 1 -.14 -.57 -.68 -.57 .49
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcov(mna)
#> mpg cyl disp hp drat wt qsec
#> mpg 32.57 -8.19 -616.41 -275.06 1.83 -4.62 2.61
#> cyl -8.19 3.24 199.16 102.50 -0.61 1.40 -1.72
#> disp -616.41 199.16 15941.09 7291.10 -43.63 121.26 -66.89
#> hp -275.06 102.50 7291.10 3882.88 -15.55 46.47 -60.02
#> drat 1.83 -0.61 -43.63 -15.55 0.28 -0.28 0.13
#> wt -4.62 1.40 121.26 46.47 -0.28 0.96 -0.24
#> vs am gear carb
#> mpg 2.00 1.69 1.79 -3.59
#> cyl -0.72 -0.47 -0.56 1.26
#> disp -44.75 -41.35 -46.04 71.32
#> hp -26.28 -16.18 -10.81 59.49
#> drat 0.10 0.16 0.28 -0.02
#> wt -0.29 -0.33 -0.40 0.62
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwnobs(mna)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 26 26 26 26 26 26 26 26 27 26
#> cyl 26 29 28 26 26 26 28 27 26 26 27
#> disp 26 28 29 26 26 26 27 27 26 26 27
#> hp 26 26 26 29 26 26 26 26 26 26 27
#> drat 26 26 26 26 29 26 26 26 26 27 27
#> wt 26 26 26 26 26 29 26 26 26 26 26
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE)
#> mpg cyl disp hp drat wt qsec
#> mpg 1 (29) -.86 (26) -.85 (26) -.81 (26) .62 (26) -.89 (26) .28 (26)
#> cyl -.86 (26) 1 (29) .90 (28) .88 (26) -.64 (26) .76 (26) -.55 (28)
#> disp -.85 (26) .90 (28) 1 (29) .86 (26) -.66 (26) .91 (26) -.36 (27)
#> hp -.81 (26) .88 (26) .86 (26) 1 (29) -.45 (26) .71 (26) -.61 (26)
#> drat .62 (26) -.64 (26) -.66 (26) -.45 (26) 1 (29) -.65 (26) .13 (26)
#> wt -.89 (26) .76 (26) .91 (26) .71 (26) -.65 (26) 1 (29) -.14 (26)
#> vs am gear carb
#> mpg .65 (26) .60 (26) .42 (27) -.49 (26)
#> cyl -.80 (27) -.55 (26) -.41 (26) .53 (27)
#> disp -.71 (27) -.66 (26) -.49 (26) .43 (27)
#> hp -.80 (26) -.56 (26) -.25 (26) .70 (27)
#> drat .36 (26) .64 (26) .69 (27) -.03 (27)
#> wt -.57 (26) -.68 (26) -.57 (26) .49 (26)
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, P = TRUE)
#> mpg cyl disp hp drat wt qsec vs am gear
#> mpg 1 -.86* -.85* -.81* .62* -.89* .28 .65* .60* .42*
#> cyl -.86* 1 .90* .88* -.64* .76* -.55* -.80* -.55* -.41*
#> disp -.85* .90* 1 .86* -.66* .91* -.36 -.71* -.66* -.49*
#> hp -.81* .88* .86* 1 -.45* .71* -.61* -.80* -.56* -.25
#> drat .62* -.64* -.66* -.45* 1 -.65* .13 .36 .64* .69*
#> wt -.89* .76* .91* .71* -.65* 1 -.14 -.57* -.68* -.57*
#> carb
#> mpg -.49*
#> cyl .53*
#> disp .43*
#> hp .70*
#> drat -.03
#> wt .49*
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE, P = TRUE)
#> mpg cyl disp hp drat wt
#> mpg 1 (29) -.86* (26) -.85* (26) -.81* (26) .62* (26) -.89* (26)
#> cyl -.86* (26) 1 (29) .90* (28) .88* (26) -.64* (26) .76* (26)
#> disp -.85* (26) .90* (28) 1 (29) .86* (26) -.66* (26) .91* (26)
#> hp -.81* (26) .88* (26) .86* (26) 1 (29) -.45* (26) .71* (26)
#> drat .62* (26) -.64* (26) -.66* (26) -.45* (26) 1 (29) -.65* (26)
#> wt -.89* (26) .76* (26) .91* (26) .71* (26) -.65* (26) 1 (29)
#> qsec vs am gear carb
#> mpg .28 (26) .65* (26) .60* (26) .42* (27) -.49* (26)
#> cyl -.55* (28) -.80* (27) -.55* (26) -.41* (26) .53* (27)
#> disp -.36 (27) -.71* (27) -.66* (26) -.49* (26) .43* (27)
#> hp -.61* (26) -.80* (26) -.56* (26) -.25 (26) .70* (27)
#> drat .13 (26) .36 (26) .64* (26) .69* (27) -.03 (27)
#> wt -.14 (26) -.57* (26) -.68* (26) -.57* (26) .49* (26)
#> [ reached getOption("max.print") -- omitted 5 rows ]
aperm(pwcor(mna, N = TRUE, P = TRUE))
#> , , mpg
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r 1 -.86 -.85 -.81 .62 -.89 .28 .65 .60 .42 -.49
#> N 29 26 26 26 26 26 26 26 26 27 26
#> P .00 .00 .00 .00 .00 .16 .00 .00 .03 .01
#>
#> , , cyl
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.86 1 .90 .88 -.64 .76 -.55 -.80 -.55 -.41 .53
#> N 26 29 28 26 26 26 28 27 26 26 27
#> P .00 .00 .00 .00 .00 .00 .00 .00 .04 .00
#>
#> [ reached getOption("max.print") -- omitted 9 matrix slice(s) ]
print(pwcor(mna, N = TRUE, P = TRUE), digits = 3, sig.level = 0.01, show = "lower.tri")
#> mpg cyl disp hp drat wt
#> mpg 1 (29)
#> cyl -.862* (26) 1 (29)
#> disp -.853* (26) .897* (28) 1 (29)
#> hp -.811* (26) .882* (26) .858* (26) 1 (29)
#> drat .616* (26) -.637* (26) -.661* (26) -.453 (26) 1 (29)
#> wt -.887* (26) .764* (26) .908* (26) .714* (26) -.650* (26) 1 (29)
#> qsec vs am gear carb
#> mpg
#> cyl
#> disp
#> hp
#> drat
#> wt
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE, P = TRUE, array = FALSE)
#> $r
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 1 -.86 -.85 -.81 .62 -.89 .28 .65 .60 .42 -.49
#> cyl -.86 1 .90 .88 -.64 .76 -.55 -.80 -.55 -.41 .53
#> disp -.85 .90 1.00 .86 -.66 .91 -.36 -.71 -.66 -.49 .43
#> hp -.81 .88 .86 1 -.45 .71 -.61 -.80 -.56 -.25 .70
#> drat .62 -.64 -.66 -.45 1 -.65 .13 .36 .64 .69 -.03
#> wt -.89 .76 .91 .71 -.65 1.00 -.14 -.57 -.68 -.57 .49
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 26 26 26 26 26 26 26 26 27 26
#> cyl 26 29 28 26 26 26 28 27 26 26 27
#> disp 26 28 29 26 26 26 27 27 26 26 27
#> hp 26 26 26 29 26 26 26 26 26 26 27
#> drat 26 26 26 26 29 26 26 26 26 27 27
#> wt 26 26 26 26 26 29 26 26 26 26 26
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $P
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg .00 .00 .00 .00 .00 .16 .00 .00 .03 .01
#> cyl .00 .00 .00 .00 .00 .00 .00 .00 .04 .00
#> disp .00 .00 .00 .00 .00 .06 .00 .00 .01 .02
#> hp .00 .00 .00 .02 .00 .00 .00 .00 .21 .00
#> drat .00 .00 .00 .02 .00 .52 .07 .00 .00 .87
#> wt .00 .00 .00 .00 .00 .50 .00 .00 .00 .01
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
print(pwcor(mna, N = TRUE, P = TRUE, array = FALSE), show = "lower.tri")
#> $r
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 1
#> cyl -.86 1
#> disp -.85 .90 1.00
#> hp -.81 .88 .86 1
#> drat .62 -.64 -.66 -.45 1
#> wt -.89 .76 .91 .71 -.65 1.00
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29
#> cyl 26 29
#> disp 26 28 29
#> hp 26 26 26 29
#> drat 26 26 26 26 29
#> wt 26 26 26 26 26 29
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $P
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg
#> cyl .00
#> disp .00 .00
#> hp .00 .00 .00
#> drat .00 .00 .00 .02
#> wt .00 .00 .00 .00 .00
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>