Last chance! 50% off unlimited learning
Sale ends in
Compute correlation, (weighted) covariance, or similar summary statistics that compare the values of all pairs of the layers of a SpatRaster.
# S4 method for SpatRaster
layerCor(x, fun, w, asSample=TRUE, na.rm=FALSE, maxcell=Inf, ...)
SpatRaster
character. The statistic to compute: either "cov" (covariance), "weighted.cov" (weighted covariance), or "pearson" (correlation coefficient) or your own function that takes two vectors as argument to comptue a single number
SpatRaster with the weights to compute the weighted covariance. It should have a single layer and the same geometry as x
logical. If TRUE
, the statistic for a sample (denominator is n-1
) is computed, rather than for the population (denominator is n
). Only for the standard functions
logical. Should missing values be removed?
postive integer. The number of cells to be regularly sampled. Only used when fun
is a function
additional arguments for fun
(if it is a proper function)
If fun
is one of the three standard statistics, you get a list with two items: the correlation or (weighted) covariance matrix, and the (weighted) means.
If fun
is a function, you get a matrix.
For the weighted covariance:
Canty, M.J. and A.A. Nielsen, 2008. Automatic radiometric normalization of multitemporal satellite imagery with the iteratively re-weighted MAD transformation. Remote Sensing of Environment 112:1025-1036.
Nielsen, A.A., 2007. The regularized iteratively reweighted MAD method for change detection in multi- and hyperspectral data. IEEE Transactions on Image Processing 16(2):463-478.
# NOT RUN {
b <- rast(system.file("ex/logo.tif", package="terra"))
layerCor(b, "pearson")
layerCor(b, "cov")
# weigh by column number
w <- init(b, fun="col")
layerCor(b, "weighted.cov", w=w)
# }
Run the code above in your browser using DataLab