简体   繁体   English

时间序列每日数据建模

[英]Time series daily data modeling

I am looking to forecast my time series.我正在寻找预测我的时间序列。 I have the following period daily data 2021-Jan-1 to 2022-Jul-1.我有以下期间的每日数据 2021-Jan-1 到 2022-Jul-1。 So I have a column of observations for each day.所以我每天都有一个观察专栏。 what I tried so far:到目前为止我尝试了什么:

d1=zoo(data, seq(from = as.Date("2021-01-01"), to = as.Date("2022-07-01"), by = 1))
tsdata <- ts(d1, frequency = 365)
ddata <- decompose(tsdata, "multiplicative")

I get following error here:我在这里收到以下错误:

Error in decompose(tsdata, "multiplicative") : time series has no or less than 2 periods分解错误(tsdata,“乘法”):时间序列没有或少于 2 个周期

From what i have read it seems like because I do not have two full years?从我所读到的似乎是因为我没有整整两年? is that correct?那是对的吗? I have tried doing it weekly as well:我也试过每周做一次:

series <- ts(data, frequency = 52, start = c(2021, 1))  
  • getting the same issue.遇到同样的问题。

How do I go about it without having to extend my dataset to two years since I do not have that, and still being able to decompose it?我如何在没有数据集的情况下将数据集扩展到两年,并且仍然能够分解它? Plus when I am actually trying to forecast it, it isn't giving me good enough forecast: Plot with forecast另外,当我实际上试图预测它时,它并没有给我足够好的预测: Plot with predict

My data somewhat resembles a bell curve during that period.在此期间,我的数据有点类似于钟形曲线。 so is there a better fitting timeseries model I can apply instead?那么我可以应用更好的拟合时间序列模型吗?

A weekly frequency for daily data should have frequency = 7 , not 52 .每日数据的每周频率应该是frequency = 7 ,而不是52 It's possible that this fix to your code will produce a model with a seasonal term.对您的代码的此修复可能会生成具有季节性术语的模型。

I don't think you'll be able to produce a time series model with annual seasonality with less than 2 years of data.我认为您无法使用少于 2 年的数据生成具有年度季节性的时间序列模型。

You can either produce a model with only weekly seasonality (I expect this is what most folks would recommend), or if you truly believe in the annual seasonal pattern exhibited in your data, your "forecast" can be a seasonal naive forecast that is simply last year's value for that particular day.您可以生成仅具有每周季节性的模型(我希望这是大多数人会推荐的),或者如果您真的相信数据中显示的年度季节性模式,那么您的“预测”可以是简单的季节性天真预测去年该特定日期的值。 I wouldn't recommend this, because it just seems risky, and I don't really see the same trajectory in your screenshot over 2022 that's apparent in 2021.我不建议这样做,因为它看起来很冒险,而且我在您的 2022 年截图中并没有真正看到与 2021 年相同的轨迹。

decompose requires two full cycles and that a full cycle represent 1 time unit. decompose需要两个完整的周期,一个完整的周期代表 1 个时间单位。 ts class can't use Date class anyways. ts 类无论如何都不能使用Date类。 To use frequency 7 we must use times 1/7th apart such as 1, 1+1/7, 1+2/7, etc. so that 1 cycle (7 days) covers 1 unit.要使用频率 7,我们必须使用相隔 1/7 的时间,例如 1、1+1/7、1+2/7 等,以便 1 个周期(7 天)覆盖 1 个单位。 Then just label the plot appropriately rather than using those times on the X axis.然后只需适当地标记绘图,而不是在 X 轴上使用这些时间。 In the code below use %Y in place of %y if the years start in 19??如果年份从 19 年开始,在下面的代码中使用 %Y 代替 %y? and end in 20??并在20结束?? so that tapply maintains the order.以便tapply维护订单。

# test data
set.seed(123)
s <- seq(from = as.Date("2021-01-01"), to = as.Date("2022-07-01"), by = 1)
data <- rnorm(length(s))
tsdata <- ts(data, freq = 7)

ddata <- decompose(tsdata, "multiplicative")

plot(ddata, xaxt = "n")
m <- tapply(time(tsdata), format(s, "%y/%m"), head, 1)
axis(1, m, names(m))

截屏

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

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