2016-08-01 59 views
1

我不明白R中如何创建时间序列对象。 我有数据:data = c(101,99,97,95,93,91,89,87,85,83,81)(为了简洁起见,较小的数据集)。 这个数据是每天从2016-07-052016-07-15开始11天。根据docs,每天采样数据的频率应为7.但我不明白startend参数的值。对于start,文档说: the time of the first observation. Either a single number or a vector of two integers, which specify a natural time unit and a (1-based) number of samples into the time unit.我不明白1-based number of samples是什么意思。我试图谷歌它,但它没有帮助。为R中每日采样的数据创建时间序列R

如果我只是用2016,7作为开始和结束日期,我只是得到:

Time Series: 
Start = c(2016, 7) 
End = c(2016, 7) 
Frequency = 7 
[1] 101 

如果我使用2016,7,12016,7,11作为开始和结束日期,我仍然得到同样的输出。

我在做什么错?

回答

1

我认为最好的方法是切换到xt或动物园,因为根据另一个问题here,ts()与日常观测斗争,因为天数在几年之间变化。

+0

我使用的时间序列预测。我尝试使用'xts',它保持数据的格式我预期(如时间戳和该时间戳的值)。但是我使用xts对象调用'forecast'的输出是一个不再包含这些时间戳的ts对象。我只是看到了价值。 –

+0

解决此问题的唯一方法是通过手动添加日期返回到ts对象,如此处所述http://stackoverflow.com/a/10347205/5795592 不知道只是使用数据会更容易。带日期列的框架。 – hannes101

0

据我了解,在ts()功能单位是年。因此,这里frequency应该设置为365(每年的天数)。因此,startend也应代表天。但是,(我相信)获得时间安排的权利startend应该是从年初开始的期望时间间隔内(在您的具体情况下,分别为186和196)。在这些数字的适当性与可检查:

as.numeric(as.Date("2016-07-05") - as.Date("2016-01-01")) 
[1] 186 
as.numeric(as.Date("2016-07-15") - as.Date("2016-01-01")) 
[1] 196 

嵌入这些信息到你的代码中调用ts()应该是:

data = c(101,99,97,95,93,91,89,87,85,83,81) 
ts(data, start = c(2016, 186), end = c(2016, 196), frequency = 365) 
# which yielded 
Time Series: 
Start = c(2016, 186) 
End = c(2016, 196) 
Frequency = 365 
[1] 101 99 97 95 93 91 89 87 85 83 81 

HTH