[R-sig-Geo] Error running Mann-Kendall trend test on a raster stack
Loïc Dutrieux
loic.dutrieux at wur.nl
Sat Feb 27 12:16:39 CET 2016
Hi Thiago,
A few calc tips inline that you can use together with Tim's suggestion.
On 02/27/2016 07:47 AM, Tim Appelhans wrote:
> 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)
>> }
You can't call getZ() on x. Within the calc function environment x is a
numeric vector not a RasterBrick.
If you do it outside of the function and refer to it within the function
it should work.
dates <- getZ(s) # then use the dates variable in the function.
You can only store numerics in raster cells, and r.kenn is a full
object. You need to choose which numerics contained in that object you
want the function to return and subset the object accordingly.
Functions passed to calc should work on numeric vectors and ideally they
should also be vectorized. It's not always easy to vectorize a function
though; so in such case you can set the forceapply= argument to TRUE. I
believe that may cost a bit of speed though.
Cheers,
Loïc
>>
>> # 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
Always loved that very informative error message from calc :)
>>
>> 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
>
More information about the R-sig-Geo
mailing list