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   -.86  -.87  -.77   .59  -.86   .45   .64   .57   .48  -.59
#> cyl   -.86    1    .91   .82  -.74   .81  -.62  -.82  -.55  -.55   .56
#> disp  -.87   .91    1    .78  -.70   .91  -.44  -.71  -.60  -.51   .50
#> hp    -.77   .82   .78    1   -.37   .70  -.70  -.69  -.17  -.13   .79
#> drat   .59  -.74  -.70  -.37    1   -.68   .16   .60   .67   .64  -.04
#> wt    -.86   .81   .91   .70  -.68    1   -.19  -.57  -.72  -.65   .60
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcov(mna)
#>            mpg       cyl      disp        hp      drat        wt      qsec
#> mpg      36.18     -9.26   -662.79   -329.80      1.56     -5.43      4.80
#> cyl      -9.26      3.28    203.20    102.60     -0.68      1.43     -1.98
#> disp   -662.79    203.20  16103.10   6728.84    -48.65    127.22   -101.75
#> hp     -329.80    102.60   6728.84   4779.25    -11.49     44.68    -89.49
#> drat      1.56     -0.68    -48.65    -11.49      0.28     -0.38      0.16
#> wt       -5.43      1.43    127.22     44.68     -0.38      1.04     -0.33
#>             vs        am      gear      carb
#> mpg       2.01      1.70      2.30     -5.73
#> cyl      -0.77     -0.49     -0.74      1.63
#> disp    -46.02    -37.14    -52.55    100.40
#> hp      -22.86     -6.08     -7.00     86.42
#> drat      0.16      0.17      0.25     -0.03
#> wt       -0.31     -0.35     -0.50      0.78
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwnobs(mna)
#>      mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg   29  26   26 28   26 26   26 26 26   26   27
#> cyl   26  29   26 26   26 26   26 27 27   26   26
#> disp  26  26   29 26   26 26   27 26 26   26   26
#> hp    28  26   26 29   26 26   26 26 26   26   27
#> drat  26  26   26 26   29 26   26 26 27   26   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) -.87 (26) -.77 (28)  .59 (26) -.86 (26)  .45 (26)
#> cyl  -.86 (26)   1  (29)  .91 (26)  .82 (26) -.74 (26)  .81 (26) -.62 (26)
#> disp -.87 (26)  .91 (26)   1  (29)  .78 (26) -.70 (26)  .91 (26) -.44 (27)
#> hp   -.77 (28)  .82 (26)  .78 (26)   1  (29) -.37 (26)  .70 (26) -.70 (26)
#> drat  .59 (26) -.74 (26) -.70 (26) -.37 (26)   1  (29) -.68 (26)  .16 (26)
#> wt   -.86 (26)  .81 (26)  .91 (26)  .70 (26) -.68 (26)   1  (29) -.19 (26)
#>             vs        am      gear      carb
#> mpg   .64 (26)  .57 (26)  .48 (26) -.59 (27)
#> cyl  -.82 (27) -.55 (27) -.55 (26)  .56 (26)
#> disp -.71 (26) -.60 (26) -.51 (26)  .50 (26)
#> hp   -.69 (26) -.17 (26) -.13 (26)  .79 (27)
#> drat  .60 (26)  .67 (27)  .64 (26) -.04 (27)
#> wt   -.57 (26) -.72 (26) -.65 (26)  .60 (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*  -.87*  -.77*   .59*  -.86*   .45*   .64*   .57*   .48*
#> cyl   -.86*    1     .91*   .82*  -.74*   .81*  -.62*  -.82*  -.55*  -.55*
#> disp  -.87*   .91*    1     .78*  -.70*   .91*  -.44*  -.71*  -.60*  -.51*
#> hp    -.77*   .82*   .78*    1    -.37    .70*  -.70*  -.69*  -.17   -.13 
#> drat   .59*  -.74*  -.70*  -.37     1    -.68*   .16    .60*   .67*   .64*
#> wt    -.86*   .81*   .91*   .70*  -.68*    1    -.19   -.57*  -.72*  -.65*
#>        carb
#> mpg   -.59*
#> cyl    .56*
#> disp   .50*
#> hp     .79*
#> drat  -.04 
#> wt     .60*
#>  [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE, P = TRUE)
#>             mpg        cyl       disp         hp       drat         wt
#> mpg    1   (29) -.86* (26) -.87* (26) -.77* (28)  .59* (26) -.86* (26)
#> cyl  -.86* (26)   1   (29)  .91* (26)  .82* (26) -.74* (26)  .81* (26)
#> disp -.87* (26)  .91* (26)   1   (29)  .78* (26) -.70* (26)  .91* (26)
#> hp   -.77* (28)  .82* (26)  .78* (26)   1   (29) -.37  (26)  .70* (26)
#> drat  .59* (26) -.74* (26) -.70* (26) -.37  (26)   1   (29) -.68* (26)
#> wt   -.86* (26)  .81* (26)  .91* (26)  .70* (26) -.68* (26)   1   (29)
#>            qsec         vs         am       gear       carb
#> mpg   .45* (26)  .64* (26)  .57* (26)  .48* (26) -.59* (27)
#> cyl  -.62* (26) -.82* (27) -.55* (27) -.55* (26)  .56* (26)
#> disp -.44* (27) -.71* (26) -.60* (26) -.51* (26)  .50* (26)
#> hp   -.70* (26) -.69* (26) -.17  (26) -.13  (26)  .79* (27)
#> drat  .16  (26)  .60* (26)  .67* (27)  .64* (26) -.04  (27)
#> wt   -.19  (26) -.57* (26) -.72* (26) -.65* (26)  .60* (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  -.86  -.87  -.77   .59  -.86   .45   .64   .57   .48  -.59
#> N     29    26    26    28    26    26    26    26    26    26    27
#> P          .00   .00   .00   .00   .00   .02   .00   .00   .01   .00
#> 
#> , , cyl
#> 
#>     mpg cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> r  -.86   1   .91   .82  -.74   .81  -.62  -.82  -.55  -.55   .56
#> N    26  29    26    26    26    26    26    27    27    26    26
#> P   .00       .00   .00   .00   .00   .00   .00   .00   .00   .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  -.864* (26)   1    (29)                                                
#> disp -.866* (26)  .911* (26)   1    (29)                                    
#> hp   -.774* (28)  .824* (26)  .782* (26)   1    (29)                        
#> drat  .591* (26) -.736* (26) -.705* (26) -.372  (26)   1    (29)            
#> wt   -.862* (26)  .808* (26)  .912* (26)  .705* (26) -.684* (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  -.86  -.87   -.77    .59  -.86   .45    .64   .57    .48  -.59
#> cyl    -.86     1   .91    .82   -.74   .81  -.62   -.82  -.55   -.55   .56
#> disp   -.87   .91     1    .78   -.70   .91  -.44   -.71  -.60   -.51   .50
#> hp     -.77   .82   .78   1.00   -.37   .70  -.70   -.69  -.17   -.13   .79
#> drat    .59  -.74  -.70   -.37   1.00  -.68   .16    .60   .67    .64  -.04
#> wt     -.86   .81   .91    .70   -.68     1  -.19   -.57  -.72   -.65   .60
#>  [ reached getOption("max.print") -- omitted 5 rows ]
#> 
#> $N
#>      mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> mpg   29  26   26  28   26  26   26  26  26   26   27
#> cyl   26  29   26  26   26  26   26  27  27   26   26
#> disp  26  26   29  26   26  26   27  26  26   26   26
#> hp    28  26   26  29   26  26   26  26  26   26   27
#> drat  26  26   26  26   29  26   26  26  27   26   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  .02  .00  .00  .01  .00
#> cyl   .00       .00  .00  .00  .00  .00  .00  .00  .00  .00
#> disp  .00  .00       .00  .00  .00  .02  .00  .00  .01  .01
#> hp    .00  .00  .00       .06  .00  .00  .00  .41  .54  .00
#> drat  .00  .00  .00  .06       .00  .44  .00  .00  .00  .85
#> wt    .00  .00  .00  .00  .00       .36  .00  .00  .00  .00
#>  [ 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    -.86     1                                                         
#> disp   -.87   .91     1                                                   
#> hp     -.77   .82   .78   1.00                                            
#> drat    .59  -.74  -.70   -.37   1.00                                     
#> wt     -.86   .81   .91    .70   -.68     1                               
#>  [ 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  26   29                                    
#> hp    28  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  .06                                   
#> wt    .00  .00  .00  .00  .00                              
#>  [ reached getOption("max.print") -- omitted 5 rows ]
#>