2017-06-07 57 views
1

我有一个数据帧,我想将其转换为ts并预测从DOC 3或SampleDate 2007-06-23开始的AvgWeight。 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 

这里是上面的数据集的dput被复制至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但尚未掌握的内容。我感谢任何帮助。

回答

0

我看到两个问题与您的代码:

  1. 时间序列intialization关闭 - 下一个应该工作。
  2. ggplot2::autoplot似乎并不为绘制时间序列但从forecast绘制拟合模型的函数

这样的事情对我的作品 - OP评论后更新:

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

那岂不是相当为2007年第25周开始= c(2007,25)?第三个整数23似乎无论如何都被忽略 –

+0

对文档有一丝不苟的看法 - 感谢指出@Aurèle! – CMichael

+1

对于我的模型,我想尝试ARIMA或HoltWinters,并希望制作此页面的情节:http://rpubs.com/sinhrks/basics或这一个:https://robjhyndman.com/hyndsight/forecast7-ggplot2/ 。这两个页面似乎都使用ggplot2的autoplot。看看x轴,我如何显示我的DOC值(3到101)加上我的3个预测(108,115和122)? – Salvador

相关问题