I have a single raster for which I want to calculate the
library(r)
r <- raster(system.file("external/test.grd", package="raster"))
For the RMSE and the (absolute) mean error I need some kind of predicted values (?) how do i calculate/get them?
For the standard deviation i could just write
sd(r)
but that give me the error
Error in as.double(x) :
cannot coerce type 'S4' to vector of type 'double'
I've looked for a while and only found solutions working for raster stacks which will then return another raster. But I don't want that. For each of the 4 measurements I want a single number to return me the value of what was calculated.
Let's create data with a known standard deviation:
set.seed(69)
m <- matrix(rnorm(10000), nrow = 100)
sd(m)
#> [1] 0.9979821
Now we can turn this into a raster:
library(raster)
r <- raster(m)
r
#> class : RasterLayer
#> dimensions : 100, 100, 10000 (nrow, ncol, ncell)
#> resolution : 0.01, 0.01 (x, y)
#> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
#> crs : NA
#> source : memory
#> names : layer
#> values : -3.507427, 3.669151 (min, max)
If you want to get the original data's values you can use the data
slot:
sd(r@data@values)
#> [1] 0.9979821
Or, more compactly, use the subsetting operator []
:
sd(r[])
#> [1] 0.9979821
Created on 2020-08-21 by the reprex package (v0.3.0)