计算R中栅格的趋势

问题描述 投票:0回答:1

我想计算栅格在时间序列中的趋势,但出现错误。 下面是代码。

trend <- function(x){
 if(is.na(x[1])){rep(NA,length(x))}
 else{
summary(lm(as.numeric(x)~c(2015:2100)))$coefficients[2]
  }   
} 
data
class       : SpatRaster 
dimensions  : 40, 81, 86  (nrow, ncol, nlyr)
resolution  : 0.5, 0.5  (x, y)
extent      : 46.5, 87, 35.5, 55.5  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 
source(s)   : memory
names       :      y_2015,       y_2016,       y_2017,       y_2018,       y_2019,       y_2020, ... 
min values  : 0.00000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, 0.000000e+00, ... 
max values  : 6.49618e-10, 6.122992e-10, 5.359198e-10, 6.447496e-10, 6.671828e-10, 6.464858e-10, ... 
time (years): 2015 to 2100 

data |> app(trend) 

**Error: Not compatible with requested type: [type=list; target=double].**
r time-series raster terra
1个回答
0
投票

这是在 R 中计算栅格趋势的代码:

library(raster)

trend <- function(x) {
  if (is.na(x[1])) {
    rep(NA, length(x))
  } else {
    summary(lm(as.numeric(x) ~ c(2015:2100)))$coefficients[2]
  }
}

# Your raster data
# Assuming 'data' is your raster object

result <- calc(data, trend)

您遇到的错误“与请求的类型不兼容:[type=list; target=double]”表明“趋势”函数可能存在问题。确保您的栅格数据格式正确,并且使用“栅格”包中的

calc
函数正确应用了该函数。

© www.soinside.com 2019 - 2024. All rights reserved.