Learn R Programming

raster (version 2.2-31)

corrLocal: Local correlation coefficient

Description

Local correlation coefficient for two RasterLayer objects

Usage

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

Arguments

x
RasterLayer
y
RasterLayer
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

  • numeric

See Also

cor, cor.test

Examples

Run this code
b <- stack(system.file("external/rlogo.grd", package="raster"))
b[[2]] <- flip(b[[2]], 'y')

x <- corLocal(b[[1]], b[[2]] )
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])

Run the code above in your browser using DataLab