me_correlate calculates a correlation matrix through cor and multiplies the diagonal with the supplied numeric vector. It's a wrapper around cor with slight tweaks.

me_correlate(
  .data,
  diag_adj = 1,
  wt = NULL,
  use = "complete.obs",
  method = "pearson"
)

Arguments

.data

a matrix or data frame with numeric columns.

diag_adj

a numeric vector with length equal to the number of columns of x to be multiplied by the diagonal. Alternatively, it can be of length 1 which will be repeated through the whole diagonal. If the argument wt is used, then the length of diag_adj must be the same as x excluding the weight column. By default it multiplies by 1, giving the same diagonal.

wt

the name of the column which contains the weights as bare unquoted names or as character vector or length 1. Note that when the weight argument is specified, the estimation is done using cov.wt instead of cor or cov. This means that the arguments use and method are ignored.

use

an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".

method

a character string indicating which correlation coefficient (or covariance) is to be computed. One of "pearson" (default), "kendall", or "spearman": can be abbreviated.

Value

a correlation tibble with variable names as a column and the diagonal multiplied by diag_adj

See also

cor for the workhorse behind the function and cov.wt for the function used for the weighting.

Examples


# New diagonal
new_diagonal <- rnorm(ncol(mtcars))

me_correlate(mtcars)
#> # A tibble: 11 × 12
#>    rowname    mpg    cyl   disp     hp    drat     wt    qsec     vs      am
#>    <chr>    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl>   <dbl>
#>  1 mpg      1     -0.852 -0.848 -0.776  0.681  -0.868  0.419   0.664  0.600 
#>  2 cyl     -0.852  1      0.902  0.832 -0.700   0.782 -0.591  -0.811 -0.523 
#>  3 disp    -0.848  0.902  1      0.791 -0.710   0.888 -0.434  -0.710 -0.591 
#>  4 hp      -0.776  0.832  0.791  1     -0.449   0.659 -0.708  -0.723 -0.243 
#>  5 drat     0.681 -0.700 -0.710 -0.449  1      -0.712  0.0912  0.440  0.713 
#>  6 wt      -0.868  0.782  0.888  0.659 -0.712   1     -0.175  -0.555 -0.692 
#>  7 qsec     0.419 -0.591 -0.434 -0.708  0.0912 -0.175  1       0.745 -0.230 
#>  8 vs       0.664 -0.811 -0.710 -0.723  0.440  -0.555  0.745   1      0.168 
#>  9 am       0.600 -0.523 -0.591 -0.243  0.713  -0.692 -0.230   0.168  1     
#> 10 gear     0.480 -0.493 -0.556 -0.126  0.700  -0.583 -0.213   0.206  0.794 
#> 11 carb    -0.551  0.527  0.395  0.750 -0.0908  0.428 -0.656  -0.570  0.0575
#> # … with 2 more variables: gear <dbl>, carb <dbl>
me_correlate(mtcars, new_diagonal)
#> # A tibble: 11 × 12
#>    rowname    mpg    cyl   disp     hp    drat      wt    qsec     vs      am
#>    <chr>    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>  <dbl>   <dbl>
#>  1 mpg      1.56  -0.852 -0.848 -0.776  0.681  -0.868   0.419   0.664  0.600 
#>  2 cyl     -0.852 -1.12   0.902  0.832 -0.700   0.782  -0.591  -0.811 -0.523 
#>  3 disp    -0.848  0.902  0.961  0.791 -0.710   0.888  -0.434  -0.710 -0.591 
#>  4 hp      -0.776  0.832  0.791  0.523 -0.449   0.659  -0.708  -0.723 -0.243 
#>  5 drat     0.681 -0.700 -0.710 -0.449 -1.95   -0.712   0.0912  0.440  0.713 
#>  6 wt      -0.868  0.782  0.888  0.659 -0.712   0.0537 -0.175  -0.555 -0.692 
#>  7 qsec     0.419 -0.591 -0.434 -0.708  0.0912 -0.175  -1.10    0.745 -0.230 
#>  8 vs       0.664 -0.811 -0.710 -0.723  0.440  -0.555   0.745   1.12   0.168 
#>  9 am       0.600 -0.523 -0.591 -0.243  0.713  -0.692  -0.230   0.168 -0.985 
#> 10 gear     0.480 -0.493 -0.556 -0.126  0.700  -0.583  -0.213   0.206  0.794 
#> 11 carb    -0.551  0.527  0.395  0.750 -0.0908  0.428  -0.656  -0.570  0.0575
#> # … with 2 more variables: gear <dbl>, carb <dbl>
me_correlate(mtcars, new_diagonal, method = "kendall")
#> # A tibble: 11 × 12
#>    rowname    mpg    cyl   disp     hp    drat      wt    qsec     vs      am
#>    <chr>    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>  <dbl>   <dbl>
#>  1 mpg      1.56  -0.795 -0.768 -0.743  0.465  -0.728   0.315   0.590  0.469 
#>  2 cyl     -0.795 -1.12   0.814  0.785 -0.551   0.728  -0.449  -0.771 -0.495 
#>  3 disp    -0.768  0.814  0.961  0.666 -0.499   0.743  -0.301  -0.603 -0.520 
#>  4 hp      -0.743  0.785  0.666  0.523 -0.383   0.611  -0.473  -0.631 -0.304 
#>  5 drat     0.465 -0.551 -0.499 -0.383 -1.95   -0.547   0.0327  0.375  0.576 
#>  6 wt      -0.728  0.728  0.743  0.611 -0.547   0.0537 -0.142  -0.488 -0.614 
#>  7 qsec     0.315 -0.449 -0.301 -0.473  0.0327 -0.142  -1.10    0.658 -0.169 
#>  8 vs       0.590 -0.771 -0.603 -0.631  0.375  -0.488   0.658   1.12   0.168 
#>  9 am       0.469 -0.495 -0.520 -0.304  0.576  -0.614  -0.169   0.168 -0.985 
#> 10 gear     0.433 -0.513 -0.476 -0.279  0.584  -0.544  -0.0913  0.270  0.771 
#> 11 carb    -0.504  0.465  0.414  0.596 -0.0954  0.371  -0.506  -0.577 -0.0586
#> # … with 2 more variables: gear <dbl>, carb <dbl>

diagonal_wout_weight <- rnorm(ncol(mtcars) - 1)
me_correlate(mtcars, diagonal_wout_weight, wt = mpg)
#> # A tibble: 10 × 11
#>    rowname    cyl    disp     hp   drat     wt   qsec     vs      am   gear
#>    <chr>    <dbl>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>  <dbl>
#>  1 cyl     -0.759  0.916   0.846 -0.711  0.804 -0.579 -0.795 -0.544  -0.507
#>  2 disp     0.916  0.0617  0.813 -0.729  0.884 -0.442 -0.706 -0.625  -0.563
#>  3 hp       0.846  0.813  -0.993 -0.515  0.687 -0.686 -0.722 -0.307  -0.163
#>  4 drat    -0.711 -0.729  -0.515 -1.34  -0.730  0.131  0.430  0.714   0.662
#>  5 wt       0.804  0.884   0.687 -0.730  0.406 -0.183 -0.559 -0.734  -0.594
#>  6 qsec    -0.579 -0.442  -0.686  0.131 -0.183 -1.27   0.743 -0.208  -0.214
#>  7 vs      -0.795 -0.706  -0.722  0.430 -0.559  0.743 -0.639  0.181   0.183
#>  8 am      -0.544 -0.625  -0.307  0.714 -0.734 -0.208  0.181 -0.230   0.764
#>  9 gear    -0.507 -0.563  -0.163  0.662 -0.594 -0.214  0.183  0.764  -1.23 
#> 10 carb     0.554  0.417   0.740 -0.140  0.454 -0.652 -0.597 -0.0155  0.231
#> # … with 1 more variable: carb <dbl>