I am working on predicting a demand forecast of a time series data.
dput
output is saved to Data
Variable
Data <- structure(list(Yr = c(2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L), Month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L), Demand = c(58L, 59L, 108L, 145L,
109L, 105L, 104L, 175L, 101L, 105L, 254L, 199L, 187L, 201L, 149L,
93L, 126L, 115L, 136L, 94L, 135L, 116L, 112L, 95L, 122L, 247L,
188L, 121L, 237L, 190L, 187L, 206L, 206L, 156L, 198L, 154L, 231L,
190L, 237L, 250L, 182L, 250L, 118L, 123L, 222L)), .Names = c("Yr",
"Month", "Demand"), class = "data.frame", row.names = c(NA, -45L
))
str(Data)
I take a log Transformation of Demand
Variable and Decompose
to check Seasonality
Data$Log_Demand = log(Data$Demand)
splot <- ts(Data$Log_Demand, start=c(2010, 1),end=c(2013,9),frequency=12)
fit <- stl(splot, s.window="period")
monthplot(splot)
library(forecast)
seasonplot(splot)
I get a Month plot and Seasonal plot - I am finding it tough to code the seasonal pattern observed.
Data$Seasonal_Jan = ifelse(Data$time %in% c(1,13,25,37),1,0)
My Question here is :
From the Graph i wanted to automatically find for what months seasonal patterns are observed and code a dummy variable(as above) for those seasonal to use that variable in lm
model to fit a trend component and from the lm
model residuals, I fit a ARIMA Model to predict the forecast.
This is not what you asked but as nobody has answered here are some comments that may help you.
First, I don't see the need to take logarithms on this series. This simple plot does not suggest that the variance of the series increases with the mean.
x <- ts(Data$Demand, start=c(2010, 1), end=c(2013,9), frequency=12)
lx <- split(x, gl(length(x)/6, 6))
m <- unlist(lapply(lx, mean))
r <- unlist(lapply(lx, function(x) diff(range(x))))
plot(m, r)
abline(lm(r ~ m))
A more elaborate method does not suggest taking logs either.
library("forecast")
> BoxCox.lambda(x, lower=0, upper=1)
[1] 0.9999339
As regards the seasonal pattern you are interested in, the autocorrelation functions do not show significant autocorrelation of seasonal order.
par(mfrow = c(2, 1), mar = c(3,2,3,2))
acf(x, lag.max = 60)
pacf(x, lag.max = 60)
A first-order autoregressive model, AR(1), can be appropriate for this series.
The function forecast: 'seasonaldummy' builds seasonal dummies for all seasons except one (to avoid multicollinearity) that can be included in a regression.
SD <- seasonaldummy(x)
> summary(lm(x ~ SD))
[...]
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 149.42857 22.69524 6.584 1.52e-07 ***
SD[, -1]Feb 24.82143 37.63580 0.660 0.514
SD[, -1]Mar 21.07143 37.63580 0.560 0.579
SD[, -1]Apr 2.82143 37.63580 0.075 0.941
SD[, -1]May 14.07143 37.63580 0.374 0.711
SD[, -1]Jun 15.57143 37.63580 0.414 0.682
SD[, -1]Jul -13.17857 37.63580 -0.350 0.728
SD[, -1]Aug 0.07143 37.63580 0.002 0.998
SD[, -1]Sep 16.57143 37.63580 0.440 0.662
SD[, -1]Oct -23.76190 41.43566 -0.573 0.570
SD[, -1]Nov 38.57143 41.43566 0.931 0.358
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
[...]
The seasonal dummies are not significant at the 5% level. Similar results are obtained with the logarithms of the original series.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.