cov2: Sample Covariance for 2 Vectors

Description Usage Arguments Value Examples

Description

This is a faster version of base R function cov. For optimal speed, use integer = TRUE if x and y are integer vectors and integer = FALSE otherwise. This function uses a one-pass covariance calculation that makes it susceptible to precision issues if x values are very large.

Usage

1
cov2(x, y, integer = FALSE)

Arguments

x, y

Integer or numeric vector.

integer

Logical value for whether x and y are integer vectors.

Value

Numeric value.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# cov2 vs. cov for integer vector
x <- rpois(1000, lambda = 5)
y <- rpois(1000, lambda = 5)
all.equal(cov(x, y), cov2(x, y, integer = TRUE))
benchmark(cov(x, y), cov2(x, y, integer = TRUE), replications = 5000)

# cov2 vs. cov for numeric vector
x <- rnorm(1000)
y <- rnorm(1000)
all.equal(cov(x, y), cov2(x, y))
benchmark(cov(x, y), cov2(x, y), replications = 5000)

vandomed/crowdopt documentation built on May 26, 2019, 5:35 a.m.