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

corLocal

Local correlation coefficient


Description

Local correlation coefficient for two RasterLayer objects (using a focal neighborhood) or for two RasterStack or Brick objects (with the same number of layers (> 2))

Usage

## S4 method for signature 'RasterLayer,RasterLayer'
corLocal(x, y, ngb=5, 
     method=c("pearson", "kendall", "spearman"), test=FALSE, filename='', ...)


## S4 method for signature 'RasterStackBrick,RasterStackBrick'
corLocal(x, y,  
     method=c("pearson", "kendall", "spearman"), test=FALSE, filename='', ...)

Arguments

x

RasterLayer or RasterStack/RasterBrick

y

object of the same class as x, and with the same number of layers

ngb

neighborhood size. Either a single integer or a vector of two integers c(nrow, ncol)

method

character indicating which correlation coefficient is to be used. One of "pearson", "kendall", or "spearman"

test

logical. If TRUE, return a p-value

filename

character. Output filename (optional)

...

additional arguments as for writeRaster

Value

RasterLayer

Note

NA values are omitted

See Also

Examples

b <- stack(system.file("external/rlogo.grd", package="raster"))
b <- aggregate(b, 2, mean)

set.seed(0)
b[[2]] <- flip(b[[2]], 'y') + runif(ncell(b))
b[[1]] <- b[[1]] + runif(ncell(b))

x <- corLocal(b[[1]], b[[2]], test=TRUE )
# plot(x)

# only cells where the p-value < 0.1
xm <- mask(x[[1]], x[[2]] < 0.1, maskvalue=FALSE)
plot(xm)


# for global correlation, use the cor function
x <- as.matrix(b)
cor(x, method="spearman")
 
# use sampleRegular for large datasets
x <- sampleRegular(b, 1000)
cor.test(x[,1], x[,2])

# RasterStack or Brick objects
y <- corLocal(b, flip(b, 'y'))

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

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