
(Pairwise, Weighted) Correlations, Covariances and Observation Counts
pwcor_pwcov_pwnobs.RdComputes (pairwise, weighted) Pearson's 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 class 'pwcor'
print(x, digits = .op[["digits"]], sig.level = 0.05,
show = c("all","lower.tri","upper.tri"), spacing = 1L, return = FALSE, ...)
# S3 method for class '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
pwcorandpwcovall 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.
TRUEalso computes pairwise observation counts.- P
logical.
TRUEalso computes pairwise p-values (same ascor.testandHmisc::rcorr).- array
logical. If
N = TRUEorP = TRUE,TRUE(default) returns output as 3D array whereasFALSEreturns 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.
TRUEreturns the formatted object from the print method for exporting. The default is to returnxinvisibly.- ...
other arguments passed to
cororcov. 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 -.89 -.79 .70 -.85 .44 .64 .64 .44 -.53
#> cyl -.86 1 .91 .86 -.74 .76 -.66 -.90 -.54 -.46 .55
#> disp -.89 .91 1 .87 -.71 .90 -.46 -.71 -.67 -.56 .48
#> hp -.79 .86 .87 1 -.45 .72 -.63 -.74 -.41 -.30 .68
#> drat .70 -.74 -.71 -.45 1 -.68 .21 .46 .73 .70 -.14
#> wt -.85 .76 .90 .72 -.68 1 -.17 -.48 -.65 -.55 .41
#> qsec .44 -.66 -.46 -.63 .21 -.17 1 .76 -.18 -.09 -.75
#> vs .64 -.90 -.71 -.74 .46 -.48 .76 1 .10 .19 -.62
#> am .64 -.54 -.67 -.41 .73 -.65 -.18 .10 1 .84 .09
#> gear .44 -.46 -.56 -.30 .70 -.55 -.09 .19 .84 1 .36
#> carb -.53 .55 .48 .68 -.14 .41 -.75 -.62 .09 .36 1
pwcov(mna)
#> mpg cyl disp hp drat wt qsec
#> mpg 37.97 -9.85 -722.44 -290.58 2.45 -5.06 4.18
#> cyl -9.85 3.35 223.15 95.17 -0.73 1.38 -2.16
#> disp -722.44 223.15 16081.91 6990.91 -54.08 115.42 -99.06
#> hp -290.58 95.17 6990.91 3591.78 -14.12 42.03 -62.02
#> drat 2.45 -0.73 -54.08 -14.12 0.30 -0.34 0.18
#> wt -5.06 1.38 115.42 42.03 -0.34 0.94 -0.29
#> qsec 4.18 -2.16 -99.06 -62.02 0.18 -0.29 3.12
#> vs 2.01 -0.82 -44.86 -22.67 0.13 -0.24 0.71
#> am 2.05 -0.49 -43.46 -11.79 0.21 -0.32 -0.16
#> gear 2.00 -0.61 -49.62 -12.44 0.25 -0.40 -0.12
#> carb -5.62 1.71 95.33 56.20 -0.13 0.67 -2.04
#> vs am gear carb
#> mpg 2.01 2.05 2.00 -5.62
#> cyl -0.82 -0.49 -0.61 1.71
#> disp -44.86 -43.46 -49.62 95.33
#> hp -22.67 -11.79 -12.44 56.20
#> drat 0.13 0.21 0.25 -0.13
#> wt -0.24 -0.32 -0.40 0.67
#> qsec 0.71 -0.16 -0.12 -2.04
#> vs 0.26 0.03 0.07 -0.54
#> am 0.03 0.25 0.31 0.08
#> gear 0.07 0.31 0.53 0.46
#> carb -0.54 0.08 0.46 2.79
pwnobs(mna)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 26 26 26 27 26 26 28 27 26 27
#> cyl 26 29 26 27 26 26 26 26 26 28 26
#> disp 26 26 29 26 26 26 26 26 26 26 26
#> hp 26 27 26 29 26 27 26 26 26 28 26
#> drat 27 26 26 26 29 26 26 26 26 26 27
#> wt 26 26 26 27 26 29 26 26 26 27 26
#> qsec 26 26 26 26 26 26 29 26 27 26 26
#> vs 28 26 26 26 26 26 26 29 27 26 26
#> am 27 26 26 26 26 26 27 27 29 26 26
#> gear 26 28 26 28 26 27 26 26 26 29 26
#> carb 27 26 26 26 27 26 26 26 26 26 29
pwcor(mna, N = TRUE)
#> mpg cyl disp hp drat wt qsec
#> mpg 1 (29) -.86 (26) -.89 (26) -.79 (26) .70 (27) -.85 (26) .44 (26)
#> cyl -.86 (26) 1 (29) .91 (26) .86 (27) -.74 (26) .76 (26) -.66 (26)
#> disp -.89 (26) .91 (26) 1 (29) .87 (26) -.71 (26) .90 (26) -.46 (26)
#> hp -.79 (26) .86 (27) .87 (26) 1 (29) -.45 (26) .72 (27) -.63 (26)
#> drat .70 (27) -.74 (26) -.71 (26) -.45 (26) 1 (29) -.68 (26) .21 (26)
#> wt -.85 (26) .76 (26) .90 (26) .72 (27) -.68 (26) 1 (29) -.17 (26)
#> qsec .44 (26) -.66 (26) -.46 (26) -.63 (26) .21 (26) -.17 (26) 1 (29)
#> vs .64 (28) -.90 (26) -.71 (26) -.74 (26) .46 (26) -.48 (26) .76 (26)
#> am .64 (27) -.54 (26) -.67 (26) -.41 (26) .73 (26) -.65 (26) -.18 (27)
#> gear .44 (26) -.46 (28) -.56 (26) -.30 (28) .70 (26) -.55 (27) -.09 (26)
#> carb -.53 (27) .55 (26) .48 (26) .68 (26) -.14 (27) .41 (26) -.75 (26)
#> vs am gear carb
#> mpg .64 (28) .64 (27) .44 (26) -.53 (27)
#> cyl -.90 (26) -.54 (26) -.46 (28) .55 (26)
#> disp -.71 (26) -.67 (26) -.56 (26) .48 (26)
#> hp -.74 (26) -.41 (26) -.30 (28) .68 (26)
#> drat .46 (26) .73 (26) .70 (26) -.14 (27)
#> wt -.48 (26) -.65 (26) -.55 (27) .41 (26)
#> qsec .76 (26) -.18 (27) -.09 (26) -.75 (26)
#> vs 1 (29) .10 (27) .19 (26) -.62 (26)
#> am .10 (27) 1 (29) .84 (26) .09 (26)
#> gear .19 (26) .84 (26) 1 (29) .36 (26)
#> carb -.62 (26) .09 (26) .36 (26) 1 (29)
pwcor(mna, P = TRUE)
#> mpg cyl disp hp drat wt qsec vs am gear
#> mpg 1 -.86* -.89* -.79* .70* -.85* .44* .64* .64* .44*
#> cyl -.86* 1 .91* .86* -.74* .76* -.66* -.90* -.54* -.46*
#> disp -.89* .91* 1 .87* -.71* .90* -.46* -.71* -.67* -.56*
#> hp -.79* .86* .87* 1 -.45* .72* -.63* -.74* -.41* -.30
#> drat .70* -.74* -.71* -.45* 1 -.68* .21 .46* .73* .70*
#> wt -.85* .76* .90* .72* -.68* 1 -.17 -.48* -.65* -.55*
#> qsec .44* -.66* -.46* -.63* .21 -.17 1 .76* -.18 -.09
#> vs .64* -.90* -.71* -.74* .46* -.48* .76* 1 .10 .19
#> am .64* -.54* -.67* -.41* .73* -.65* -.18 .10 1 .84*
#> gear .44* -.46* -.56* -.30 .70* -.55* -.09 .19 .84* 1
#> carb -.53* .55* .48* .68* -.14 .41* -.75* -.62* .09 .36
#> carb
#> mpg -.53*
#> cyl .55*
#> disp .48*
#> hp .68*
#> drat -.14
#> wt .41*
#> qsec -.75*
#> vs -.62*
#> am .09
#> gear .36
#> carb 1
pwcor(mna, N = TRUE, P = TRUE)
#> mpg cyl disp hp drat wt
#> mpg 1 (29) -.86* (26) -.89* (26) -.79* (26) .70* (27) -.85* (26)
#> cyl -.86* (26) 1 (29) .91* (26) .86* (27) -.74* (26) .76* (26)
#> disp -.89* (26) .91* (26) 1 (29) .87* (26) -.71* (26) .90* (26)
#> hp -.79* (26) .86* (27) .87* (26) 1 (29) -.45* (26) .72* (27)
#> drat .70* (27) -.74* (26) -.71* (26) -.45* (26) 1 (29) -.68* (26)
#> wt -.85* (26) .76* (26) .90* (26) .72* (27) -.68* (26) 1 (29)
#> qsec .44* (26) -.66* (26) -.46* (26) -.63* (26) .21 (26) -.17 (26)
#> vs .64* (28) -.90* (26) -.71* (26) -.74* (26) .46* (26) -.48* (26)
#> am .64* (27) -.54* (26) -.67* (26) -.41* (26) .73* (26) -.65* (26)
#> gear .44* (26) -.46* (28) -.56* (26) -.30 (28) .70* (26) -.55* (27)
#> carb -.53* (27) .55* (26) .48* (26) .68* (26) -.14 (27) .41* (26)
#> qsec vs am gear carb
#> mpg .44* (26) .64* (28) .64* (27) .44* (26) -.53* (27)
#> cyl -.66* (26) -.90* (26) -.54* (26) -.46* (28) .55* (26)
#> disp -.46* (26) -.71* (26) -.67* (26) -.56* (26) .48* (26)
#> hp -.63* (26) -.74* (26) -.41* (26) -.30 (28) .68* (26)
#> drat .21 (26) .46* (26) .73* (26) .70* (26) -.14 (27)
#> wt -.17 (26) -.48* (26) -.65* (26) -.55* (27) .41* (26)
#> qsec 1 (29) .76* (26) -.18 (27) -.09 (26) -.75* (26)
#> vs .76* (26) 1 (29) .10 (27) .19 (26) -.62* (26)
#> am -.18 (27) .10 (27) 1 (29) .84* (26) .09 (26)
#> gear -.09 (26) .19 (26) .84* (26) 1 (29) .36 (26)
#> carb -.75* (26) -.62* (26) .09 (26) .36 (26) 1 (29)
aperm(pwcor(mna, N = TRUE, P = TRUE))
#> , , mpg
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r 1 -.86 -.89 -.79 .70 -.85 .44 .64 .64 .44 -.53
#> N 29 26 26 26 27 26 26 28 27 26 27
#> P .00 .00 .00 .00 .00 .03 .00 .00 .02 .00
#>
#> , , cyl
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.86 1 .91 .86 -.74 .76 -.66 -.90 -.54 -.46 .55
#> N 26 29 26 27 26 26 26 26 26 28 26
#> P .00 .00 .00 .00 .00 .00 .00 .00 .01 .00
#>
#> , , disp
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.89 .91 1 .87 -.71 .90 -.46 -.71 -.67 -.56 .48
#> N 26 26 29 26 26 26 26 26 26 26 26
#> P .00 .00 .00 .00 .00 .02 .00 .00 .00 .01
#>
#> , , hp
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.79 .86 .87 1 -.45 .72 -.63 -.74 -.41 -.30 .68
#> N 26 27 26 29 26 27 26 26 26 28 26
#> P .00 .00 .00 .02 .00 .00 .00 .04 .12 .00
#>
#> , , drat
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r .70 -.74 -.71 -.45 1 -.68 .21 .46 .73 .70 -.14
#> N 27 26 26 26 29 26 26 26 26 26 27
#> P .00 .00 .00 .02 .00 .29 .02 .00 .00 .48
#>
#> , , wt
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.85 .76 .90 .72 -.68 1 -.17 -.48 -.65 -.55 .41
#> N 26 26 26 27 26 29 26 26 26 27 26
#> P .00 .00 .00 .00 .00 .41 .01 .00 .00 .04
#>
#> , , qsec
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r .44 -.66 -.46 -.63 .21 -.17 1 .76 -.18 -.09 -.75
#> N 26 26 26 26 26 26 29 26 27 26 26
#> P .03 .00 .02 .00 .29 .41 .00 .37 .65 .00
#>
#> , , vs
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r .64 -.90 -.71 -.74 .46 -.48 .76 1 .10 .19 -.62
#> N 28 26 26 26 26 26 26 29 27 26 26
#> P .00 .00 .00 .00 .02 .01 .00 .62 .35 .00
#>
#> , , am
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r .64 -.54 -.67 -.41 .73 -.65 -.18 .10 1 .84 .09
#> N 27 26 26 26 26 26 27 27 29 26 26
#> P .00 .00 .00 .04 .00 .00 .37 .62 .00 .67
#>
#> , , gear
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r .44 -.46 -.56 -.30 .70 -.55 -.09 .19 .84 1 .36
#> N 26 28 26 28 26 27 26 26 26 29 26
#> P .02 .01 .00 .12 .00 .00 .65 .35 .00 .07
#>
#> , , carb
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.53 .55 .48 .68 -.14 .41 -.75 -.62 .09 .36 1
#> N 27 26 26 26 27 26 26 26 26 26 29
#> P .00 .00 .01 .00 .48 .04 .00 .00 .67 .07
#>
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 -.861* (26) 1 (29)
#> disp -.891* (26) .907* (26) 1 (29)
#> hp -.793* (26) .864* (27) .873* (26) 1 (29)
#> drat .695* (27) -.740* (26) -.715* (26) -.452 (26) 1 (29)
#> wt -.854* (26) .757* (26) .897* (26) .719* (27) -.681* (26) 1 (29)
#> qsec .438 (26) -.658* (26) -.462 (26) -.634* (26) .214 (26) -.170 (26)
#> vs .644* (28) -.897* (26) -.709* (26) -.736* (26) .462 (26) -.479 (26)
#> am .643* (27) -.542* (26) -.671* (26) -.406 (26) .732* (26) -.652* (26)
#> gear .443 (26) -.460 (28) -.559* (26) -.300 (28) .699* (26) -.554* (27)
#> carb -.529* (27) .551* (26) .484 (26) .676* (26) -.141 (27) .412 (26)
#> qsec vs am gear carb
#> mpg
#> cyl
#> disp
#> hp
#> drat
#> wt
#> qsec 1 (29)
#> vs .759* (26) 1 (29)
#> am -.180 (27) .100 (27) 1 (29)
#> gear -.092 (26) .193 (26) .841* (26) 1 (29)
#> carb -.751* (26) -.624* (26) .088 (26) .359 (26) 1 (29)
pwcor(mna, N = TRUE, P = TRUE, array = FALSE)
#> $r
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 1 -.86 -.89 -.79 .70 -.85 .44 .64 .64 .44 -.53
#> cyl -.86 1 .91 .86 -.74 .76 -.66 -.90 -.54 -.46 .55
#> disp -.89 .91 1 .87 -.71 .90 -.46 -.71 -.67 -.56 .48
#> hp -.79 .86 .87 1 -.45 .72 -.63 -.74 -.41 -.30 .68
#> drat .70 -.74 -.71 -.45 1 -.68 .21 .46 .73 .70 -.14
#> wt -.85 .76 .90 .72 -.68 1 -.17 -.48 -.65 -.55 .41
#> qsec .44 -.66 -.46 -.63 .21 -.17 1 .76 -.18 -.09 -.75
#> vs .64 -.90 -.71 -.74 .46 -.48 .76 1 .10 .19 -.62
#> am .64 -.54 -.67 -.41 .73 -.65 -.18 .10 1 .84 .09
#> gear .44 -.46 -.56 -.30 .70 -.55 -.09 .19 .84 1 .36
#> carb -.53 .55 .48 .68 -.14 .41 -.75 -.62 .09 .36 1
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 26 26 26 27 26 26 28 27 26 27
#> cyl 26 29 26 27 26 26 26 26 26 28 26
#> disp 26 26 29 26 26 26 26 26 26 26 26
#> hp 26 27 26 29 26 27 26 26 26 28 26
#> drat 27 26 26 26 29 26 26 26 26 26 27
#> wt 26 26 26 27 26 29 26 26 26 27 26
#> qsec 26 26 26 26 26 26 29 26 27 26 26
#> vs 28 26 26 26 26 26 26 29 27 26 26
#> am 27 26 26 26 26 26 27 27 29 26 26
#> gear 26 28 26 28 26 27 26 26 26 29 26
#> carb 27 26 26 26 27 26 26 26 26 26 29
#>
#> $P
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg .00 .00 .00 .00 .00 .03 .00 .00 .02 .00
#> cyl .00 .00 .00 .00 .00 .00 .00 .00 .01 .00
#> disp .00 .00 .00 .00 .00 .02 .00 .00 .00 .01
#> hp .00 .00 .00 .02 .00 .00 .00 .04 .12 .00
#> drat .00 .00 .00 .02 .00 .29 .02 .00 .00 .48
#> wt .00 .00 .00 .00 .00 .41 .01 .00 .00 .04
#> qsec .03 .00 .02 .00 .29 .41 .00 .37 .65 .00
#> vs .00 .00 .00 .00 .02 .01 .00 .62 .35 .00
#> am .00 .00 .00 .04 .00 .00 .37 .62 .00 .67
#> gear .02 .01 .00 .12 .00 .00 .65 .35 .00 .07
#> carb .00 .00 .01 .00 .48 .04 .00 .00 .67 .07
#>
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 -.89 .91 1
#> hp -.79 .86 .87 1
#> drat .70 -.74 -.71 -.45 1
#> wt -.85 .76 .90 .72 -.68 1
#> qsec .44 -.66 -.46 -.63 .21 -.17 1
#> vs .64 -.90 -.71 -.74 .46 -.48 .76 1
#> am .64 -.54 -.67 -.41 .73 -.65 -.18 .10 1
#> gear .44 -.46 -.56 -.30 .70 -.55 -.09 .19 .84 1
#> carb -.53 .55 .48 .68 -.14 .41 -.75 -.62 .09 .36 1
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29
#> cyl 26 29
#> disp 26 26 29
#> hp 26 27 26 29
#> drat 27 26 26 26 29
#> wt 26 26 26 27 26 29
#> qsec 26 26 26 26 26 26 29
#> vs 28 26 26 26 26 26 26 29
#> am 27 26 26 26 26 26 27 27 29
#> gear 26 28 26 28 26 27 26 26 26 29
#> carb 27 26 26 26 27 26 26 26 26 26 29
#>
#> $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
#> qsec .03 .00 .02 .00 .29 .41
#> vs .00 .00 .00 .00 .02 .01 .00
#> am .00 .00 .00 .04 .00 .00 .37 .62
#> gear .02 .01 .00 .12 .00 .00 .65 .35 .00
#> carb .00 .00 .01 .00 .48 .04 .00 .00 .67 .07
#>