准备输入数据以运行bfastSpatial和bfast

huangapple go评论137阅读模式
英文:

Preparing input data to run bfastSpatial & bfast

问题

我想要识别整个NDVI时间序列的断点,然后显示断点的幅度和频率。我的输入数据是以.tif格式存储的每日NDVI时间序列。每个图像都以其日期命名(20031002,20031101,20031201,...)。我使用以下代码来准备我的数据,但遇到了一个错误。在此提前感谢您的时间。

setwd(".../ndvi")
list.grid <- list.files(pattern = ".tif$", recursive = TRUE)
d <- c("20000101","20000102","20000103","20000104","20000105", …) 
saveRDS(d, file = "dates.rds")
date_NDVI <- readRDS("dates.rds")
NDVI_S <- raster::stack(list.grid)
NDVI_S <- setZ(NDVI_S, as.Date(date_NDVI$date), name="time")
bfmSpatial(NDVI_S, start=c(2000,1), order=1, mc.cores=8, returnLayers = c("breakpoint", "magnitude", "error"))

错误信息:在date_NDVI$date上使用$操作符无效。

英文:

I want to identify breakpoints for the whole NDVI time series and then show magnitude and frequency of the breakpoints. My input data is a daily NDVI time series in .tif format. Each image is named by its date (20031002, 20031101, 20031201, …). I use this code to prepare my data but I am faced with an error. In advance, I appreciate your time.

setwd(&quot;…/ndvi&quot;)
list.grid&lt;-list.files(pattern = &quot;.tif$&quot;, recursive = TRUE)
d &lt;- c(&quot;20000101&quot;,&quot;20000102&quot;,&quot;20000103&quot;,&quot;20000104&quot;,&quot;20000105&quot;, …) 
saveRDS(d, file = &quot;dates.rds&quot;)
date_NDVI&lt;-readRDS(&quot;dates.rds&quot;)
NDVI_S &lt;- raster::stack(list.grid)
NDVI_S&lt;-setZ(NDVI_S,as.Date(date_NDVI$date),name=&quot;time&quot;)
bfmSpatial(NDVI_S, start=c(2000,1), order=1, mc.cores=8,returnLayers = c(&quot;breakpoint&quot;, &quot;magnitude&quot;, &quot;error&quot;))

Error in date_NDVI$date : $ operator is invalid for atomic vectors

答案1

得分: 0

你发布的错误意味着date_NDVI已经是一个向量,所以你不需要使用$运算符来访问日期(如果date_NDVI是一个data.frame的情况下需要使用)。然后,你只需要指定日期的格式(使用as.Date),并将它们设置为Z值(使用setZ)。最后,你应该能够运行bfast分析。

library(raster)
library(bfastSpatial)

setwd("../ndvi")
list.grid <- list.files(pattern = ".tif$", recursive = TRUE)
d <- c("20000101","20000102","20000103","20000104","20000105", ...)
NDVI_S <- raster::stack(list.grid)
NDVI_S <- setZ(NDVI_S, as.Date(d, format = "%Y%m%d"), name="time")
# 检查起始日期和mc.cores值(Windows操作系统不接受mc.cores > 1)
resul <- bfmSpatial(NDVI_S, start=c(2005,1), order=1, mc.cores=8, returnLayers = c("breakpoint", "magnitude", "error"))
英文:

The error you posted means that date_NDVI is already a vector, so you do not need to use an $ operator to access the dates (this would be the case if date_NDVI was a data.frame). Then, you just need to indicate the format in which the dates are given (using as.Date) and set them as a Z value (using setZ). Finally, you shoud be able to run the bfast analysis.

library(raster)
library(bfastSpatial)

setwd(&quot;…/ndvi&quot;)
list.grid &lt;- list.files(pattern = &quot;.tif$&quot;, recursive = TRUE)
d &lt;- c(&quot;20000101&quot;,&quot;20000102&quot;,&quot;20000103&quot;,&quot;20000104&quot;,&quot;20000105&quot;, …) 
NDVI_S &lt;- raster::stack(list.grid)
NDVI_S &lt;- setZ(NDVI_S, as.Date(d, format = &quot;%Y%m%d&quot;), name=&quot;time&quot;)
# Check the start date and mc.cores values (windows OS do not accept mc.cores &gt;1) 
resul &lt;- bfmSpatial(NDVI_S, start=c(2005,1), order=1, mc.cores=8,returnLayers = c(&quot;breakpoint&quot;, &quot;magnitude&quot;, &quot;error&quot;))

huangapple
  • 本文由 发表于 2023年7月13日 19:26:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定