var2: Sample Variance for a Vector

Description Usage Arguments Value References Examples

Description

This is a faster version of base R function var. For optimal speed, use integer = TRUE if x is an integer vector and integer = FALSE otherwise. Note that for long vectors the function Var in Rfast is typically much faster than var2. Also, this function uses a one-pass variance calculation that makes it susceptible to precision issues if x values are very large.

Usage

1

Arguments

x

Integer or numeric vector.

integer

Logical value for whether x is an integer vector.

Value

Numeric value.

References

Papadakis, M., Tsagris, M., Dimitriadis, M., Fafalios, S., Tsamardinos, I., Fasiolo, M., Borboudakis, G., Burkardt, J., Zou, C. and Lakiotaki, K. (2018) Rfast: A Collection of Efficient and Extremely Fast R Functions. R package version 1.8.8. https://CRAN.R-project.org/package=Rfast.

Examples

1
2
3
4
5
6
7
8
9
# var2 vs. var for integer vector
x <- rpois(1000, lambda = 5)
all.equal(var(x), var2(x, TRUE))
benchmark(var(x), var2(x, TRUE), replications = 2000)

# var2 vs. var for numeric vector
x <- rnorm(1000)
all.equal(var(x), var2(x))
benchmark(var(x), var2(x), replications = 2000)

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