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.

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, return = FALSE, ...)

# S3 method for pwcov
print(x, digits = 2L, 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   -.85  -.86  -.82   .69  -.88   .40   .68   .65   .56  -.59
#> cyl   -.85    1    .90   .89  -.67   .79  -.58  -.80  -.52  -.62   .49
#> disp  -.86   .90    1    .80  -.78   .90  -.41  -.72  -.68  -.68   .36
#> hp    -.82   .89   .80    1   -.49   .62  -.71  -.72  -.21  -.17   .77
#> drat   .69  -.67  -.78  -.49    1   -.73   .11   .46   .69   .67  -.17
#> wt    -.88   .79   .90   .62  -.73    1   -.15  -.54  -.68  -.61   .40
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcov(mna)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg      38.28     -9.30   -727.62   -343.80      2.42     -5.28      4.61
#> cyl      -9.30      3.12    204.54     97.92     -0.67      1.32     -1.85
#> disp   -727.62    204.54  15869.66   7274.08    -56.96    102.73    -97.29
#> hp     -343.80     97.92   7274.08   5127.41    -19.36     42.48   -100.07
#> drat      2.42     -0.67    -56.96    -19.36      0.30     -0.39      0.12
#> wt       -5.28      1.32    102.73     42.48     -0.39      0.84     -0.28
#>             vs        am      gear      carb
#> mpg       2.20      1.98      2.63     -4.88
#> cyl      -0.73     -0.46     -0.78      1.20
#> disp    -46.83    -41.41    -61.81     75.42
#> hp      -26.38     -7.82     -8.89     95.56
#> drat      0.12      0.18      0.27     -0.15
#> wt       -0.26     -0.29     -0.43      0.58
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwnobs(mna)
#>      mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg   29  28   26 26   27 26   27 26 26   26   27
#> cyl   28  29   26 26   26 26   26 27 26   26   26
#> disp  26  26   29 26   26 26   26 26 26   27   26
#> hp    26  26   26 29   26 26   27 26 27   28   26
#> drat  27  26   26 26   29 26   27 26 26   26   27
#> wt    26  26   26 26   26 29   26 27 26   26   26
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg    1  (29) -.85 (28) -.86 (26) -.82 (26)  .69 (27) -.88 (26)  .40 (27)
#> cyl  -.85 (28)   1  (29)  .90 (26)  .89 (26) -.67 (26)  .79 (26) -.58 (26)
#> disp -.86 (26)  .90 (26)   1  (29)  .80 (26) -.78 (26)  .90 (26) -.41 (26)
#> hp   -.82 (26)  .89 (26)  .80 (26)   1  (29) -.49 (26)  .62 (26) -.71 (27)
#> drat  .69 (27) -.67 (26) -.78 (26) -.49 (26)   1  (29) -.73 (26)  .11 (27)
#> wt   -.88 (26)  .79 (26)  .90 (26)  .62 (26) -.73 (26)   1  (29) -.15 (26)
#>             vs        am      gear      carb
#> mpg   .68 (26)  .65 (26)  .56 (26) -.59 (27)
#> cyl  -.80 (27) -.52 (26) -.62 (26)  .49 (26)
#> disp -.72 (26) -.68 (26) -.68 (27)  .36 (26)
#> hp   -.72 (26) -.21 (27) -.17 (28)  .77 (26)
#> drat  .46 (26)  .69 (26)  .67 (26) -.17 (27)
#> wt   -.54 (27) -.68 (26) -.61 (26)  .40 (26)
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, P = TRUE)
#>         mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear
#> mpg     1    -.85*  -.86*  -.82*   .69*  -.88*   .40*   .68*   .65*   .56*
#> cyl   -.85*    1     .90*   .89*  -.67*   .79*  -.58*  -.80*  -.52*  -.62*
#> disp  -.86*   .90*    1     .80*  -.78*   .90*  -.41*  -.72*  -.68*  -.68*
#> hp    -.82*   .89*   .80*    1    -.49*   .62*  -.71*  -.72*  -.21   -.17 
#> drat   .69*  -.67*  -.78*  -.49*    1    -.73*   .11    .46*   .69*   .67*
#> wt    -.88*   .79*   .90*   .62*  -.73*    1    -.15   -.54*  -.68*  -.61*
#>        carb
#> mpg   -.59*
#> cyl    .49*
#> disp   .36 
#> hp     .77*
#> drat  -.17 
#> wt     .40*
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE, P = TRUE)
#>             mpg        cyl       disp         hp       drat         wt
#> mpg    1   (29) -.85* (28) -.86* (26) -.82* (26)  .69* (27) -.88* (26)
#> cyl  -.85* (28)   1   (29)  .90* (26)  .89* (26) -.67* (26)  .79* (26)
#> disp -.86* (26)  .90* (26)   1   (29)  .80* (26) -.78* (26)  .90* (26)
#> hp   -.82* (26)  .89* (26)  .80* (26)   1   (29) -.49* (26)  .62* (26)
#> drat  .69* (27) -.67* (26) -.78* (26) -.49* (26)   1   (29) -.73* (26)
#> wt   -.88* (26)  .79* (26)  .90* (26)  .62* (26) -.73* (26)   1   (29)
#>            qsec         vs         am       gear       carb
#> mpg   .40* (27)  .68* (26)  .65* (26)  .56* (26) -.59* (27)
#> cyl  -.58* (26) -.80* (27) -.52* (26) -.62* (26)  .49* (26)
#> disp -.41* (26) -.72* (26) -.68* (26) -.68* (27)  .36  (26)
#> hp   -.71* (27) -.72* (26) -.21  (27) -.17  (28)  .77* (26)
#> drat  .11  (27)  .46* (26)  .69* (26)  .67* (26) -.17  (27)
#> wt   -.15  (26) -.54* (27) -.68* (26) -.61* (26)  .40* (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.00  -.85  -.86  -.82   .69  -.88   .40   .68   .65   .56  -.59
#> N     29    28    26    26    27    26    27    26    26    26    27
#> P          .00   .00   .00   .00   .00   .04   .00   .00   .00   .00
#> 
#> , , cyl
#> 
#>     mpg cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.85   1   .90   .89  -.67   .79  -.58  -.80  -.52  -.62   .49
#> N    28  29    26    26    26    26    26    27    26    26    26
#> P   .00       .00   .00   .00   .00   .00   .00   .01   .00   .01
#> 
#>  [ 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  -.853* (28)   1    (29)                                                
#> disp -.861* (26)  .895* (26)   1    (29)                                    
#> hp   -.823* (26)  .889* (26)  .805* (26)   1    (29)                        
#> drat  .691* (27) -.673* (26) -.776* (26) -.493  (26)   1    (29)            
#> wt   -.880* (26)  .790* (26)  .897* (26)  .618* (26) -.729* (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.00  -.85   -.86  -.82   .69  -.88   .40    .68   .65   .56  -.59
#> cyl    -.85     1    .90   .89  -.67   .79  -.58   -.80  -.52  -.62   .49
#> disp   -.86   .90   1.00   .80  -.78   .90  -.41   -.72  -.68  -.68   .36
#> hp     -.82   .89    .80     1  -.49   .62  -.71   -.72  -.21  -.17   .77
#> drat    .69  -.67   -.78  -.49     1  -.73   .11    .46   .69   .67  -.17
#> wt     -.88   .79    .90   .62  -.73     1  -.15   -.54  -.68  -.61   .40
#>  [ reached getOption("max.print") -- omitted 5 rows ]
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29  28   26  26   27  26   27  26  26   26   27
#> cyl   28  29   26  26   26  26   26  27  26   26   26
#> disp  26  26   29  26   26  26   26  26  26   27   26
#> hp    26  26   26  29   26  26   27  26  27   28   26
#> drat  27  26   26  26   29  26   27  26  26   26   27
#> wt    26  26   26  26   26  29   26  27  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  .04  .00  .00  .00  .00
#> cyl   .00       .00  .00  .00  .00  .00  .00  .01  .00  .01
#> disp  .00  .00       .00  .00  .00  .04  .00  .00  .00  .07
#> hp    .00  .00  .00       .01  .00  .00  .00  .29  .40  .00
#> drat  .00  .00  .00  .01       .00  .58  .02  .00  .00  .40
#> wt    .00  .00  .00  .00  .00       .46  .00  .00  .00  .04
#>  [ 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.00                                                             
#> cyl    -.85     1                                                       
#> disp   -.86   .90   1.00                                                
#> hp     -.82   .89    .80     1                                          
#> drat    .69  -.67   -.78  -.49     1                                    
#> wt     -.88   .79    .90   .62  -.73     1                              
#>  [ reached getOption("max.print") -- omitted 5 rows ]
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29                                             
#> cyl   28  29                                         
#> disp  26  26   29                                    
#> hp    26  26   26  29                                
#> drat  27  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  .01                                   
#> wt    .00  .00  .00  .00  .00                              
#>  [ reached getOption("max.print") -- omitted 5 rows ]
#>