This is a follow up to Using spei() function on time series from rasterstack in R?
I am trying to use the spei() function from the SPEI package to calculate drought metrics. I have a rasterstack of 30 years of monthly data, so 360 layers. I want to run spei() once on historic climate data to calculate the coefficients for fitting the SPEI index. Then I want to apply those coefficients to new monthly data for recent and future climate to actually calculate SPEI (fitted) values on those data. So, @Kamo helped me in the previous Q&A to run spei() and return a rasterstack.
library(SPEI)
library(raster)
# Generate a sample raster stack time series with 360 layers
r <- raster(nrows=10,ncols=10,vals=rnorm(100))
rstack <- stack(r)
for(i in 1:359){
rstack <- stack(rstack, raster(nrows=10,ncols=10,vals=rnorm(100)))
cat(paste("..",round(((i+1)/360)*100,1),"%")) # check progress in %
}
# Change the function SPEI so it outputs a numeric vector of coefficients
# Modified from @Kamo
funSPEI <- function(x, scale=2, na.rm=TRUE) {
as.numeric((spei(x, scale=scale, na.rm=na.rm))$coefficients)
}
rst.coeff <- calc(rstack, fun = funSPEI)
Now I want to use these coefficients as the param argument in spei(). According to spei documentation (https://cran.r-project.org/web/packages/SPEI/SPEI.pdf), user specified params "should be a three-dimensional array with dimensions (3,number of series in data,12), containing twelve parameter triads (xi, alpha, kappa) for each data series, one for each month".
Instead of 12 parameter triads, what I have is a raster stack of 36 layers (3 parameters times 12 months). How can I use that as input to spei()?
I have tried:
funSPEI.2 <- function (x, scale=2, na.rm=TRUE, p) {
as.numeric((spei(x, scale=scale, na.rm=na.rm, p))$fitted)
}
rstFit <- calc(rstack, fun = funSPEI.2, params = rst.coeff))
But I get the error:
#Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function