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.

  • 3
    Well, let's stop and think about this for a second. The example you provide is for a time-series n=t(1..i). With two rasters, for any given pixel, you have an n=2. How can you base a correlation on two observations? Now, do you want a correlation based on a moving window or for the entire raster? A moving window correlation would allow for creating a new raster of the correlation coefficients but would be highly dependent on the size or shape of the focal window. – Jeffrey Evans Feb 2 '16 at 20:10
  • 2
    Hi Jeffrey, thanks for the answer. The example I gave in my previous message was more to show the kind of output I " hope " I could get. I'm fully aware that this is for time-series. What I basically want to do is to compare two rasters for the same geographic areas done in two different ways. Looking if there regions that are similar in both maps or completely different, and check how significant is the difference for example. What I have done so far is doing one minus the other but I cannot say that it's statistically significant. – Jaber Belkhiria Feb 2 '16 at 22:09
  • 2
    Computing the Spearman correlation will tell you little. To determine statistical significance it would help to have an explicit model of the sources of variation between the two rasters. To compare the rasters, it is far better to look at a map of their differences and move on from there. But, as @JeffreyEvans notes, a "pixel by pixel" correlation makes no sense and is not possible to compute. – whuber Feb 2 '16 at 23:57
  • The link appears to be broken. If a code snippet that you tried and were stuck on had been included as formatted text within your question body then this question would have retained much more value. – PolyGeo Oct 4 at 20:08

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
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')
  • @JaberBelkhiria if this solved your problem, you should mark it as the solution. – Paul H Feb 18 '16 at 21:22
  • Fails because d does not exist when you do d[] = runif(....). Did d come from somewhere else? – Spacedman Jul 4 '17 at 12:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.