【问题标题】:R applying growth rate backwardsR向后应用增长率
【发布时间】:2015-07-30 06:30:03
【问题描述】:

我的数据包含两个不同的月度序列和一年重叠

ts1 <- ts(cumsum(rnorm(120,.1,1)), start = 1995, frequency = 12)
ts2 <- ts(cumsum(rnorm(120,.2,1)), start = 2004, frequency = 12)

它们没有相同的水平(2004 年进行了重新调整),但由于重叠,人们可以使用第一个的月增长率来反向预测第二个直到 1995 年。

我想创建一个变量 ts_series,它具有 2004 年之后的 ts2 水平,然后使用 ts1 的月增长率对其进行反向投影。我在 zoo 对象中有几个这样的系列,所以我可以使用 zoo 方法或将它们分组到列表中并使用 mapply。

非常感谢

【问题讨论】:

    标签: r zoo


    【解决方案1】:

    这是一种方法,对重叠位使用简单的线性回归来识别两个系列之间的关系,然后将该模型应用于ts1 的非重叠部分以估计ts2 的早期值。最后一步为您提供了一个新的ts 对象,该对象表示ts2 在非重叠期间的预测值。

    # Make the toy data
    set.seed(1)
    ts1 <- ts(cumsum(rnorm(120,.1,1)), start = 1995, frequency = 12)
    ts2 <- ts(cumsum(rnorm(120,.2,1)), start = 2004, frequency = 12)
    # Now do the estimation
    x <- as.vector(window(ts1, start = c(2004,1), end = c(2004,12)))
    y <- as.vector(window(ts2, start = c(2004,1), end = c(2004,12)))
    tsmod <- lm(y ~ x)
    ts2preds <- predict(tsmod, newdata = as.data.frame(window(ts1, start = c(1995,1), end = c(2003,12))))
    ts2prior <- ts(data = ts2preds, start = c(1995, 1), end = c(2003, 12), frequency = 12)
    

    不过,如果您想自己回溯 ts2,Rob Hyndman 在他的 forecast 包中为您提供了 forecast() 函数。从他的博客关注an example

    library(forecast)
    f <- frequency(ts2) # Identify the frequency of your ts
    h <- (start(ts2)[1] - start(ts1)[1]) * f # Set the number of periods you want to backcast
    revx <- ts(rev(ts2), frequency = f) # Reverse time in the series you want to backcast
    ts2plus <- forecast(auto.arima(revx), h)  # Do the backcasting
    # Reverse its elements
    ts2plus$mean <- ts(rev(ts2plus$mean), end=tsp(ts2)[1] - 1/f, frequency=f)
    ts2plus$upper <- ts2plus$upper[h:1,]
    ts2plus$lower <- ts2plus$lower[h:1,]
    ts2plus$x <- ts2 # Replace the reversed reference series in the prediction object with the original one
    # Plot it
    plot(ts2plus, xlim=c(tsp(ts2)[1]-h/f, tsp(ts2)[2]))
    

    这是产生的情节:

    这两个系列的比较如下:

    > cor(ts2plus$mean, ts2preds)
    [1] 0.9760174
    

    如果您的主要目标是获得这些早期值的最佳点预测,您可以考虑运行这两个版本并平均它们的结果。然后这变成了一个非常简单的多模型集合预测(或回溯)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      相关资源
      最近更新 更多