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  -.83  -.81   .71  -.87   .46   .73   .68   .54  -.58
#> cyl   -.84    1    .90   .83  -.70   .80  -.58  -.91  -.50  -.55   .56
#> disp  -.83   .90    1    .76  -.70   .89  -.39  -.75  -.56  -.59   .39
#> hp    -.81   .83   .76    1   -.44   .63  -.66  -.72  -.21  -.33   .73
#> drat   .71  -.70  -.70  -.44    1   -.72   .21   .52   .73   .72  -.23
#> wt    -.87   .80   .89   .63  -.72    1   -.13  -.57  -.70  -.63   .42
#> qsec   .46  -.58  -.39  -.66   .21  -.13    1    .68  -.11  -.11  -.54
#> vs     .73  -.91  -.75  -.72   .52  -.57   .68    1    .20   .41  -.53
#> am     .68  -.50  -.56  -.21   .73  -.70  -.11   .20    1    .83   .07
#> gear   .54  -.55  -.59  -.33   .72  -.63  -.11   .41   .83    1    .09
#> carb  -.58   .56   .39   .73  -.23   .42  -.54  -.53   .07   .09    1 
pwcov(mna)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg      37.55     -9.14   -636.42   -314.49      2.48     -5.56      5.00
#> cyl      -9.14      3.06    193.03     98.69     -0.69      1.45     -1.73
#> disp   -636.42    193.03  14444.87   6408.56    -49.23    104.03    -83.32
#> hp     -314.49     98.69   6408.56   4802.89    -17.78     40.61    -71.00
#> drat      2.48     -0.69    -49.23    -17.78      0.30     -0.43      0.20
#> wt       -5.56      1.45    104.03     40.61     -0.43      1.02     -0.23
#> qsec      5.00     -1.73    -83.32    -71.00      0.20     -0.23      2.79
#> vs        2.18     -0.82    -46.73    -25.72      0.15     -0.30      0.56
#> am        2.17     -0.43    -34.54     -7.42      0.21     -0.37     -0.10
#> gear      2.33     -0.68    -53.72    -14.96      0.30     -0.49     -0.11
#> carb     -4.56      1.66     78.22     84.93     -0.20      0.72     -1.17
#>             vs        am      gear      carb
#> mpg       2.18      2.17      2.33     -4.56
#> cyl      -0.82     -0.43     -0.68      1.66
#> disp    -46.73    -34.54    -53.72     78.22
#> hp      -25.72     -7.42    -14.96     84.93
#> drat      0.15      0.21      0.30     -0.20
#> wt       -0.30     -0.37     -0.49      0.72
#> qsec      0.56     -0.10     -0.11     -1.17
#> vs        0.26      0.05      0.15     -0.41
#> am        0.05      0.25      0.31      0.06
#> gear      0.15      0.31      0.52      0.09
#> carb     -0.41      0.06      0.09      2.61
pwnobs(mna)
#>      mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg   29  27   26 26   26 27   27 26 26   27   26
#> cyl   27  29   26 26   26 26   26 26 27   26   26
#> disp  26  26   29 27   26 27   26 26 26   26   26
#> hp    26  26   27 29   26 27   26 26 27   26   26
#> drat  26  26   26 26   29 26   26 26 26   26   26
#> wt    27  26   27 27   26 29   26 26 27   26   26
#> qsec  27  26   26 26   26 26   29 26 26   28   27
#> vs    26  26   26 26   26 26   26 29 26   26   27
#> am    26  27   26 27   26 27   26 26 29   26   27
#> gear  27  26   26 26   26 26   28 26 26   29   27
#> carb  26  26   26 26   26 26   27 27 27   27   29
pwcor(mna, N = TRUE)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg    1  (29) -.84 (27) -.83 (26) -.81 (26)  .71 (26) -.87 (27)  .46 (27)
#> cyl  -.84 (27)   1  (29)  .90 (26)  .83 (26) -.70 (26)  .80 (26) -.58 (26)
#> disp -.83 (26)  .90 (26)   1  (29)  .76 (27) -.70 (26)  .89 (27) -.39 (26)
#> hp   -.81 (26)  .83 (26)  .76 (27)   1  (29) -.44 (26)  .63 (27) -.66 (26)
#> drat  .71 (26) -.70 (26) -.70 (26) -.44 (26)   1  (29) -.72 (26)  .21 (26)
#> wt   -.87 (27)  .80 (26)  .89 (27)  .63 (27) -.72 (26)   1  (29) -.13 (26)
#> qsec  .46 (27) -.58 (26) -.39 (26) -.66 (26)  .21 (26) -.13 (26)   1  (29)
#> vs    .73 (26) -.91 (26) -.75 (26) -.72 (26)  .52 (26) -.57 (26)  .68 (26)
#> am    .68 (26) -.50 (27) -.56 (26) -.21 (27)  .73 (26) -.70 (27) -.11 (26)
#> gear  .54 (27) -.55 (26) -.59 (26) -.33 (26)  .72 (26) -.63 (26) -.11 (28)
#> carb -.58 (26)  .56 (26)  .39 (26)  .73 (26) -.23 (26)  .42 (26) -.54 (27)
#>             vs        am      gear      carb
#> mpg   .73 (26)  .68 (26)  .54 (27) -.58 (26)
#> cyl  -.91 (26) -.50 (27) -.55 (26)  .56 (26)
#> disp -.75 (26) -.56 (26) -.59 (26)  .39 (26)
#> hp   -.72 (26) -.21 (27) -.33 (26)  .73 (26)
#> drat  .52 (26)  .73 (26)  .72 (26) -.23 (26)
#> wt   -.57 (26) -.70 (27) -.63 (26)  .42 (26)
#> qsec  .68 (26) -.11 (26) -.11 (28) -.54 (27)
#> vs     1  (29)  .20 (26)  .41 (26) -.53 (27)
#> am    .20 (26)   1  (29)  .83 (26)  .07 (27)
#> gear  .41 (26)  .83 (26)   1  (29)  .09 (27)
#> carb -.53 (27)  .07 (27)  .09 (27)   1  (29)
pwcor(mna, P = TRUE)
#>         mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear
#> mpg     1    -.84*  -.83*  -.81*   .71*  -.87*   .46*   .73*   .68*   .54*
#> cyl   -.84*    1     .90*   .83*  -.70*   .80*  -.58*  -.91*  -.50*  -.55*
#> disp  -.83*   .90*    1     .76*  -.70*   .89*  -.39*  -.75*  -.56*  -.59*
#> hp    -.81*   .83*   .76*    1    -.44*   .63*  -.66*  -.72*  -.21   -.33 
#> drat   .71*  -.70*  -.70*  -.44*    1    -.72*   .21    .52*   .73*   .72*
#> wt    -.87*   .80*   .89*   .63*  -.72*    1    -.13   -.57*  -.70*  -.63*
#> qsec   .46*  -.58*  -.39*  -.66*   .21   -.13     1     .68*  -.11   -.11 
#> vs     .73*  -.91*  -.75*  -.72*   .52*  -.57*   .68*    1     .20    .41*
#> am     .68*  -.50*  -.56*  -.21    .73*  -.70*  -.11    .20     1     .83*
#> gear   .54*  -.55*  -.59*  -.33    .72*  -.63*  -.11    .41*   .83*    1  
#> carb  -.58*   .56*   .39*   .73*  -.23    .42*  -.54*  -.53*   .07    .09 
#>        carb
#> mpg   -.58*
#> cyl    .56*
#> disp   .39*
#> hp     .73*
#> drat  -.23 
#> wt     .42*
#> qsec  -.54*
#> vs    -.53*
#> am     .07 
#> gear   .09 
#> carb    1  
pwcor(mna, N = TRUE, P = TRUE)
#>             mpg        cyl       disp         hp       drat         wt
#> mpg    1   (29) -.84* (27) -.83* (26) -.81* (26)  .71* (26) -.87* (27)
#> cyl  -.84* (27)   1   (29)  .90* (26)  .83* (26) -.70* (26)  .80* (26)
#> disp -.83* (26)  .90* (26)   1   (29)  .76* (27) -.70* (26)  .89* (27)
#> hp   -.81* (26)  .83* (26)  .76* (27)   1   (29) -.44* (26)  .63* (27)
#> drat  .71* (26) -.70* (26) -.70* (26) -.44* (26)   1   (29) -.72* (26)
#> wt   -.87* (27)  .80* (26)  .89* (27)  .63* (27) -.72* (26)   1   (29)
#> qsec  .46* (27) -.58* (26) -.39* (26) -.66* (26)  .21  (26) -.13  (26)
#> vs    .73* (26) -.91* (26) -.75* (26) -.72* (26)  .52* (26) -.57* (26)
#> am    .68* (26) -.50* (27) -.56* (26) -.21  (27)  .73* (26) -.70* (27)
#> gear  .54* (27) -.55* (26) -.59* (26) -.33  (26)  .72* (26) -.63* (26)
#> carb -.58* (26)  .56* (26)  .39* (26)  .73* (26) -.23  (26)  .42* (26)
#>            qsec         vs         am       gear       carb
#> mpg   .46* (27)  .73* (26)  .68* (26)  .54* (27) -.58* (26)
#> cyl  -.58* (26) -.91* (26) -.50* (27) -.55* (26)  .56* (26)
#> disp -.39* (26) -.75* (26) -.56* (26) -.59* (26)  .39* (26)
#> hp   -.66* (26) -.72* (26) -.21  (27) -.33  (26)  .73* (26)
#> drat  .21  (26)  .52* (26)  .73* (26)  .72* (26) -.23  (26)
#> wt   -.13  (26) -.57* (26) -.70* (27) -.63* (26)  .42* (26)
#> qsec   1   (29)  .68* (26) -.11  (26) -.11  (28) -.54* (27)
#> vs    .68* (26)   1   (29)  .20  (26)  .41* (26) -.53* (27)
#> am   -.11  (26)  .20  (26)   1   (29)  .83* (26)  .07  (27)
#> gear -.11  (28)  .41* (26)  .83* (26)   1   (29)  .09  (27)
#> carb -.54* (27) -.53* (27)  .07  (27)  .09  (27)   1   (29)
aperm(pwcor(mna, N = TRUE, P = TRUE))
#> , , mpg
#> 
#>   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r   1  -.84  -.83  -.81   .71  -.87   .46   .73   .68   .54  -.58
#> N  29    27    26    26    26    27    27    26    26    27    26
#> P       .00   .00   .00   .00   .00   .02   .00   .00   .00   .00
#> 
#> , , cyl
#> 
#>     mpg cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.84   1   .90   .83  -.70   .80  -.58  -.91  -.50  -.55   .56
#> N    27  29    26    26    26    26    26    26    27    26    26
#> P   .00       .00   .00   .00   .00   .00   .00   .01   .00   .00
#> 
#> , , disp
#> 
#>     mpg   cyl disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.83   .90    1   .76  -.70   .89  -.39  -.75  -.56  -.59   .39
#> N    26    26   29    27    26    27    26    26    26    26    26
#> P   .00   .00        .00   .00   .00   .05   .00   .00   .00   .05
#> 
#> , , hp
#> 
#>     mpg   cyl  disp  hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.81   .83   .76   1  -.44   .63  -.66  -.72  -.21  -.33   .73
#> N    26    26    27  29    26    27    26    26    27    26    26
#> P   .00   .00   .00       .03   .00   .00   .00   .29   .10   .00
#> 
#> , , drat
#> 
#>     mpg   cyl  disp    hp drat    wt  qsec    vs    am  gear  carb
#> r   .71  -.70  -.70  -.44    1  -.72   .21   .52   .73   .72  -.23
#> N    26    26    26    26   29    26    26    26    26    26    26
#> P   .00   .00   .00   .03        .00   .29   .01   .00   .00   .27
#> 
#> , , wt
#> 
#>     mpg   cyl  disp    hp  drat  wt  qsec    vs    am  gear  carb
#> r  -.87   .80   .89   .63  -.72   1  -.13  -.57  -.70  -.63   .42
#> N    27    26    27    27    26  29    26    26    27    26    26
#> P   .00   .00   .00   .00   .00       .53   .00   .00   .00   .03
#> 
#> , , qsec
#> 
#>     mpg   cyl  disp    hp  drat    wt qsec    vs    am  gear  carb
#> r   .46  -.58  -.39  -.66   .21  -.13    1   .68  -.11  -.11  -.54
#> N    27    26    26    26    26    26   29    26    26    28    27
#> P   .02   .00   .05   .00   .29   .53        .00   .58   .58   .00
#> 
#> , , vs
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec  vs    am  gear  carb
#> r   .73  -.91  -.75  -.72   .52  -.57   .68   1   .20   .41  -.53
#> N    26    26    26    26    26    26    26  29    26    26    27
#> P   .00   .00   .00   .00   .01   .00   .00       .34   .04   .00
#> 
#> , , am
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs  am  gear  carb
#> r   .68  -.50  -.56  -.21   .73  -.70  -.11   .20   1   .83   .07
#> N    26    27    26    27    26    27    26    26  29    26    27
#> P   .00   .01   .00   .29   .00   .00   .58   .34       .00   .71
#> 
#> , , gear
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am gear  carb
#> r   .54  -.55  -.59  -.33   .72  -.63  -.11   .41   .83    1   .09
#> N    27    26    26    26    26    26    28    26    26   29    27
#> P   .00   .00   .00   .10   .00   .00   .58   .04   .00        .65
#> 
#> , , carb
#> 
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear carb
#> r  -.58   .56   .39   .73  -.23   .42  -.54  -.53   .07   .09    1
#> N    26    26    26    26    26    26    27    27    27    27   29
#> P   .00   .00   .05   .00   .27   .03   .00   .00   .71   .65     
#> 
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  -.838* (27)   1    (29)                                                
#> disp -.832* (26)  .899* (26)   1    (29)                                    
#> hp   -.806* (26)  .827* (26)  .764* (27)   1    (29)                        
#> drat  .705* (26) -.700* (26) -.696* (26) -.437  (26)   1    (29)            
#> wt   -.869* (27)  .803* (26)  .888* (27)  .625* (27) -.723* (26)   1    (29)
#> qsec  .459  (27) -.583* (26) -.390  (26) -.660* (26)  .215  (26) -.129  (26)
#> vs    .730* (26) -.906* (26) -.751* (26) -.721* (26)  .523* (26) -.573* (26)
#> am    .682* (26) -.501* (27) -.559* (26) -.212  (27)  .732* (26) -.697* (27)
#> gear  .535* (27) -.551* (26) -.593* (26) -.327  (26)  .723* (26) -.630* (26)
#> carb -.576* (26)  .562* (26)  .389  (26)  .734* (26) -.227  (26)  .417  (26)
#>             qsec          vs          am        gear        carb
#> mpg                                                             
#> cyl                                                             
#> disp                                                            
#> hp                                                              
#> drat                                                            
#> wt                                                              
#> qsec   1    (29)                                                
#> vs    .683* (26)   1    (29)                                    
#> am   -.114  (26)  .195  (26)   1    (29)                        
#> gear -.110  (28)  .413  (26)  .832* (26)   1    (29)            
#> carb -.542* (27) -.530* (27)  .074  (27)  .092  (27)   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  -.83  -.81   .71  -.87   .46   .73   .68   .54  -.58
#> cyl   -.84     1   .90   .83  -.70   .80  -.58  -.91  -.50  -.55   .56
#> disp  -.83   .90     1   .76  -.70   .89  -.39  -.75  -.56  -.59   .39
#> hp    -.81   .83   .76     1  -.44   .63  -.66  -.72  -.21  -.33   .73
#> drat   .71  -.70  -.70  -.44     1  -.72   .21   .52   .73   .72  -.23
#> wt    -.87   .80   .89   .63  -.72     1  -.13  -.57  -.70  -.63   .42
#> qsec   .46  -.58  -.39  -.66   .21  -.13     1   .68  -.11  -.11  -.54
#> vs     .73  -.91  -.75  -.72   .52  -.57   .68     1   .20   .41  -.53
#> am     .68  -.50  -.56  -.21   .73  -.70  -.11   .20     1   .83   .07
#> gear   .54  -.55  -.59  -.33   .72  -.63  -.11   .41   .83     1   .09
#> carb  -.58   .56   .39   .73  -.23   .42  -.54  -.53   .07   .09     1
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29  27   26  26   26  27   27  26  26   27   26
#> cyl   27  29   26  26   26  26   26  26  27   26   26
#> disp  26  26   29  27   26  27   26  26  26   26   26
#> hp    26  26   27  29   26  27   26  26  27   26   26
#> drat  26  26   26  26   29  26   26  26  26   26   26
#> wt    27  26   27  27   26  29   26  26  27   26   26
#> qsec  27  26   26  26   26  26   29  26  26   28   27
#> vs    26  26   26  26   26  26   26  29  26   26   27
#> am    26  27   26  27   26  27   26  26  29   26   27
#> gear  27  26   26  26   26  26   28  26  26   29   27
#> carb  26  26   26  26   26  26   27  27  27   27   29
#> 
#> $P
#>       mpg  cyl disp   hp drat   wt qsec   vs   am gear carb
#> mpg        .00  .00  .00  .00  .00  .02  .00  .00  .00  .00
#> cyl   .00       .00  .00  .00  .00  .00  .00  .01  .00  .00
#> disp  .00  .00       .00  .00  .00  .05  .00  .00  .00  .05
#> hp    .00  .00  .00       .03  .00  .00  .00  .29  .10  .00
#> drat  .00  .00  .00  .03       .00  .29  .01  .00  .00  .27
#> wt    .00  .00  .00  .00  .00       .53  .00  .00  .00  .03
#> qsec  .02  .00  .05  .00  .29  .53       .00  .58  .58  .00
#> vs    .00  .00  .00  .00  .01  .00  .00       .34  .04  .00
#> am    .00  .01  .00  .29  .00  .00  .58  .34       .00  .71
#> gear  .00  .00  .00  .10  .00  .00  .58  .04  .00       .65
#> carb  .00  .00  .05  .00  .27  .03  .00  .00  .71  .65     
#> 
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  -.83   .90     1                                               
#> hp    -.81   .83   .76     1                                         
#> drat   .71  -.70  -.70  -.44     1                                   
#> wt    -.87   .80   .89   .63  -.72     1                             
#> qsec   .46  -.58  -.39  -.66   .21  -.13     1                       
#> vs     .73  -.91  -.75  -.72   .52  -.57   .68     1                 
#> am     .68  -.50  -.56  -.21   .73  -.70  -.11   .20     1           
#> gear   .54  -.55  -.59  -.33   .72  -.63  -.11   .41   .83     1     
#> carb  -.58   .56   .39   .73  -.23   .42  -.54  -.53   .07   .09    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   27  29                                
#> drat  26  26   26  26   29                           
#> wt    27  26   27  27   26  29                       
#> qsec  27  26   26  26   26  26   29                  
#> vs    26  26   26  26   26  26   26  29              
#> am    26  27   26  27   26  27   26  26  29          
#> gear  27  26   26  26   26  26   28  26  26   29     
#> carb  26  26   26  26   26  26   27  27  27   27   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  .02  .00  .05  .00  .29  .53                         
#> vs    .00  .00  .00  .00  .01  .00  .00                    
#> am    .00  .01  .00  .29  .00  .00  .58  .34               
#> gear  .00  .00  .00  .10  .00  .00  .58  .04  .00          
#> carb  .00  .00  .05  .00  .27  .03  .00  .00  .71  .65     
#>