简体   繁体   English

R每周时间序列对象

[英]R Weekly Time Series Object

I have the following vector, which contains data for each day of December. 我有以下向量,其中包含12月每天的数据。

vector1 <- c(1056772, 674172, 695744, 775040, 832036,735124,820668,1790756,1329648,1195276,1267644,986716,926468,828892,826284,749504,650924,822256,3434204,2502916,1262928,1025980,1828580,923372,658824,956916,915776,1081736,869836,898736,829368)

Now I want to create a time series object on a weekly basis and used the following code snippet: 现在,我想每周创建一个时间序列对象,并使用以下代码片段:

weeklyts = ts(vector1,start=c(2016,12,01), frequency=7)

However, the starting and end points are not correct. 但是,起点和终点不正确。 I always get the following time series: 我总是得到以下时间序列:

> weeklyts
Time Series:
Start = c(2017, 5) 
End = c(2021, 7) 
Frequency = 7 
 [1] 1056772  674172  695744  775040  832036  735124  820668 1790756 1329648 1195276 1267644  986716  926468  828892  826284  749504
[17]  650924  822256 3434204 2502916 1262928 1025980 1828580  923372  658824  956916  915776 1081736  869836  898736  829368

Does anybody nows what I am doing wrong? 有人现在我做错了吗?

To get a timeseries that starts and ends as you would expect, you need to think about the timeserie. 要获得一个按您期望的开始和结束的时间序列,您需要考虑时间序列。 You have 31 days from december 2016. 从2016年12月起,您有31天的时间。

The timeserie start option handles 2 numbers, not 3. So something like c(2016, 1) if you start with month 1 in 2016. See following example. 时间开始选项仅处理2个数字,而不处理3个数字。因此,如果您从2016年的第1个月开始,则类似于c(2016,1)。请参见以下示例。

ts(1:12, start = c(2016, 1), frequency = 12) 
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2016   1   2   3   4   5   6   7   8   9  10  11  12

Now ts and daily data is an annoyance. 现在,ts和每日数据令人烦恼。 ts cannot handle leap years. ts无法处理leap年。 That is why you see people using a frequency of 365.25 to get an annual timeseries. 这就是为什么您看到人们使用365.25的频率获取年度时间序列的原因。 To get a good december 2016 series we can do the following: 为了获得良好的2016年12月系列,我们可以执行以下操作:

ts(vector1, start = c(2016, 336), frequency = 366)
Time Series:
Start = c(2016, 336) 
End = c(2016, 366) 
Frequency = 366 
 [1] 1056772  674172  695744  775040  832036  735124  820668 1790756 1329648 1195276 1267644  986716  926468  828892  826284  749504
[17]  650924  822256 3434204 2502916 1262928 1025980 1828580  923372  658824  956916  915776 1081736  869836  898736  829368

Note the following things that are going on: 请注意以下情况:

  1. Frequence is 366 because 2016 is a leap year 频率为366,因为2016年是a年
  2. start is c(2016, 336), because 336 is the day in the year on "2016-12-01" start是c(2016,336),因为336是“ 2016-12-01”上的一年中的一天

Personally I use xts package (and zoo) to handle daily data and use the functions in xts to aggregate to weekly timeseries. 我个人使用xts包(和动物园)来处理每日数据,并使用xts中的功能汇总到每周时间序列。 These can then be used with packages that like ts timeseries like forecast. 然后,可以将它们与类似ts时间序列之类的软件包一起使用。

edit: added small xts example 编辑:添加了小的xts示例

my_df <- data.frame(dates = seq.Date(as.Date("2016-12-01"), as.Date("2017-01-31"), by = "day"),
                    var1 = rep(1:31, 2))

library(xts)
my_xts <- xts(my_df[, -1], order.by = my_df$dates)

# rollup to weekly. Dates shown are the last day in the weekperiod.
my_xts_weekly <- period.apply(my_xts, endpoints(my_xts, on = "weeks"), colSums)
head(my_xts_weekly)
           [,1]
2016-12-04   10
2016-12-11   56
2016-12-18  105
2016-12-25  154
2017-01-01  172
2017-01-08   35

Depending on your needs you can transform this back into data.frames etc etc. Read the help for period.apply as you can specify your own functions in the rolling mechanism. 根据您的需要,您可以将其转换回data.frames等。阅读period.apply的帮助,因为您可以在滚动机制中指定自己的功能。 And read the xts (and zoo) vignettes. 并阅读xts(和动物园)小插图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM