Compute correlation and (weighted) covariance for multi-layer Raster objects. Like cellStats this function returns a few values, not a Raster* object (see Summary-methods for that).

layerStats(x, stat, w, asSample=TRUE, na.rm=FALSE, ...)

## Arguments

x

RasterStack or RasterBrick for which to compute a statistic

stat

Character. The statistic to compute: either 'cov' (covariance), 'weighted.cov' (weighted covariance), or 'pearson' (correlation coefficient)

w

RasterLayer with the weights (should have the same extent, resolution and number of layers as x) to compute the weighted covariance

asSample

Logical. If TRUE, the statistic for a sample (denominator is n-1) is computed, rather than for the population (denominator is n)

na.rm

Logical. Should missing values be removed?

...

## Value

List with two items: the correlation or (weighted) covariance matrix, and the (weighted) means.

## Author

Jonathan A. Greenberg & Robert Hijmans. Weighted covariance based on code by Mort Canty

cellStats, cov.wt, weighted.mean

## Examples

b <- brick(system.file("external/rlogo.grd", package="raster"))
layerStats(b, 'pearson')
#> $pearson correlation coefficient #> red green blue #> red 1.0000000 0.9980961 0.9501633 #> green 0.9980961 1.0000000 0.9658011 #> blue 0.9501633 0.9658011 1.0000000 #> #>$mean
#>      red    green     blue
#> 182.2855 185.3509 192.8046
#>

layerStats(b, 'cov')
#> $covariance #> red green blue #> red 5564.371 5443.405 4993.165 #> green 5443.405 5345.403 4974.478 #> blue 4993.165 4974.478 4962.942 #> #>$mean
#>      red    green     blue
#> 182.2855 185.3509 192.8046
#>

# weigh by column number
w <- init(b, v='col')
layerStats(b, 'weighted.cov', w=w)
#> $weigthed covariance #> red green blue #> red 5670.750 5536.351 5009.851 #> green 5536.351 5427.161 4987.092 #> blue 5009.851 4987.092 4937.007 #> #>$weighted mean
#>  layer.1  layer.2  layer.3
#> 177.5983 181.3521 191.5236
#>