I have two rasters of the same area. The only difference is that they were created using two different methods. I would like to compare them and see how correlated they are pixel by pixel. Any ideas how to do this in R or how to do a spearman correlation. I wanted to do something similar to this (http://www.hakimabdi.com/20150101/an-r-function-to-test-pixelwise-correlation-between-two-time-series-of-raster-data/) with the result presented as a third raster.
Answer
Here are two approaches to compute correlation coefficients with Raster objects (notwithstanding the comments on your question about utility; that I concur with).
# generate some data
library(raster)
set.seed(89)
b <- brick(system.file("external/rlogo.grd", package="raster"))
d <- b
d[] <- runif(ncell(b)*nlayers(b))
s <- stack(b[[1:2]], d[[1:2]], b[[3]], d[[3]])
# let's say we have two Raster objects with three layers
x <- s[[1:3]]
y <- s[[4:6]]
# stack these
z <- stack(x,y)
r <- calc(z, fun=function(x) cor(x[1:3], x[4:6], method='spearman'))
## Now, to do local correlation with two layers
r1 <- b[[1]]
r2 <- b[[2]]
rc <- corLocal(r1, r2, method='spearman')
No comments:
Post a Comment