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

Tim Appelhans tim.appelhans at gmail.com
Sat Feb 27 07:47:40 CET 2016


Dear Thiago,
have a look at the gimms package, available on CRAN. The function of 
interest is significantTau().

https://github.com/environmentalinformatics-marburg/gimms/blob/master/R/significantTau.R

Hope this helps,
Tim

On 27.02.2016 09:49, Thiago V. dos Santos wrote:
> 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:
>
> --------------------------------
> library(rkt)
>
> # 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)
> kenn
>
> --------------------------------
>
> Now, this is my attempt to apply this test on a raster stack, using a calc function:
>
> --------------------------------
> library(raster)
>
> # 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)
> s
>
> # Now I define a function
> tsfun <- function(x) {
>      year <- as.numeric(substr(getZ(x), 1, 4))
>      r.kenn <- rkt(year, x)
>      return(r.kenn)
> }
>
> # 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?
> Greetings,
>   -- Thiago V. dos Santos
>
> PhD student
> Land and Atmospheric Science
> University of Minnesota
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
#####################################
Tim Appelhans
Department of Geography
Environmental Informatics
Philipps Universität Marburg
Deutschhausstraße 12
Raum 00A08
35032 Marburg (Paketpost: 35037 Marburg)
Germany

Tel +49 (0) 6421 28-25957

http://environmentalinformatics-marburg.de/



More information about the R-sig-Geo mailing list