I have 36 layers of raster stack(annual composits of rainfall over specified region). When I tried to compute Sen's slope by using the following code:
library(raster)
library(trend)
# example data
s <- stack(system.file("external/rlogo.grd", package="raster"))
s <- stack(s, s* 2, s*3)
func <- function(x) { unlist(sens.slope(x)) }
sen.slop <- calc(s, fun=func)
It returns the following error
Error in .local(x, values, ...) :
values must be numeric, integer or logical.
Is there anybody who can help me to resolve the problem?
sens.slope returns an object of class htest
that includes numeric values, but also character values. To make a Raster, you need to select the numeric values you want. Eg :
library(raster)
library(trend)
s <- stack(system.file("external/rlogo.grd", package="raster"))
s <- stack(s, s* 2, s*3)
func <- function(x) { unlist(sens.slope(x)[1:3]) }
sen.slop <- calc(s, fun=func)
The thing to understand is that before supplying your own function to calc
you should inspect its behavior. For example, compare:
set.seed(9);
v <- runif(100) * 1:100
# original function
func <- function(x) { unlist(sens.slope(x)) }
func(v)
# estimates.Sen's slope statistic.z p.value null.value.z alternative data.name method parameter.n
# "0.40383510858131" "6.6084411517969" "3.88387866698504e-11" "0" "two.sided" "x" "Sen's slope" "100"
# Yikes! character output.
... with what is returned by this function
func <- function(x) { unlist(sens.slope(x)[1:3]) }
func(v)
# estimates.Sen's slope statistic.z p.value
# 4.038351e-01 6.608441e+00 3.883879e-11
# better!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.