parCov
efficiently calculates the covariance of Raster* objects,
taking advantage of parallel processing and pulling data into memory only as
necessary. For large datasets with lots of variables, calculating the covariance
matrix rapidly becomes unwieldy, as the number of calculations required grows
quadratically with the number of variables.
parCov(x, y, ...)# S4 method for Raster,missing
parCov(
x,
w = NULL,
sample = TRUE,
progress = FALSE,
parallel = FALSE,
n = 1,
cl = NULL,
keep.open = FALSE
)
# S4 method for Raster,Raster
parCov(
x,
y,
w = NULL,
sample = TRUE,
progress = FALSE,
parallel = FALSE,
n = 1,
cl = NULL,
keep.open = FALSE
)
Raster* object, typically a brick or stack
NULL (default) or a Raster* object with the same extent and resolution
as x
additional arguments, including any of the following:
optional Raster* object of weights for a weighted covariance matrix
logical. If TRUE
, the sample covariance is calculated
with a denominator of $n-1$
logical. If TRUE
, messages and progress bar will be
printed
logical. If TRUE
then multiple cores are utilized
numeric. Number of CPU cores to utilize for parallel processing
optional cluster object
logical. If TRUE
and parallel = TRUE
, the
cluster object will not be closed after the function has finished
Returns a matrix with the same row and column names as the layers of
x
. If y
is supplied, then the covariances between the layers
of x
and the layers of codey are computed.
This function is designed to work similarly to the
cov
and the layerStats
functions, with two major differences. First, parCov
allows you to
calculate the covariance between two different Raster* objects, whereas
layerStats
does not. Second, parCov
can (optionally) compute
each element of the covariance matrix in parallel, offering a dramatic
improvement in computation time for large Raster* objects.
The raster layer of weights w
should contain raw weights as values,
and should not be normalized so that sum(w) = 1
. This is
necessary for computing the sample covariance, whose formula contains
sum(w) - 1
in its denominator.
# NOT RUN {
mat1 <- parCov(climdat.hist)
# correlation matrix
Z <- parScale(climdat.hist)
mat2 <- parCov(Z)
# covariance between two Raster* objects
mat3 <- parCov(x = climdat.hist, y = climdat.fut)
# }
Run the code above in your browser using DataLab