3

I have demand for a product on daily bases for last 4 years. This demand has quarterly seasonal patterns, as shown in following image

ploting of ts object

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)

Result of STL

Result of decompose

Here instead of 4 seasonal factor ts is creating 90 seasonal factor, which is not what I want. I want to apply same seasonality on 3 month duration and then do forecasting.

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: enter image description here

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • Please provide the output of `dput(head(forecastsku1))` to make your data reproducible. – talat May 15 '14 at 07:35
  • You may want to transform your data first. – Rich Scriven May 15 '14 at 07:48
  • @beginneR - Data added... – vrajs5 May 15 '14 at 07:55
  • @RichardScriven - Thats my question how can I transform so that daily data can be kept along with quarter-based seasonality. – vrajs5 May 15 '14 at 07:56
  • @vrajs5 have a thorough look around SO. There are already many questions with answers how to convert daily to weekly/monthly data, e.g. [here](http://stackoverflow.com/questions/16442396/convert-daily-to-weekly-monthly-data-with-r). Once you have converted it to monthly data, you can transform it to quarterly for example with `ts(...,frequency=4)` – talat May 15 '14 at 08:16
  • @beginneR, it is more than that, he need to add weekly, monthly and quortarly seasonalities all together. The way to do so is with `model.matrix`. i would post a solution, but I really don't have enought time for this right now. Try to look [here](http://stats.stackexchange.com/questions/52462/arima-double-seasonality-with-dummy-in-r-error-xreg) for now – David Arenburg May 15 '14 at 08:25
  • @beginneR - Just taking mean and plotting graph won't solve my purpose – vrajs5 May 15 '14 at 08:45
  • @DavidArenburg - I will try model.matrix and let you know... – vrajs5 May 15 '14 at 08:45
  • I must have missed something then, sorry about that. – talat May 15 '14 at 08:49

2 Answers2

2

It is creating a 90 seasonal factor because your frequency is 90 in the ts definition. 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) . So instead of these dates I think it is more clear to have like start=c(2005,1) .Hopefully this is useful

Rads
  • 345
  • 1
  • 4
  • 11
0

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).

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.