Reputation: 69
I have a raster stack (yearly time series) of land cover maps and I want to calculate the percentage of change. I have found this question, but is not clear to me. Any ideas how to do this?
Supposing that my raster stack is named r_stk
, currently the only statistics that I have calculated are the sd
and cv
.
Any ideas on how to calculate the percentage change?
r_stk_sd<-calc(r_stk, fun=sd)
r_stk_cv<-r_stk_sd/r_stk_Mean
Upvotes: 2
Views: 990
Reputation: 47536
Here is some example data. From ?raster::stack
!
library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster"))
I understand you have done this
mn <- mean(s)
sd <- calc(s, sd)
cv <- sd / mn
Now you want "% change" but you do not define it. Please edit your question, and explain better what you want to compute. Do you want local change (on a cell by cell bases); but if so, how would you compute the percentage? Or do you want it globally (one number for each layer / time period). Do you want to compare all time periods with each other? Or??
Here another possibility, compare the first layer to all other layers. Perhaps this can get you started to do what you want, or to ask a better question.
x <- s[[1]] != s[[-1]]
Are there any changed cells?
y <- any(x)
What is the total fraction of cells has changed relative to year 1?
cellStats(y, "mean")
#[1] 0.7923364
Or for each "year" relative to year 1.
cellStats(x, "mean")
# green blue
#0.6218336 0.7891218
If you want year by year change, you could do
s1 <- s[[2:nlayers(s)]]
s2 <- s[[1:(nlayers(s)-1)]]
z <- s1 != s2
cellStats(z, "mean")
# green blue
# 0.6218336 0.7672624
Upvotes: 5