R/me_correlate_covariance.R
me_covariance.Rd
me_covariance
calculates a covariance matrix through
cov
and multiplies the diagonal with the supplied
numeric vector. It's a wrapper around cov
with slight
tweaks.
me_covariance(
.data,
diag_adj = 1,
wt = NULL,
use = "complete.obs",
method = "pearson"
)
a matrix or data frame with numeric columns.
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.
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.
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".
a character string indicating which correlation coefficient (or covariance) is to be computed. One of "pearson" (default), "kendall", or "spearman": can be abbreviated.
a covariance tibble
with variable names as a column and
the diagonal multiplied by diag_adj
# New diagonal
new_diagonal <- rnorm(ncol(mtcars))
me_covariance(mtcars)
#> # A tibble: 11 × 12
#> rowname mpg cyl disp hp drat wt qsec vs
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mpg 36.3 -9.17 -633. -321. 2.20 -5.12 4.51 2.02
#> 2 cyl -9.17 3.19 200. 102. -0.668 1.37 -1.89 -0.730
#> 3 disp -633. 200. 15361. 6721. -47.1 108. -96.1 -44.4
#> 4 hp -321. 102. 6721. 4701. -16.5 44.2 -86.8 -25.0
#> 5 drat 2.20 -0.668 -47.1 -16.5 0.286 -0.373 0.0871 0.119
#> 6 wt -5.12 1.37 108. 44.2 -0.373 0.957 -0.305 -0.274
#> 7 qsec 4.51 -1.89 -96.1 -86.8 0.0871 -0.305 3.19 0.671
#> 8 vs 2.02 -0.730 -44.4 -25.0 0.119 -0.274 0.671 0.254
#> 9 am 1.80 -0.466 -36.6 -8.32 0.190 -0.338 -0.205 0.0423
#> 10 gear 2.14 -0.649 -50.8 -6.36 0.276 -0.421 -0.280 0.0766
#> 11 carb -5.36 1.52 79.1 83.0 -0.0784 0.676 -1.89 -0.464
#> # … with 3 more variables: am <dbl>, gear <dbl>, carb <dbl>
me_covariance(mtcars, new_diagonal)
#> # A tibble: 11 × 12
#> rowname mpg cyl disp hp drat wt qsec vs
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mpg -32.6 -9.17 -633. -321. 2.20 -5.12 4.51 2.02
#> 2 cyl -9.17 -3.49 200. 102. -0.668 1.37 -1.89 -0.730
#> 3 disp -633. 200. 20104. 6721. -47.1 108. -96.1 -44.4
#> 4 hp -321. 102. 6721. -5755. -16.5 44.2 -86.8 -25.0
#> 5 drat 2.20 -0.668 -47.1 -16.5 -0.315 -0.373 0.0871 0.119
#> 6 wt -5.12 1.37 108. 44.2 -0.373 0.674 -0.305 -0.274
#> 7 qsec 4.51 -1.89 -96.1 -86.8 0.0871 -0.305 -1.77 0.671
#> 8 vs 2.02 -0.730 -44.4 -25.0 0.119 -0.274 0.671 -0.119
#> 9 am 1.80 -0.466 -36.6 -8.32 0.190 -0.338 -0.205 0.0423
#> 10 gear 2.14 -0.649 -50.8 -6.36 0.276 -0.421 -0.280 0.0766
#> 11 carb -5.36 1.52 79.1 83.0 -0.0784 0.676 -1.89 -0.464
#> # … with 3 more variables: am <dbl>, gear <dbl>, carb <dbl>
me_covariance(mtcars, new_diagonal, method = "kendall")
#> # A tibble: 11 × 12
#> rowname mpg cyl disp hp drat wt qsec vs am gear carb
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mpg -878. -638 -752 -722 452 -714 310 414 326 340 -436
#> 2 cyl -638 -720. 654 626 -440 586 -362 -444 -282 -330 330
#> 3 disp -752 654 1283. 648 -486 730 -296 -424 -362 -374 358
#> 4 hp -722 626 648 -1183. -370 596 -462 -440 -210 -218 512
#> 5 drat 452 -440 -486 -370 -1066. -534 32 262 398 456 -82
#> 6 wt -714 586 730 596 -534 693. -140 -344 -428 -428 322
#> 7 qsec 310 -362 -296 -462 32 -140 -549. 464 -118 -72 -440
#> 8 vs 414 -444 -424 -440 262 -344 464 -236. 84 152 -358
#> 9 am 326 -282 -362 -210 398 -428 -118 84 -408. 430 -36
#> 10 gear 340 -330 -374 -218 456 -428 -72 152 430 446. 68
#> 11 carb -436 330 358 512 -82 322 -440 -358 -36 68 382.
diagonal_wout_weight <- rnorm(ncol(mtcars) - 1)
me_covariance(mtcars, diagonal_wout_weight, wt = mpg)
#> # A tibble: 10 × 11
#> rowname cyl disp hp drat wt qsec vs am
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 cyl 3.28 193. 99.0 -0.691 1.35 -1.85 -0.720 -0.494
#> 2 disp 193. -8537. 6308. -46.9 98.6 -93.5 -42.3 -37.6
#> 3 hp 99.0 6308. 872. -18.4 42.5 -80.6 -24.0 -10.3
#> 4 drat -0.691 -46.9 -18.4 -0.296 -0.375 0.128 0.119 0.198
#> 5 wt 1.35 98.6 42.5 -0.375 0.726 -0.310 -0.267 -0.353
#> 6 qsec -1.85 -93.5 -80.6 0.128 -0.310 -3.59 0.675 -0.189
#> 7 vs -0.720 -42.3 -24.0 0.119 -0.267 0.675 -0.0919 0.0467
#> 8 am -0.494 -37.6 -10.3 0.198 -0.353 -0.189 0.0467 -0.0378
#> 9 gear -0.650 -47.8 -7.68 0.259 -0.402 -0.275 0.0667 0.279
#> 10 carb 1.55 77.0 75.9 -0.119 0.670 -1.82 -0.473 -0.0123
#> # … with 2 more variables: gear <dbl>, carb <dbl>