Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cova

Fast covariance and correlation matrix calculation


Description

Fast covariance and correlation matrix calculation.

Usage

cova(x, center = FALSE)

cora(x)

Arguments

x

A matrix with data. It has to be matrix, if it is data.frame for example the function does not turn it into a matrix.

center

If you want to center the data prior to applying the cross product of the mateix set this equal to TRUE, otherwise leave it NULL.

Details

The calculations take place faster than the built-in functions cor as the number of variables increases. For a few tens of variables. This is true if the number of variables is high, say from 500 and above. The "cova" on the other hand is always faster. For the "cova" in specific, we have an option to center the data prior to the cross product. This can be more stable if you have many tens of thousands of rows due to numerical issues that can arise. It is sligtly slower.

For the correlation matrix we took the code from here

https://stackoverflow.com/questions/18964837/fast-correlation-in-r-using-c-and-parallelization/18965892#18965892

Value

The covariance or the correlation matrix.

Author(s)

Michail Tsagris

R implementation and documentation: Michail Tsagris <mtsagris@yahoo.gr> and Manos Papadakis <papadakm95@gmail.com>.

See Also

Examples

x <- matrnorm(100, 40)
s1 <- cov(x) 
s2 <- cova(x)
all.equal(s1, s2)
x <- NULL

Rfast

A Collection of Efficient and Extremely Fast R Functions

v2.0.1
GPL (>= 2.0)
Authors
Manos Papadakis, Michail Tsagris, Marios Dimitriadis, Stefanos Fafalios, Ioannis Tsamardinos, Matteo Fasiolo, Giorgos Borboudakis, John Burkardt, Changliang Zou, Kleanthi Lakiotaki and Christina Chatzipantsiou.
Initial release
2020-09-13

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.