As a new coder to the world of Stack Exchange/Stack Overflow I am unable to comment directly on a question - so this is a specific question in relation to Use spei() function with user specified parameters from a rasterstack in R?
In jk22 very useful answer (which I am also unable to upvote as I have no reputation yet):
library(SPEI)
library(raster)
library(zoo)
# Generate a sample raster stack time series with 720 layers
# This would be 60 years of monthly data
r <- raster(nrows=10,ncols=10,vals=rnorm(100))
rstack <- stack(r)
for(i in 1:719){
rstack <- stack(rstack, raster(nrows=10,ncols=10,vals=rnorm(100)))
cat(paste("..",round(((i+1)/720)*100,1),"%")) # check progress in %
}
r.mat <- as.matrix(rstack)
# Run spei()
funSPEImat <- function(x, sc, start, end, na.rm=TRUE,...) {
dat <- ts(x, start = c(1971, 1), end = c(2030, 12), frequency = 12)
as.numeric((spei(dat, sc, ref.start = start, ref.end = end, na.rm=na.rm, ...))$fitted)
}
fitted.mat <- t(apply(r.mat, 1, funSPEImat, sc = 2, start = c(1971, 1),
end = c(2000, 12)))
# Convert back to raster brick
spei <- setValues(rstack, fitted.mat)
dates <- seq(as.Date("1971-01-01"), as.Date("2030-12-31"), by="month")
names(spei) <- as.yearmon(dates)
It is my understanding that the section below is the function that generates the Standardised Precipitation Evapotranspiration Index (SPEI):
# Run spei()
funSPEImat <- function(x, sc, start, end, na.rm=TRUE,...) {
dat <- ts(x, start = c(1971, 1), end = c(2030, 12), frequency = 12)
as.numeric((spei(dat, sc, ref.start = start, ref.end = end, na.rm=na.rm, ...))$fitted)
}
And this next part of the code is to generate the reference period (in this case January 1971 to December 2000):
fitted.mat <- t(apply(r.mat, 1, funSPEImat, sc = 2, start = c(1971, 1),
end = c(2000, 12)))
My specific question is: what does the sc = 2 part relate to in the reference period code above?
Further comments - as I am not sure I will be able to reply to anyones answer (as I have no reputation yet):
I can not find any reference to "sc =" in the documentation (Package ‘SPEI’). I guess this is for the scale - but there is already a "1" scale reference following "r.mat" in the above code which I think would make this a 1 monthly spei. "sc" does feature in the function that generates the SPEI but would this mean that there is a 2 monthly spei generated which uses a 1 monthly reference period?