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   -.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
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
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
#>             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
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
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
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
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)
#>             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)
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
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*
#>        carb
#> mpg   -.53*
#> cyl    .55*
#> disp   .48*
#> hp     .68*
#> drat  -.14 
#> wt     .41*
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
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         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)
#>  [ reached 'max' / 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  -.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
#> r  -.89   .91    1   .87
#> 
#>  [ reached 'max' / getOption("max.print") -- omitted 8 slices ] 
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          vs          am        gear        carb
#> mpg                                                             
#> cyl                                                             
#> disp                                                            
#> hp                                                              
#> drat                                                            
#> wt                                                              
#>  [ reached 'max' / 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  -.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
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
#> 
#> $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
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
#> 
#> $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
#>  [ reached 'max' / 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                                                             
#> 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                               
#>  [ reached 'max' / 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    26  27   26  29                                
#> drat  27  26   26  26   29                           
#> wt    26  26   26  27   26  29                       
#>  [ reached 'max' / 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  .02                                   
#> wt    .00  .00  .00  .00  .00                              
#>  [ reached 'max' / getOption("max.print") -- omitted 5 rows ]
#>