3
votes

I have data spanning over 17 years, and I would like to make a time series at the year level with ts(). It works very fine for 1,3 and 6 month intervals, but when I try per year I get an error.

Here is my example:

date <- c("2000-01-01", "2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01", "2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01")
var <- c(1:17)
df <- data.frame(as.Date(date), var)
ts_df <- ts(df$var,start=c(2000,1),frequency=1)
stl_df <- stl(ts_df, s.window = "periodic")

Is there a way around this problem?

I've read that for months you need a minimum of 24 months to model it. Should I have the same minimum amount of values for years too? I'm open to using other tools too if they help solve this...

My goal ultimate goal is to use:

autoplot(cbind(Data=ts_df,Seasonal=seasonal(stl_df),
               Trend=trendcycle(stl_df)), 
         facets=TRUE)

date <- c("2000-01-01", "2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01", "2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01")
var <- c(1:17)
df <- data.frame(as.Date(date), var)
ts_df <- ts(df$var,start=c(2000,1),frequency=1)
stl_df <- stl(ts_df, s.window = "periodic")
1

1 Answers

4
votes

To have more varied example:

date <- seq(as.Date("2000-01-01"),as.Date("2016-01-01"), by="1 year")
var <- cumsum(rnorm(17))

df <- data.frame(date = as.Date(date), var)
ts_df <- ts(df$var,start=c(2000,1),frequency=1)

I'm afraid you can't decompose seasonality from ts with time intervals >= year. It's just against the seasonality definition, and stl or decompose have not much to do here. What you can do in this case is decompose trend and cyclical pattern instead of seasonality. There are several smoothing/filtering methods in mFilter which is easy to implement. For example Hodrick Prescott filtering:

library(mFilter)
filtered <- mFilter(ts_df,filter="HP")
print(filtered)
summary(filtered)
residuals(filtered)
fitted(filtered)
plot(filtered)

enter image description here

And more 'fancy' method which uses Multiresolution Analysis Of Time Series in package wavelets. This method can provide you decomposed series of smoothed values (S), residuals and cyclical patterns (D1,D2,..., DJ). This package does not have default charting method so there is more to do - just a little:

library(waveslim)
library(ggplot2)
library(reshape2)
filtered <- mra(ts_df,J=2)

results <- as.data.frame(filtered)
results <- cbind(df,results)

melt(results, id.var="date") %>%
  ggplot(aes(x=date,y=value,group=variable)) +
  geom_line() +
  facet_wrap(~variable)

enter image description here

Enjoy!