[R-sig-Geo] Error running Mann-Kendall trend test on a raster stack

Thiago V. dos Santos thi_veloso at yahoo.com.br
Fri Feb 26 21:49:40 CET 2016

Dear colleagues,

I have a raster stack with 89 layers, each layer representing yearly precipitation.

I am trying to use the function rkt (from package "rkt") to detect a possible trend in my precipitation time series.

Its usage is pretty simple, and this is how it runs on a data frame:


# generate some random data
myDF <- data.frame(Date = seq.Date(as.Date("2011-01-01"), as.Date("2099-01-01"), by='year'),
value = runif(89,0,1200))

# extract year from date, as numeric
myDF$year <- as.numeric(format(myDF$Date, "%Y"))

# run mann-kendall test
kenn <- rkt(myDF$year, myDF$value)


Now, this is my attempt to apply this test on a raster stack, using a calc function:


# Create the date sequence
idx <- myDF$Date

# Create raster stack and apply the date
r <- raster(ncol=50, nrow=50)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r), 0, 1200))))
s <- setZ(s, idx)

# Now I define a function
tsfun <- function(x) {
    year <- as.numeric(substr(getZ(x), 1, 4))
    r.kenn <- rkt(year, x)

# I apply the function
raster.kenn <- calc(s, fun=tsfun)


And the error message I get is:

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : 
cannot use this function

What am I doing wrong here?
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota

More information about the R-sig-Geo mailing list