简体   繁体   English

每日时间序列分析

[英]Daily Time Series Analysis

I have a daily time series about the sales of a product, my series start from 01/01/2016 until 31/08/2017, my problem is that I do not know what value of frequency I should use, considering that it is a six-day week (my week starts on Monday and ends Saturday) and there is no data for Sundays. 我有一个有关产品销售的每日时间序列,我的序列从2016年1月1日开始到2017年8月31日,我的问题是我不知道应该使用哪个频率值,因为这是一个六天的一周(我的一周从星期一开始,到星期六结束),没有星期日的数据。

Should be it like this ? 应该是这样吗?

myts <- ts(sales, start=c(2016, 1), frequency=6)

Thanks for your help !! 谢谢你的帮助 !!

ts expects you to have values for each element of the time-series, ie, it would expect you to have the seventh day values in the data. ts希望您具有时间序列中每个元素的值,即,希望您在数据中具有第七天的值。

One option is to expand the date index to include your missing observations. 一种选择是扩展日期索引以包括您丢失的观察值。 You could fill those missing observations with na.approx or na , but you can't give ts a six day week and expect it to comprehend it as a seven day cycle. 您可以使用na.approxna来填充那些缺失的观测值,但是您不能给ts六天的工作周,并希望它能将其理解为一个七天的周期。

A good way to do this is to look at zoo , which has specific functions for dealing with these sorts of situations. 做到这一点的一种好方法是看zoo ,它具有处理这类情况的特定功能。

It really depends on what you want to do with the data. 这实际上取决于您要对数据执行的操作。

1) plot for example, if your objective is simply to plot it then "ts" class is not a good fit since it is not good at representing dates. 1)例如, 绘制 ,如果您的目标只是绘制它,则"ts"类不合适,因为它不擅长表示日期。 Try this instead where we have defined test vector for sales and tt in the Note at the end. 请改用此方法,我们在末尾的注释中定义了salestt测试向量。

library(zoo)
z <- zoo(sales, tt)
plot(z)

2) acf If you want to compute the autocorrelation function then using the plain vector sales or ts(sales) would be fine: 2)acf如果要计算自相关函数,则可以使用纯向量salests(sales)

acf(sales)

3) StructTS If you want to fit a structural time series using StructTS then you will need to decide on the length of a cycle, ie does it repeat every week? 3)StructTS如果要使用StructTS拟合结构时间序列,则需要确定一个周期的长度,即每周重复一次吗? quarter? 25美分硬币? year?. 年?。 Typically an annual cycle is appropriate for sales but, in general, you will need two complete cycles to do anything so you don't really have enough data for that. 通常情况下,一个年度周期适合销售,但是总的来说,您将需要两个完整的周期才能执行任何操作,因此您实际上没有足够的数据来执行此操作。

4) monthly/quarterly If you are willing to reduce it to monthly or quarterly data then you could use ts but you only have 20 points for monthly or 7 for quarterly. 4)每月/每季度如果您愿意将其简化为每月或每季度数据,则可以使用ts但您每月只有20点,每季度只有7点。 Here we have used the last point in each month: 在这里,我们使用了每个月的最后一点:

library(zoo)

z <- zoo(sales, tt)
zm <- aggregate(z, as.yearmon, tail, 1) 
tsm <- as.ts(zm)
tsm

giving: 赠送:

          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
2016 3.258097 3.931826 4.356709 4.644391 4.867534 5.049856 5.204007 5.342334
2017 5.828946 5.897154 5.968708 6.030685 6.093570 6.150603 6.204558 6.257668
          Sep      Oct      Nov      Dec
2016 5.459586 5.564520 5.659482 5.749393
2017                                    

5) weekly Another thing you could consider would be to use weekly series by just using Saturday, for example: 5)每周您可以考虑的另一件事是仅通过使用星期六使用每周系列,例如:

library(zoo)
z <- zoo(sales, tt)
zw <- z[weekdays(time(z)) == "Saturday"]

Note: We used this dummy data: 注意:我们使用了以下虚拟数据:

set.seed(123)
tt <- seq(as.Date("2016-01-01"), as.Date("2017-08-31"), "day")
tt <- tt[! weekdays(tt) == "Sunday"]
n <- length(tt)
sales <- log(1:n)

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

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