简体   繁体   English

如何使用R计算栅格堆栈上的Sen斜率

[英]How to compute Sen's slope on raster stack using R

I have 36 layers of raster stack(annual composits of rainfall over specified region). 我有36层栅格堆栈(指定区域的年降雨量)。 When I tried to compute Sen's slope by using the following code: 当我尝试使用以下代码计算Sen的斜率时:

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. sens.slope返回类htest的对象,包括数值,但也包括字符值。 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. 要理解的是,在将自己的函数提供给calc您应该检查它的行为。 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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM