1

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?

3
  • It is being passed as the second argument in the spirt function so just look at the functions help and there will be a description May 31, 2021 at 14:08
  • Stupid spell check..."second argument of the spei function". You can bring up help using ?spei May 31, 2021 at 14:57
  • I got it thanks @JeffreyEvans May 31, 2021 at 17:59

2 Answers 2

1

The help for this function starts its description like this:

spei(data, scale, kernel = list(type ='rectangular', shift = 0),....

In R, arguments get matched by position and by name if called with a name=value argument.

The name of the thing holding the value (sc in this case) is irrelevant. The following are equivalent:

sc = 99
z = 99
# match by position
spei(mydata, sc, .....)
# match by name, but in the same positions:
spei(data=mydata, scale=sc, .....)
# match by name, different positions:
spei(scale=z, data=mydata, .....)
# doesn't matter what the name of the thing with the value is:
spei(mydata, z, ......)
spei(mydata, scale=z,.....)
spei(mydata, ....., scale=z)
spei(scale=z, data=mydata, ......)
0

Thanks to the comments and answer received It made me realise that my Further Comments assumptions within my question are wrong. The "which I think would make this a 1 monthly spei" part is incorrect. The 1 after r.mat below actually relates to the apply function;

fitted.mat <- t(apply(r.mat, 1, funSPEImat, sc = 2, start = c(1971, 1), 
                                     end = c(2000, 12)))

Where;

apply(X, MARGIN, FUN, …)

MARGIN relates to "a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns" (see apply function).

So, in the example within the question the 1 in the "fitted.mat" is linked to the apply function not the spei function i.e. applies the function to a matrix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.