layerCor | R Documentation |
Compute correlation, (weighted) covariance, or similar summary statistics that compare the values of all pairs of the layers of a SpatRaster.
## S4 method for signature 'SpatRaster'
layerCor(x, fun, w, asSample=TRUE, use="everything", maxcell=Inf, ...)
x |
SpatRaster |
fun |
character. The statistic to compute: either "cov" (covariance), "weighted.cov" (weighted covariance), or "cor" (pearson correlation coefficient) or your own function that takes two vectors as argument to compute a single number |
w |
SpatRaster with the weights to compute the weighted covariance. It should have a single layer and the same geometry as |
asSample |
logical. If |
use |
character. To decide how to handle missing values. This must be (an abbreviation of) one of "everything", "complete.obs", "pairwise.complete.obs", "masked.complete". With "pairwise.complete.obs", the value for a pair of layers is computed for all cells that are not |
maxcell |
positive integer. The maximum number of cells to be used. If this is smaller than ncell(x), a regular sample of |
... |
additional arguments for |
If fun
is one of the three standard statistics, you get a list with three items: the
correlation or (weighted) covariance matrix, the (weighted) means, and the number of data cells in
each comparison. The means are also a matrix because they may depend on the combination of layers
if different cells have missing values and these are excluded from the computation. The rows of
the mean matrix represent the layer whose (weighted) mean is being calculated and the columns
represent the layer it is being paired with. Only cells with non-missing observations for both
layers are used in the calculation of the (weighted) mean. The diagonals of the mean and n
matrices are set to missing.
If fun
is a function, you get a single 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.
global
, cov.wt
, weighted.mean
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.