简体   繁体   English

R-年和周的每日数据和时间序列

[英]R - Daily data and Time Series by year and week

Hi I am new to R and this time series forecasting. 嗨,我是R和这个时间序列预测的新手。

I have a sample data of sales by day for past 3 years and I would like to use this data set to produce plot to find seasonality and pattern. 我有过去3年中每天销售的样本数据,我想使用此数据集来制作图以查找季节和样式。

My daily data format is like eg.. 我的日常数据格式例如。

Date, Sales  
2010-01-01, 5 
2010-01-03, 3 
2010-01-04, 2 
.. 
2011-12-01, 4
..
2014-11-01, 1

What I want to see is similar to below plot but by week and year using ts function. 我想看到的内容与下图相似,但使用ts函数按周和年显示。 Also, due to leap year some year has 53 weeks and some 52 weeks, any idea how this taken into account when plotting ? 另外,由于leap年,有些年份有53周和52周,您知道在绘图时如何考虑到这一点吗?

Playing with this ts function is not easy to me so it will be great if someone could help with this .. 玩这个ts函数对我来说并不容易,因此如果有人可以提供帮助,那将是很好的..

在此处输入图片说明

You should start by creating a ts object. 您应该首先创建一个ts对象。 Check ?ts for the syntax, but assuming your data above were stored in `data', it's basically 检查?ts的语法,但假设上面分别存放在`数据的数据”,它基本上是

tsData <- ts(data, start=c(2010,1), frequency=365)

where start refers to the (year, month) and frequency is the number of samples per year. start是指(年,月), frequency是每年的样本数。 Then you can use plot.ts() to plot the entire time series 然后,您可以使用plot.ts()绘制整个时间序列

plot.ts(tsData)

To extract seasonal patterns or trends, you can use the decompose() function. 要提取季节性模式或趋势,可以使用decompose()函数。

decompose(tsData)

Here is an sample 这是一个样本

x <- 1:10
y <- 11:20
plot(x, y)
lines(x,y)

On your data sales and date. 关于您的数据销售和日期。

You can replace y with date and x with sales. 您可以将y替换为日期,将x替换为sales。 If you still have issue, plz post it to me. 如果仍然有问题,请将它发布给我。

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

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