pwcor_pwcov_pwnobs.Rd
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, ...)
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
).
an object of class 'pwcor' / 'pwcov'.
numeric. A vector of (frequency) weights.
logical. TRUE
also computes pairwise observation counts.
logical. TRUE
also computes pairwise p-values (same as cor.test
and Hmisc::rcorr
).
logical. If N = TRUE
or P = TRUE
, TRUE
(default) returns output as 3D array whereas FALSE
returns a list of matrices.
argument passed to cor
/ cov
. If use != "pairwise.complete.obs"
, sum(complete.cases(X))
is used for N
, and p-values are computed accordingly.
integer. The number of digits to round to in print.
numeric. P-value threshold below which a '*'
is displayed above significant coefficients if P = TRUE
.
character. The part of the correlation / covariance matrix to display.
integer. Controls the spacing between different reported quantities in the printout of the matrix: 0 - compressed, 1 - single space, 2 - double space.
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
.
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.
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).
mna <- na_insert(mtcars)
pwcor(mna)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 1 -.87 -.84 -.79 .65 -.88 .40 .68 .63 .47 -.52
#> cyl -.87 1 .89 .84 -.73 .80 -.62 -.80 -.53 -.46 .51
#> disp -.84 .89 1 .76 -.64 .89 -.49 -.74 -.57 -.51 .33
#> hp -.79 .84 .76 1 -.35 .66 -.73 -.73 -.33 -.02 .73
#> drat .65 -.73 -.64 -.35 1 -.71 .03 .54 .74 .78 .01
#> wt -.88 .80 .89 .66 -.71 1 -.17 -.64 -.71 -.60 .46
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcov(mna)
#> mpg cyl disp hp drat wt qsec
#> mpg 38.76 -9.91 -619.74 -338.69 1.96 -6.16 4.33
#> cyl -9.91 3.28 190.24 107.99 -0.65 1.48 -2.07
#> disp -619.74 190.24 15145.22 6456.45 -36.01 117.00 -113.87
#> hp -338.69 107.99 6456.45 4868.00 -12.39 44.76 -98.72
#> drat 1.96 -0.65 -36.01 -12.39 0.25 -0.36 0.03
#> wt -6.16 1.48 117.00 44.76 -0.36 1.05 -0.33
#> vs am gear carb
#> mpg 2.15 1.96 2.22 -5.05
#> cyl -0.73 -0.48 -0.66 1.53
#> disp -46.99 -34.33 -46.99 67.95
#> hp -22.13 -9.45 -0.85 85.39
#> drat 0.13 0.18 0.30 0.01
#> wt -0.35 -0.37 -0.45 0.76
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwnobs(mna)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 27 27 26 26 26 26 27 26 26 26
#> cyl 27 29 26 27 26 26 26 27 26 26 26
#> disp 27 26 29 26 26 26 27 26 27 26 28
#> hp 26 27 26 29 26 26 26 26 26 26 26
#> drat 26 26 26 26 29 26 26 26 26 26 26
#> wt 26 26 26 26 26 29 27 26 26 27 26
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE)
#> mpg cyl disp hp drat wt qsec
#> mpg 1 (29) -.87 (27) -.84 (27) -.79 (26) .65 (26) -.88 (26) .40 (26)
#> cyl -.87 (27) 1 (29) .89 (26) .84 (27) -.73 (26) .80 (26) -.62 (26)
#> disp -.84 (27) .89 (26) 1 (29) .76 (26) -.64 (26) .89 (26) -.49 (27)
#> hp -.79 (26) .84 (27) .76 (26) 1 (29) -.35 (26) .66 (26) -.73 (26)
#> drat .65 (26) -.73 (26) -.64 (26) -.35 (26) 1 (29) -.71 (26) .03 (26)
#> wt -.88 (26) .80 (26) .89 (26) .66 (26) -.71 (26) 1 (29) -.17 (27)
#> vs am gear carb
#> mpg .68 (27) .63 (26) .47 (26) -.52 (26)
#> cyl -.80 (27) -.53 (26) -.46 (26) .51 (26)
#> disp -.74 (26) -.57 (27) -.51 (26) .33 (28)
#> hp -.73 (26) -.33 (26) -.02 (26) .73 (26)
#> drat .54 (26) .74 (26) .78 (26) .01 (26)
#> wt -.64 (26) -.71 (26) -.60 (27) .46 (26)
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, P = TRUE)
#> mpg cyl disp hp drat wt qsec vs am gear
#> mpg 1 -.87* -.84* -.79* .65* -.88* .40* .68* .63* .47*
#> cyl -.87* 1 .89* .84* -.73* .80* -.62* -.80* -.53* -.46*
#> disp -.84* .89* 1 .76* -.64* .89* -.49* -.74* -.57* -.51*
#> hp -.79* .84* .76* 1 -.35 .66* -.73* -.73* -.33 -.02
#> drat .65* -.73* -.64* -.35 1 -.71* .03 .54* .74* .78*
#> wt -.88* .80* .89* .66* -.71* 1 -.17 -.64* -.71* -.60*
#> carb
#> mpg -.52*
#> cyl .51*
#> disp .33
#> hp .73*
#> drat .01
#> wt .46*
#> [ reached getOption("max.print") -- omitted 5 rows ]
pwcor(mna, N = TRUE, P = TRUE)
#> mpg cyl disp hp drat wt
#> mpg 1 (29) -.87* (27) -.84* (27) -.79* (26) .65* (26) -.88* (26)
#> cyl -.87* (27) 1 (29) .89* (26) .84* (27) -.73* (26) .80* (26)
#> disp -.84* (27) .89* (26) 1 (29) .76* (26) -.64* (26) .89* (26)
#> hp -.79* (26) .84* (27) .76* (26) 1 (29) -.35 (26) .66* (26)
#> drat .65* (26) -.73* (26) -.64* (26) -.35 (26) 1 (29) -.71* (26)
#> wt -.88* (26) .80* (26) .89* (26) .66* (26) -.71* (26) 1 (29)
#> qsec vs am gear carb
#> mpg .40* (26) .68* (27) .63* (26) .47* (26) -.52* (26)
#> cyl -.62* (26) -.80* (27) -.53* (26) -.46* (26) .51* (26)
#> disp -.49* (27) -.74* (26) -.57* (27) -.51* (26) .33 (28)
#> hp -.73* (26) -.73* (26) -.33 (26) -.02 (26) .73* (26)
#> drat .03 (26) .54* (26) .74* (26) .78* (26) .01 (26)
#> wt -.17 (27) -.64* (26) -.71* (26) -.60* (27) .46* (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 -.87 -.84 -.79 .65 -.88 .40 .68 .63 .47 -.52
#> N 29 27 27 26 26 26 26 27 26 26 26
#> P .00 .00 .00 .00 .00 .04 .00 .00 .01 .01
#>
#> , , cyl
#>
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> r -.87 1 .89 .84 -.73 .80 -.62 -.80 -.53 -.46 .51
#> N 27 29 26 27 26 26 26 27 26 26 26
#> P .00 .00 .00 .00 .00 .00 .00 .01 .02 .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 -.868* (27) 1 (29)
#> disp -.841* (27) .890* (26) 1 (29)
#> hp -.788* (26) .838* (27) .758* (26) 1 (29)
#> drat .650* (26) -.731* (26) -.644* (26) -.346 (26) 1 (29)
#> wt -.879* (26) .800* (26) .892* (26) .663* (26) -.710* (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 -.87 -.84 -.79 .65 -.88 .40 .68 .63 .47 -.52
#> cyl -.87 1 .89 .84 -.73 .80 -.62 -.80 -.53 -.46 .51
#> disp -.84 .89 1.00 .76 -.64 .89 -.49 -.74 -.57 -.51 .33
#> hp -.79 .84 .76 1.00 -.35 .66 -.73 -.73 -.33 -.02 .73
#> drat .65 -.73 -.64 -.35 1.00 -.71 .03 .54 .74 .78 .01
#> wt -.88 .80 .89 .66 -.71 1.00 -.17 -.64 -.71 -.60 .46
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29 27 27 26 26 26 26 27 26 26 26
#> cyl 27 29 26 27 26 26 26 27 26 26 26
#> disp 27 26 29 26 26 26 27 26 27 26 28
#> hp 26 27 26 29 26 26 26 26 26 26 26
#> drat 26 26 26 26 29 26 26 26 26 26 26
#> wt 26 26 26 26 26 29 27 26 26 27 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 .01 .01
#> cyl .00 .00 .00 .00 .00 .00 .00 .01 .02 .01
#> disp .00 .00 .00 .00 .00 .01 .00 .00 .01 .09
#> hp .00 .00 .00 .08 .00 .00 .00 .10 .94 .00
#> drat .00 .00 .00 .08 .00 .87 .00 .00 .00 .96
#> wt .00 .00 .00 .00 .00 .39 .00 .00 .00 .02
#> [ 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
#> cyl -.87 1
#> disp -.84 .89 1.00
#> hp -.79 .84 .76 1.00
#> drat .65 -.73 -.64 -.35 1.00
#> wt -.88 .80 .89 .66 -.71 1.00
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>
#> $N
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> mpg 29
#> cyl 27 29
#> disp 27 26 29
#> hp 26 27 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 .08
#> wt .00 .00 .00 .00 .00
#> [ reached getOption("max.print") -- omitted 5 rows ]
#>