用年间隔

时间:2017-09-21 14:02:21

标签: r time-series

我的数据超过17年,我想在ts()年级制作时间序列。它在1,3和6个月的间隔内工作得非常好,但是当我每年尝试时,我都会收到错误。

以下是我的例子:

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")

有解决这个问题的方法吗?

我已经读过几个月你需要至少24个月才能对其进行建模。多年来我应该拥有相同的最小值吗?如果他们帮助解决这个问题,我也愿意使用其他工具......

我的目标最终目标是使用:

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 个答案:

答案 0 :(得分:1)

要有更多变化的例子:

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)

我担心你不能以时间间隔&gt; =年来分解ts的季节性。它只是反对季节性定义,而stldecompose在这里没什么可做的。在这种情况下,您可以做的是分解趋势循环模式,而不是季节性mFilter中有几种平滑/过滤方法,易于实现。例如Hodrick Prescott过滤:

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

enter image description here

更多&#39;幻想&#39;在包wavelets中使用时间序列的多分辨率分析的方法。此方法可以为您提供分解的一系列平滑值(S),残差循环模式(D1,D2,...,DJ) 。这个包没有默认的图表方法,所以还有更多工作要做 - 只是一点点:

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

享受!

相关问题