如何将数据帧转换为时间序列

时间:2017-06-07 13:59:45

标签: r ggplot2 time-series forecasting

我有一个数据框,我想转换为ts并预测AvgWeight从DOC 3或SampleDate 2007-06-23开始。每周估计AvgWeight(DOC)。我想预测最后一次DOC(101)提前3周,所以我基本上想要对DOC 108,115和122进行预测。这个列表中有人可以帮我预测下面这个小数据集吗?

      DOC AvgWeight PondName SampleDate
  1    3  1.000000   Pond01 2007-06-23
  2   10  1.666667   Pond01 2007-06-30
  3   17  2.066667   Pond01 2007-07-07
  4   24  2.275000   Pond01 2007-07-14
  5   31  3.833333   Pond01 2007-07-21
  6   38  6.200000   Pond01 2007-07-28
  7   45  7.400000   Pond01 2007-08-04
  8   52  8.500000   Pond01 2007-08-11
  9   59 10.250000   Pond01 2007-08-18
  10  66 11.100000   Pond01 2007-08-25
  11  73 13.625000   Pond01 2007-09-01
  12  80 15.200000   Pond01 2007-09-08
  13  87 16.375000   Pond01 2007-09-15
  14  94 17.800000   Pond01 2007-09-22
  15 101 21.500000   Pond01 2007-09-29

以下是要复制到R

的数据集的输入
 wt <- structure(list(DOC = c(3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 
 73, 80, 87, 94, 101), AvgWeight = c(1, 1.66666666666667, 2.06666666666667, 
 2.275, 3.83333333333333, 6.2, 7.4, 8.5, 10.25, 11.1, 13.625, 
 15.2, 16.375, 17.8, 21.5), PondName = structure(c(1L, 1L, 1L, 
 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Pond01", class = 

"factor"),  SampleDate = structure(c(1182585600, 1183190400, 
1183795200, 1184400000, 1185004800, 1185609600, 1186214400, 1186819200, 
 1187424000, 1188028800, 1188633600, 1189238400, 1189843200, 
 1190448000, 1191052800), class = c("POSIXct", "POSIXt"))), .Names = 
c("DOC", "AvgWeight", "PondName", "SampleDate"), row.names = c(NA, 15L
 ), class = "data.frame")  

wt$SampleDate <- as.Date(wt$SampleDate)
 wt

我试过了;

library(forecast)
library(ggplot2)
pond <- ts(wt$AvgWeight,start=3,frequency=52,end=101)
autoplot(pond)

但我的电话是关闭的。我正在阅读关于ts的内容,但还无法掌握它。我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我发现您的代码有两个问题:

  1. 时间序列初始化已关闭 - 下面的那个应该有效。
  2. ggplot2::autoplot似乎不是绘制时间序列的函数,而是绘制来自forecast
  3. 的拟合模型

    这样的事情对我有用 - 在OP评论后更新:

    library(forecast)
    library(ggplot2)
    pond <- ts(wt$AvgWeight,start=c(2007, 25), frequency=52)
    autoplot(forecast(auto.arima(pond)))
    autoplot(forecast(ets(pond)))