# Statistical comparison between different rasters using R?

I need to compare, statistically, different rasters with a pattern raster (in this case a Revised Universal Soil Loss Equation (RUSLE) raster). I used different methods to calculate the environment vulnerability, and now, I need to calculate which method is closer to the RUSLE raster. I thought about subtracting each method raster to the RUSLE raster and then compare the means of the resulting rasters.

Anyone have any idea using R?

I thought about subtracting each method raster to the RUSLE raster and then compare the means of the resulting rasters.

That is not a great approach as the mean can be zero, but the errors very large. Root Mean Square Error is perhaps the most common statistic used for comparisons like this.

To illustrate, I create two RasterLayer objects with random numbers, and one layer with the RUSLE data:

``````library(raster)
r <- raster(ncol=10, nrow=10)
set.seed(1)
r1 <- setValues(r, runif(ncell(r)))
r2 <- setValues(r, runif(ncell(r)))
rusle <- setValues(r, runif(ncell(r)))
``````

We need a function to compute RMSE

``````RMSE <- function(x, y) { sqrt(mean((x - y)^2)) }
``````

Now use it:

``````RMSE(values(rusle), values(r1))
#[1] 0.3651395
RMSE(values(rusle), values(r2))
#[1] 0.3942261
``````

The winner is `r1` (but not by much)

• Thank you so much for the explanation and the tutorial, it'll help me a lot!! Now, I'm gonna start study the R to make this analysis. Just one more question, this command 'r1 <- setValues(r, runif(ncell(r)))', I won't need to do that, I'll just need to load the rasters, right? – Bruno Dec 19 '17 at 13:47
• Yes, that is correct. I am creating example data to show how you can approach it; and to show that the code works (at least with these example data). – Robert Hijmans Dec 19 '17 at 18:29
• Again, thank you very much!! I'll work on this data in the next days and provide a feedback here! – Bruno Dec 19 '17 at 21:09

R package diffeR is a good way to start. If you want to compute correlation among 2 rasters use function layerStats() from package raster.

• Thank for the answer! I have searched on google about this R packages, and it looked really complicated to use. I don't have any notion in the software and using codes. Do you know any good tutorial that can help me? – Bruno Dec 18 '17 at 19:31
• There are tutorials for R and for spatial data with R here: rspatial.org – Robert Hijmans Dec 19 '17 at 3:23