【问题标题】:Dual seasonal cycles in ts objectts 对象中的双季节循环
【发布时间】:2014-01-26 10:27:03
【问题描述】:

我想从 ts 中去除季节性。这个特定的 ts 是每天的,并且有每年和每周的季节性周期(频率 365 和 7)。

为了消除两者,我尝试在频率设置为 365 的 ts 上执行 stl(),然后提取趋势和余数,并将新 ts 的频率设置为 7,然后重复。

这似乎不是很好,我想知道这是我的方法,还是 ts 固有的东西导致我出现问题。任何人都可以批评我的方法,或者推荐一种替代方法吗?

【问题讨论】:

    标签: r time-series decomposition stl-decomposition


    【解决方案1】:

    使用forecast 包中实现的 TBATS 模型有一个非常简单的方法。这是一个假设您的数据存储为x的示例:

    library(forecast)
    x2 <- msts(x, seasonal.periods=c(7,365))
    fit <- tbats(x2)
    x.sa <- seasadj(fit)
    

    模型的详细信息在De Livera, Hyndman and Snyder (JASA, 2011)中描述。

    【讨论】:

    • 非常感谢您的回复,罗布。你的预测包是最好的,看起来我只是触及了皮毛。您答案中的 seasadj(fit) 行返回了一个我正在努力解释的错误,也许您可​​以帮忙? "[.default(comp, , "season") 中的错误:下标越界"
    • 你能给我一个最小的例子来复制错误吗?此功能相对较新且未经过充分测试。它适用于我尝试的示例。将错误报告发送至github.com/robjhyndman/forecast/issues?state=open
    【解决方案2】:

    stl() 是一种不仅可以处理季节性因素(周期性重复发生的事件)而且还可以处理趋势(常态的缓慢变化)的方法,这一点令人赞叹,具体由 Rob J Hyndman 实施。

    Hyndman 提供的 decomp 函数(转载如下)非常有助于检查 seasonalitydecomposing 到季节性(如果存在)、trendresidual 组件的时间序列。

    decomp <- function(x,transform=TRUE)
    {
      #decomposes time series into seasonal and trend components
      #from http://robjhyndman.com/researchtips/tscharacteristics/
      require(forecast)
      # Transform series
      if(transform & min(x,na.rm=TRUE) >= 0)
      {
        lambda <- BoxCox.lambda(na.contiguous(x))
        x <- BoxCox(x,lambda)
      }
      else
      {
        lambda <- NULL
        transform <- FALSE
      }
      # Seasonal data
      if(frequency(x)>1)
      {
        x.stl <- stl(x,s.window="periodic",na.action=na.contiguous)
        trend <- x.stl$time.series[,2]
        season <- x.stl$time.series[,1]
        remainder <- x - trend - season
      }
      else #Nonseasonal data
      {
        require(mgcv)
        tt <- 1:length(x)
        trend <- rep(NA,length(x))
        trend[!is.na(x)] <- fitted(gam(x ~ s(tt)))
        season <- NULL
        remainder <- x - trend
      }
      return(list(x=x,trend=trend,season=season,remainder=remainder,
        transform=transform,lambda=lambda))
    }
    

    如您所见,如果存在季节性,则使用stl()(使用loess),如果没有季节性,则使用惩罚回归样条。

    【讨论】:

      【解决方案3】:

      检查这是否有用:
      Start and End Values depends on your Data - Change the Frequency values accordingly

      splot <- ts(Data1, start=c(2010, 2), end=c(2013, 9), frequency=12)
      

      additive trend, seasonal, and irregular components can be decomposed using the stl() Function

      fit <- stl(splot, s.window="period")
      monthplot(splot) 
      library(forecast)
      vi <-seasonplot(splot)
      

      vi 应为季节性指数提供单独的值

      还请检查以下一项:

      splot.stl <- stl(splot,s.window="periodic",na.action=na.contiguous)
          trend <- splot.stl$time.series[,2]
          season <- splot.stl$time.series[,1]
          remainder <- splot - trend - season
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 2014-04-02
        • 2021-06-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-12
        • 2021-02-11
        相关资源
        最近更新 更多