Skip to contents

Computes (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 pwcor and pwcov all columns must be numeric. All functions are faster on matrices, so converting is advised for large data (see qM).

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 as cor.test and Hmisc::rcorr).

array

logical. If N = TRUE or P = TRUE, TRUE (default) returns output as 3D array whereas FALSE returns a list of matrices.

use

argument passed to cor / cov. If use != "pairwise.complete.obs", sum(complete.cases(X)) is used for N, 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 if P = 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 return x invisibly.

...

other arguments passed to cor or cov. Only sensible if P = 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   -.84  -.84  -.77   .67  -.87   .41   .71   .63   .44  -.57
#> cyl   -.84    1    .90   .80  -.69   .80  -.57  -.88  -.62  -.42   .52
#> disp  -.84   .90    1    .79  -.69   .88  -.40  -.71  -.64  -.52   .39
#> hp    -.77   .80   .79    1   -.43   .70  -.72  -.69  -.33  -.10   .76
#> drat   .67  -.69  -.69  -.43    1   -.73   .03   .44   .78   .67  -.11
#> wt    -.87   .80   .88   .70  -.73    1   -.11  -.60  -.74  -.64   .51
#> qsec   .41  -.57  -.40  -.72   .03  -.11    1    .69  -.10  -.17  -.62
#> vs     .71  -.88  -.71  -.69   .44  -.60   .69    1    .32   .28  -.59
#> am     .63  -.62  -.64  -.33   .78  -.74  -.10   .32    1    .84   .02
#> gear   .44  -.42  -.52  -.10   .67  -.64  -.17   .28   .84    1    .28
#> carb  -.57   .52   .39   .76  -.11   .51  -.62  -.59   .02   .28    1 
pwcov(mna)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg      37.72     -9.09   -660.72   -353.46      2.22     -5.58      4.41
#> cyl      -9.09      3.06    190.13     95.74     -0.68      1.49     -1.73
#> disp   -660.72    190.13  14681.29   6856.11    -48.99    107.00    -90.71
#> hp     -353.46     95.74   6856.11   4805.57    -17.10     41.68    -91.96
#> drat      2.22     -0.68    -48.99    -17.10      0.30     -0.43      0.03
#> wt       -5.58      1.49    107.00     41.68     -0.43      1.02     -0.19
#> qsec      4.41     -1.73    -90.71    -91.96      0.03     -0.19      3.14
#> vs        2.33     -0.80    -44.55    -24.58      0.13     -0.33      0.61
#> am        2.05     -0.56    -40.80    -11.59      0.22     -0.39     -0.07
#> gear      1.86     -0.54    -47.89     -5.61      0.28     -0.47     -0.24
#> carb     -5.95      1.56     77.23     85.74     -0.09      0.71     -1.86
#>             vs        am      gear      carb
#> mpg       2.33      2.05      1.86     -5.95
#> cyl      -0.80     -0.56     -0.54      1.56
#> disp    -44.55    -40.80    -47.89     77.23
#> hp      -24.58    -11.59     -5.61     85.74
#> drat      0.13      0.22      0.28     -0.09
#> wt       -0.33     -0.39     -0.47      0.71
#> qsec      0.61     -0.07     -0.24     -1.86
#> vs        0.26      0.08      0.10     -0.49
#> am        0.08      0.26      0.33      0.02
#> gear      0.10      0.33      0.56      0.36
#> carb     -0.49      0.02      0.36      2.79
pwnobs(mna)
#>      mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg   29  27   26 26   26 27   26 26 26   26   26
#> cyl   27  29   26 26   27 26   26 27 27   26   26
#> disp  26  26   29 28   26 26   26 26 26   26   26
#> hp    26  26   28 29   26 26   26 26 26   26   26
#> drat  26  27   26 26   29 26   26 26 26   26   26
#> wt    27  26   26 26   26 29   26 26 27   27   26
#> qsec  26  26   26 26   26 26   29 26 26   27   26
#> vs    26  27   26 26   26 26   26 29 27   27   26
#> am    26  27   26 26   26 27   26 27 29   27   26
#> gear  26  26   26 26   26 27   27 27 27   29   26
#> carb  26  26   26 26   26 26   26 26 26   26   29
pwcor(mna, N = TRUE)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg    1  (29) -.84 (27) -.84 (26) -.77 (26)  .67 (26) -.87 (27)  .41 (26)
#> cyl  -.84 (27)   1  (29)  .90 (26)  .80 (26) -.69 (27)  .80 (26) -.57 (26)
#> disp -.84 (26)  .90 (26)   1  (29)  .79 (28) -.69 (26)  .88 (26) -.40 (26)
#> hp   -.77 (26)  .80 (26)  .79 (28)   1  (29) -.43 (26)  .70 (26) -.72 (26)
#> drat  .67 (26) -.69 (27) -.69 (26) -.43 (26)   1  (29) -.73 (26)  .03 (26)
#> wt   -.87 (27)  .80 (26)  .88 (26)  .70 (26) -.73 (26)   1  (29) -.11 (26)
#> qsec  .41 (26) -.57 (26) -.40 (26) -.72 (26)  .03 (26) -.11 (26)   1  (29)
#> vs    .71 (26) -.88 (27) -.71 (26) -.69 (26)  .44 (26) -.60 (26)  .69 (26)
#> am    .63 (26) -.62 (27) -.64 (26) -.33 (26)  .78 (26) -.74 (27) -.10 (26)
#> gear  .44 (26) -.42 (26) -.52 (26) -.10 (26)  .67 (26) -.64 (27) -.17 (27)
#> carb -.57 (26)  .52 (26)  .39 (26)  .76 (26) -.11 (26)  .51 (26) -.62 (26)
#>             vs        am      gear      carb
#> mpg   .71 (26)  .63 (26)  .44 (26) -.57 (26)
#> cyl  -.88 (27) -.62 (27) -.42 (26)  .52 (26)
#> disp -.71 (26) -.64 (26) -.52 (26)  .39 (26)
#> hp   -.69 (26) -.33 (26) -.10 (26)  .76 (26)
#> drat  .44 (26)  .78 (26)  .67 (26) -.11 (26)
#> wt   -.60 (26) -.74 (27) -.64 (27)  .51 (26)
#> qsec  .69 (26) -.10 (26) -.17 (27) -.62 (26)
#> vs     1  (29)  .32 (27)  .28 (27) -.59 (26)
#> am    .32 (27)   1  (29)  .84 (27)  .02 (26)
#> gear  .28 (27)  .84 (27)   1  (29)  .28 (26)
#> carb -.59 (26)  .02 (26)  .28 (26)   1  (29)
pwcor(mna, P = TRUE)
#>         mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear
#> mpg     1    -.84*  -.84*  -.77*   .67*  -.87*   .41*   .71*   .63*   .44*
#> cyl   -.84*    1     .90*   .80*  -.69*   .80*  -.57*  -.88*  -.62*  -.42*
#> disp  -.84*   .90*    1     .79*  -.69*   .88*  -.40*  -.71*  -.64*  -.52*
#> hp    -.77*   .80*   .79*    1    -.43*   .70*  -.72*  -.69*  -.33   -.10 
#> drat   .67*  -.69*  -.69*  -.43*    1    -.73*   .03    .44*   .78*   .67*
#> wt    -.87*   .80*   .88*   .70*  -.73*    1    -.11   -.60*  -.74*  -.64*
#> qsec   .41*  -.57*  -.40*  -.72*   .03   -.11     1     .69*  -.10   -.17 
#> vs     .71*  -.88*  -.71*  -.69*   .44*  -.60*   .69*    1     .32    .28 
#> am     .63*  -.62*  -.64*  -.33    .78*  -.74*  -.10    .32     1     .84*
#> gear   .44*  -.42*  -.52*  -.10    .67*  -.64*  -.17    .28    .84*    1  
#> carb  -.57*   .52*   .39    .76*  -.11    .51*  -.62*  -.59*   .02    .28 
#>        carb
#> mpg   -.57*
#> cyl    .52*
#> disp   .39 
#> hp     .76*
#> drat  -.11 
#> wt     .51*
#> qsec  -.62*
#> vs    -.59*
#> am     .02 
#> gear   .28 
#> carb    1  
pwcor(mna, N = TRUE, P = TRUE)
#>             mpg        cyl       disp         hp       drat         wt
#> mpg    1   (29) -.84* (27) -.84* (26) -.77* (26)  .67* (26) -.87* (27)
#> cyl  -.84* (27)   1   (29)  .90* (26)  .80* (26) -.69* (27)  .80* (26)
#> disp -.84* (26)  .90* (26)   1   (29)  .79* (28) -.69* (26)  .88* (26)
#> hp   -.77* (26)  .80* (26)  .79* (28)   1   (29) -.43* (26)  .70* (26)
#> drat  .67* (26) -.69* (27) -.69* (26) -.43* (26)   1   (29) -.73* (26)
#> wt   -.87* (27)  .80* (26)  .88* (26)  .70* (26) -.73* (26)   1   (29)
#> qsec  .41* (26) -.57* (26) -.40* (26) -.72* (26)  .03  (26) -.11  (26)
#> vs    .71* (26) -.88* (27) -.71* (26) -.69* (26)  .44* (26) -.60* (26)
#> am    .63* (26) -.62* (27) -.64* (26) -.33  (26)  .78* (26) -.74* (27)
#> gear  .44* (26) -.42* (26) -.52* (26) -.10  (26)  .67* (26) -.64* (27)
#> carb -.57* (26)  .52* (26)  .39  (26)  .76* (26) -.11  (26)  .51* (26)
#>            qsec         vs         am       gear       carb
#> mpg   .41* (26)  .71* (26)  .63* (26)  .44* (26) -.57* (26)
#> cyl  -.57* (26) -.88* (27) -.62* (27) -.42* (26)  .52* (26)
#> disp -.40* (26) -.71* (26) -.64* (26) -.52* (26)  .39  (26)
#> hp   -.72* (26) -.69* (26) -.33  (26) -.10  (26)  .76* (26)
#> drat  .03  (26)  .44* (26)  .78* (26)  .67* (26) -.11  (26)
#> wt   -.11  (26) -.60* (26) -.74* (27) -.64* (27)  .51* (26)
#> qsec   1   (29)  .69* (26) -.10  (26) -.17  (27) -.62* (26)
#> vs    .69* (26)   1   (29)  .32  (27)  .28  (27) -.59* (26)
#> am   -.10  (26)  .32  (27)   1   (29)  .84* (27)  .02  (26)
#> gear -.17  (27)  .28  (27)  .84* (27)   1   (29)  .28  (26)
#> carb -.62* (26) -.59* (26)  .02  (26)  .28  (26)   1   (29)
aperm(pwcor(mna, N = TRUE, P = TRUE))
#> , , mpg
#> 
#>   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r   1  -.84  -.84  -.77   .67  -.87   .41   .71   .63   .44  -.57
#> N  29    27    26    26    26    27    26    26    26    26    26
#> P       .00   .00   .00   .00   .00   .04   .00   .00   .03   .00
#> 
#> , , cyl
#> 
#>     mpg cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.84   1   .90   .80  -.69   .80  -.57  -.88  -.62  -.42   .52
#> N    27  29    26    26    27    26    26    27    27    26    26
#> P   .00       .00   .00   .00   .00   .00   .00   .00   .03   .01
#> 
#> , , disp
#> 
#>     mpg   cyl disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.84   .90    1   .79  -.69   .88  -.40  -.71  -.64  -.52   .39
#> N    26    26   29    28    26    26    26    26    26    26    26
#> P   .00   .00        .00   .00   .00   .04   .00   .00   .01   .05
#> 
#> , , hp
#> 
#>     mpg   cyl  disp  hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.77   .80   .79   1  -.43   .70  -.72  -.69  -.33  -.10   .76
#> N    26    26    28  29    26    26    26    26    26    26    26
#> P   .00   .00   .00       .03   .00   .00   .00   .10   .61   .00
#> 
#> , , drat
#> 
#>     mpg   cyl  disp    hp drat    wt  qsec    vs    am  gear  carb
#> r   .67  -.69  -.69  -.43    1  -.73   .03   .44   .78   .67  -.11
#> N    26    27    26    26   29    26    26    26    26    26    26
#> P   .00   .00   .00   .03        .00   .89   .02   .00   .00   .60
#> 
#> , , wt
#> 
#>     mpg   cyl  disp    hp  drat  wt  qsec    vs    am  gear  carb
#> r  -.87   .80   .88   .70  -.73   1  -.11  -.60  -.74  -.64   .51
#> N    27    26    26    26    26  29    26    26    27    27    26
#> P   .00   .00   .00   .00   .00       .59   .00   .00   .00   .01
#> 
#> , , qsec
#> 
#>     mpg   cyl  disp    hp  drat    wt qsec    vs    am  gear  carb
#> r   .41  -.57  -.40  -.72   .03  -.11    1   .69  -.10  -.17  -.62
#> N    26    26    26    26    26    26   29    26    26    27    26
#> P   .04   .00   .04   .00   .89   .59        .00   .62   .38   .00
#> 
#> , , vs
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec  vs    am  gear  carb
#> r   .71  -.88  -.71  -.69   .44  -.60   .69   1   .32   .28  -.59
#> N    26    27    26    26    26    26    26  29    27    27    26
#> P   .00   .00   .00   .00   .02   .00   .00       .10   .16   .00
#> 
#> , , am
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs  am  gear  carb
#> r   .63  -.62  -.64  -.33   .78  -.74  -.10   .32   1   .84   .02
#> N    26    27    26    26    26    27    26    27  29    27    26
#> P   .00   .00   .00   .10   .00   .00   .62   .10       .00   .92
#> 
#> , , gear
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am gear  carb
#> r   .44  -.42  -.52  -.10   .67  -.64  -.17   .28   .84    1   .28
#> N    26    26    26    26    26    27    27    27    27   29    26
#> P   .03   .03   .01   .61   .00   .00   .38   .16   .00        .16
#> 
#> , , carb
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear carb
#> r  -.57   .52   .39   .76  -.11   .51  -.62  -.59   .02   .28    1
#> N    26    26    26    26    26    26    26    26    26    26   29
#> P   .00   .01   .05   .00   .60   .01   .00   .00   .92   .16     
#> 
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  -.837* (27)   1    (29)                                                
#> disp -.841* (26)  .898* (26)   1    (29)                                    
#> hp   -.767* (26)  .803* (26)  .788* (28)   1    (29)                        
#> drat  .666* (26) -.688* (27) -.693* (26) -.430  (26)   1    (29)            
#> wt   -.866* (27)  .796* (26)  .878* (26)  .702* (26) -.730* (26)   1    (29)
#> qsec  .407  (26) -.567* (26) -.401  (26) -.715* (26)  .028  (26) -.112  (26)
#> vs    .711* (26) -.885* (27) -.711* (26) -.688* (26)  .440  (26) -.601* (26)
#> am    .629* (26) -.624* (27) -.637* (26) -.327  (26)  .781* (26) -.740* (27)
#> gear  .438  (26) -.418  (26) -.515* (26) -.104  (26)  .670* (26) -.637* (27)
#> carb -.565* (26)  .519* (26)  .387  (26)  .756* (26) -.109  (26)  .511* (26)
#>             qsec          vs          am        gear        carb
#> mpg                                                             
#> cyl                                                             
#> disp                                                            
#> hp                                                              
#> drat                                                            
#> wt                                                              
#> qsec   1    (29)                                                
#> vs    .686* (26)   1    (29)                                    
#> am   -.103  (26)  .320  (27)   1    (29)                        
#> gear -.174  (27)  .279  (27)  .838* (27)   1    (29)            
#> carb -.619* (26) -.590* (26)  .021  (26)  .281  (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  -.84  -.84  -.77   .67  -.87   .41   .71   .63   .44  -.57
#> cyl   -.84     1   .90   .80  -.69   .80  -.57  -.88  -.62  -.42   .52
#> disp  -.84   .90     1   .79  -.69   .88  -.40  -.71  -.64  -.52   .39
#> hp    -.77   .80   .79     1  -.43   .70  -.72  -.69  -.33  -.10   .76
#> drat   .67  -.69  -.69  -.43     1  -.73   .03   .44   .78   .67  -.11
#> wt    -.87   .80   .88   .70  -.73     1  -.11  -.60  -.74  -.64   .51
#> qsec   .41  -.57  -.40  -.72   .03  -.11     1   .69  -.10  -.17  -.62
#> vs     .71  -.88  -.71  -.69   .44  -.60   .69     1   .32   .28  -.59
#> am     .63  -.62  -.64  -.33   .78  -.74  -.10   .32     1   .84   .02
#> gear   .44  -.42  -.52  -.10   .67  -.64  -.17   .28   .84     1   .28
#> carb  -.57   .52   .39   .76  -.11   .51  -.62  -.59   .02   .28     1
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29  27   26  26   26  27   26  26  26   26   26
#> cyl   27  29   26  26   27  26   26  27  27   26   26
#> disp  26  26   29  28   26  26   26  26  26   26   26
#> hp    26  26   28  29   26  26   26  26  26   26   26
#> drat  26  27   26  26   29  26   26  26  26   26   26
#> wt    27  26   26  26   26  29   26  26  27   27   26
#> qsec  26  26   26  26   26  26   29  26  26   27   26
#> vs    26  27   26  26   26  26   26  29  27   27   26
#> am    26  27   26  26   26  27   26  27  29   27   26
#> gear  26  26   26  26   26  27   27  27  27   29   26
#> carb  26  26   26  26   26  26   26  26  26   26   29
#> 
#> $P
#>       mpg  cyl disp   hp drat   wt qsec   vs   am gear carb
#> mpg        .00  .00  .00  .00  .00  .04  .00  .00  .03  .00
#> cyl   .00       .00  .00  .00  .00  .00  .00  .00  .03  .01
#> disp  .00  .00       .00  .00  .00  .04  .00  .00  .01  .05
#> hp    .00  .00  .00       .03  .00  .00  .00  .10  .61  .00
#> drat  .00  .00  .00  .03       .00  .89  .02  .00  .00  .60
#> wt    .00  .00  .00  .00  .00       .59  .00  .00  .00  .01
#> qsec  .04  .00  .04  .00  .89  .59       .00  .62  .38  .00
#> vs    .00  .00  .00  .00  .02  .00  .00       .10  .16  .00
#> am    .00  .00  .00  .10  .00  .00  .62  .10       .00  .92
#> gear  .03  .03  .01  .61  .00  .00  .38  .16  .00       .16
#> carb  .00  .01  .05  .00  .60  .01  .00  .00  .92  .16     
#> 
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   -.84     1                                                     
#> disp  -.84   .90     1                                               
#> hp    -.77   .80   .79     1                                         
#> drat   .67  -.69  -.69  -.43     1                                   
#> wt    -.87   .80   .88   .70  -.73     1                             
#> qsec   .41  -.57  -.40  -.72   .03  -.11     1                       
#> vs     .71  -.88  -.71  -.69   .44  -.60   .69     1                 
#> am     .63  -.62  -.64  -.33   .78  -.74  -.10   .32     1           
#> gear   .44  -.42  -.52  -.10   .67  -.64  -.17   .28   .84     1     
#> carb  -.57   .52   .39   .76  -.11   .51  -.62  -.59   .02   .28    1
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29                                             
#> cyl   27  29                                         
#> disp  26  26   29                                    
#> hp    26  26   28  29                                
#> drat  26  27   26  26   29                           
#> wt    27  26   26  26   26  29                       
#> qsec  26  26   26  26   26  26   29                  
#> vs    26  27   26  26   26  26   26  29              
#> am    26  27   26  26   26  27   26  27  29          
#> gear  26  26   26  26   26  27   27  27  27   29     
#> carb  26  26   26  26   26  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  .03                                   
#> wt    .00  .00  .00  .00  .00                              
#> qsec  .04  .00  .04  .00  .89  .59                         
#> vs    .00  .00  .00  .00  .02  .00  .00                    
#> am    .00  .00  .00  .10  .00  .00  .62  .10               
#> gear  .03  .03  .01  .61  .00  .00  .38  .16  .00          
#> carb  .00  .01  .05  .00  .60  .01  .00  .00  .92  .16     
#>