简体   繁体   English

基于时间序列的每日数据预测,但季节性是季度 - 在R

[英]Time Series based Forecasting for Daily Data but Seasonality is Quarterly - in R

I have demand for a product on daily bases for last 4 years. 过去4年我每天都要求生产一种产品。 This demand has quarterly seasonal patterns, as shown in following image 这种需求具有季度季节性模式,如下图所示

绘制ts对象

I would like to do time series based forecasting on this data. 我想对这些数据做基于时间序列的预测。 Following is my code 以下是我的代码

myts = ts(forecastsku1$Value,frequency=90)
fit <- stl(myts, s.window="period")
plot(fit)
fit <- decompose(myts)
plot(fit)

STL的结果

分解结果

Here instead of 4 seasonal factor ts is creating 90 seasonal factor, which is not what I want. 这里代替4个季节性因素ts创造了90个季节性因素,这不是我想要的。 I want to apply same seasonality on 3 month duration and then do forecasting. 我希望在3个月的持续时间内应用相同的季节性,然后进行预测。

Data for reference 数据供参考

dput(head(forecastsku1,100))
structure(list(date = structure(c(14625, 14626, 14627, 14628, 14629, 14630, 14631, 14632, 14633, 14634, 14635, 14636, 14637, 
14638, 14639, 14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648, 14649, 14650, 14651, 14652, 14653, 14654, 14655, 
14656, 14657, 14658, 14659, 14660, 14661, 14662, 14663, 14664, 14665, 14666, 14667, 14668, 14669, 14670, 14671, 14672, 14673, 
14674, 14675, 14676, 14677, 14678, 14679, 14680, 14681, 14682, 14683, 14684, 14685, 14686, 14687, 14688, 14689, 14690, 14691, 
14692, 14693, 14694, 14695, 14696, 14697, 14698, 14699, 14700, 14701, 14702, 14703, 14704, 14705, 14706, 14707, 14708, 14709, 
14710, 14711, 14712, 14713, 14714, 14715, 14716, 14717, 14718, 14719, 14720, 14721, 14722, 14723, 14724), class = "Date"), 
Value = c(1407, 1413, 1407, 1406, 1401, 1410, 1411, 1416, 1404, 1409, 1414, 1414, 1400, 1421, 1398, 1404, 1397, 1404, 1407, 1409, 1406, 1395, 1397, 
1403, 1412, 1399, 1409, 1393, 1405, 1403, 1406, 1402, 1405, 1386, 1393, 1405, 1397, 1393, 1402, 1402, 1393, 1391, 1410, 1402, 1408, 
1394, 1404, 1398, 1406, 1389, 1401, 1391, 1394, 1384, 1377, 1390, 1395, 1399, 1384, 1397, 1398, 1384, 1377, 1394, 1398, 1394, 1391, 
1403, 1382, 1390, 1385, 1403, 1390, 1388, 1391, 1384, 1392, 1390, 1381, 1387, 1395, 1390, 1388, 1384, 1387, 1395, 1380, 1378, 1383, 
1384, 1232, 1247, 1232, 1248, 1236, 1236, 1231, 1237, 1224, 1236)), 
.Names = c("date", "Value"), row.names = 13150:13249, class = "data.frame")

Can anyone help me in this case? 在这种情况下,有人可以帮助我吗? Please let me know if more data required. 如果需要更多数据,请告诉我。

myts = ts(forecastsku1$Value,frequency=4)
fit <- decompose(myts)
plot(fit)

Result would be: 结果将是: 在此输入图像描述

It is creating a 90 seasonal factor because your frequency is 90 in the ts definition. 它创建了一个90季节因素,因为你的频率在ts定义中是90。 What you need to do is to specify a start and end in the ts and the period=4 so that the observations can be segregated the way you want them to be.. if you can successfully create a 4 seasonal factor, you can obviousy predict quarterly (4*3=12) . 您需要做的是指定ts中的开始和结束以及周期= 4,以便可以按照您希望的方式隔离观察结果。如果您可以成功创建4个季节性因子,则可以明显地预测每季度(4 * 3 = 12)。 So instead of these dates I think it is more clear to have like start=c(2005,1) .Hopefully this is useful 所以不是这些日期,我认为更清楚的是像start = c(2005,1)。希望这很有用

this is an old question, but still, maybe my answer is of some value. 这是一个古老的问题,但是,也许我的回答是有一定价值的。 You can seasonally adjust daily data using the dsa package (disclaimer: I'm the author). 您可以使用dsa包季节性地调整每日数据(免责声明:我是作者)。

I tried to replicate your time series (or something similar) to give you an idea of how to seasonally adjust them (the setting of the seasonal adjustment try to help modelling the jumping behaviour of the time series appropriately): 我试图复制你的时间序列(或类似的东西),让你知道如何季节性调整它们(季节性调整的设置试图帮助建模时间序列的跳跃行为):

# loading packages
library(dsa); library(xts)

# Replication of the data
set.seed(23)
data <- seq(1250, 1000, , length.out=365.25*4) + rnorm(365.25*4, 0, 5)
time <- seq(as.Date("2008-01-01"), by="days", length.out=365.25*4)    
x <- xts(data, time)
ind <- as.numeric(format(zoo::index(x), "%m")) # Indicator of day of year
x[ind==1 | ind==2 | ind==3 | ind==7 | ind==8 | ind==9] <- 
x[ind==1 | ind==2 | ind==3 | ind==7 | ind==8 | ind==9] + 200

# Seasonally adjusting the data
result <- dsa(x, fourier_number=40, reiterate3=4, reg.create=NULL, cval=30) 
sa <- result$output[,1]
xtsplot(result$output[,c(2,1)], names=c("original", "seasonally adjusted"))
output(result) # creates a html in your working directory.

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

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