bfast2 / bfast

Breaks For Additive Season and Trend
https://bfast2.github.io
GNU General Public License v2.0
41 stars 15 forks source link

Error when running bfastlite using a monthly time-series matrix as input #110

Closed nikosGeography closed 8 months ago

nikosGeography commented 8 months ago

I have 12 raster images which I stack them. Then I converted the rasterstack to a matrix, I remove the NA's from the matrix and I convert the matrix to monthly time-series matrix. Finally, I run the bfastlite function, like so:

library(bfast)
library(raster)

wd <- "path"

l <- list.files(paste0(wd), pattern='.tif$', 
                all.files=TRUE, full.names=FALSE)

r <- raster::stack(paste0(wd, l))

m <- raster::as.matrix(r)
m <- m[!rowSums(is.na(m)), ]

# convert the matrix to timeseries matrix
tm <- ts(data = m, start = c(2019, 1), end = c(2019, 12), frequency = 12, class = "ts")
class(tm)

bf <- bfastlite(
  tm,
  formula = response ~ trend + harmon,
  order = 3,
  breaks = "LWZ",
  na.action = na.omit,
  stl = "trend",
  decomp = "stl",
  sbins = 1,
  h = 0.5,
  level = 0.5,
  type = "OLS-MOSUM"
)

And the error:

Error in stats::stl(x, s.window = "periodic") : 
  series is not periodic or has less than two periods

I have tried many variations in the parameters of the bfastlite function.

Any ideas what that error means and how I can overcome it? The dataset can be downloaded from my GoogleDrive.

RStudio 2023.12.0+369, R 4.3.2, Windows 11.

GreatEmerald commented 8 months ago

The error means that your time series input does not make sense for seasonality. Your formula response ~ trend + harmon means that you are fitting both seasonality over the year and also trend over the time series, but you only have a singe year worth of data. That makes it impossible to fit seasonality; you need at least three years for it.

nikosGeography commented 8 months ago

I see. I have monthly data for 4 years (so far). I will use them all and I will update this post with the results. Is it okay if we leave the post open until then?

nikosGeography commented 8 months ago

Indeed, when I imported monthly data for 9 years, the error disappeared.