-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDA170216.R
813 lines (572 loc) · 25.8 KB
/
DDA170216.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#################################################################RETAIL GIANT FORECAST ####################################################################################
#Installing tthe required libraries
#install.packages(forecast)
#install.packages(lubridate)
#install.packages(dplyr)
#install.packages(ggplot2)
#install.packages(tseries)
#install.packages(cowplot)
library(forecast)
library(lubridate)
library(dplyr)
library(ggplot2)
library(tseries)
library(cowplot)
#######################################################################DATA GATHERING###########################################################################
#Reading the Global superstore dataset:
superstore<-read.csv('Global Superstore.csv',header = TRUE)
View(superstore)
#######################################################################DATA CLEANING############################################################################
sum(is.na(superstore))
sapply(superstore,function(x) sum(is.na(x)))
#Apart from the Postal Code there doesn't seem to be any NA Values in the dataset and hence no imputation required.
segment<-paste(superstore$Segment,superstore$Market,sep = '_')
superstore_seg<-cbind(superstore,segment)
#######################################################################EXPLORATORY DATA ANALYSIS###############################################################
#Finding the min & Max Order Date in the dataset
superstore_seg$Order.Date<-as.Date(superstore_seg$Order.Date,format = "%d-%m-%Y")
min(superstore_seg$Order.Date)
#2011-01-01
max(superstore_seg$Order.Date)
#2014-12-31
#extracting the monthly date column out of the Superstore Segment
superstore_seg$month<-format(as.Date(superstore_seg$Order.Date), "%Y-%m")
#Finding the most profitable segment in the given dataset
Most_profitable<-arrange(with(superstore_seg, aggregate(Profit, by=list(segment), FUN=sum)),x)
Most_profitable
#Plot the graph to find the most profitable segment:
bar_theme<- theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
legend.position="none")
ggplot(data = Most_profitable,aes(x=Group.1,x))+geom_bar(stat = "identity") +bar_theme
#Below are the 2 most profitable segments
#Consumer_EU 188687.707
#Consumer_APAC 222817.560
#Find out the segemtns which are the most consistently profitable
#Build the dataframe with only order date,Profit,sales,quantity and segmentnames #Consumer_EU #Consumer_APAC
superstore_prof<-cbind()
superstore_seg_val<-data.frame(superstore_seg$segment,superstore_seg$Sales,superstore_seg$Quantity,superstore_seg$Profit,superstore_seg$month,superstore_seg$Order.Date)
str(superstore_seg_val$superstore_seg.month)
View(superstore_seg_val)
colnames(superstore_seg_val)<-c("segment","Sales","Quantity","Profit","Month","ODate")
s <- setNames(split(superstore_seg_val, superstore_seg_val$segment), paste0(unique(sort(superstore_seg_val$segment)) ,"_df"))
list2env(s, globalenv())
profit_month<-list()
COV_month<-list()
for(i in 1:length(s))
{
prof_month<-with(s[[i]],aggregate(Profit,by=list(Month),FUN = sum))
profit_month[[i]]<-prof_month
cv_month<-sd(prof_month$x)/mean(prof_month$x)*100
COV_month[[i]]<-cv_month
}
d1<-data.frame(COV_month)
#Transform D1
d1<-t(d1)
rownames(d1)<-c(1:nrow(d1))
segment_name<-data.frame(sort(unique((superstore_seg$segment))))
cv_segment<-arrange(data.frame(segment_name,d1),d1)
colnames(cv_segment)<-c("Segment","CV Value")
ggplot(data = cv_segment,aes(x=Segment,`CV Value`))+geom_bar(stat = "identity") +bar_theme
#From the above segment it is clear that Consumer EU and Consumer APAC are the most consistent and profitable segments
#Consumer_EU 62.43052
#Consumer_APAC 63.21323
#Building the Monthly datasets consisting of the aggregate of the Quantity,Sales and Profit
###################################################BUILDING THE AGGREGATE DATA FOR THE APAC CONSUMER DATASET######################################################
Consumer_APAC_df_agg<-subset(Consumer_APAC_df[-1],Consumer_APAC_df$ODate<'2014-12-31')
Consumer_APAC_df_agg<-Consumer_APAC_df_agg[-5]%>%
group_by(Month) %>%
summarise_all(funs(sum))
Consumer_APAC_df_agg$Month<-gsub("-","",Consumer_APAC_df_agg$Month)
Consumer_APAC_df_agg$Month<-as.numeric(Consumer_APAC_df_agg$Month)
#######################################################BUILDING THE AGGREGATE DATA FOR THE EU CONSUMER DATASET##################################################
Consumer_EU_df_agg<-subset(Consumer_EU_df[-1],Consumer_EU_df$ODate<'2014-12-31')
Consumer_EU_df_agg<-Consumer_EU_df_agg[-5]%>%
group_by(Month) %>%
summarise_all(funs(sum))
Consumer_EU_df_agg$Month<-gsub("-","",Consumer_EU_df_agg$Month)
Consumer_EU_df_agg$Month<-as.numeric(Consumer_EU_df_agg$Month)
#Building the time series for each of these above datasets:
########################################################################Consumer APAC##########################################################################
##Sales ##
ts_cons_APAC_sales_agg <- ts(Consumer_APAC_df_agg$Sales,frequency = 12)
plot(ts_cons_APAC_sales_agg)
ts_cons_APAC_sales <- ts(Consumer_APAC_df_agg[1:42,]$Sales,frequency = 12)
plot(ts_cons_APAC_sales)
##Quantity
ts_cons_APAC_quan_agg <- ts(Consumer_APAC_df_agg$Quantity,frequency = 12)
plot(ts_cons_APAC_quan_agg)
ts_cons_APAC_quan <- ts(Consumer_APAC_df_agg[1:42,]$Quantity,frequency = 12)
plot(ts_cons_APAC_quan)
#Smoothing the curve for the above set of timeseries :
#ts_cons_APAC_sales
decompose(ts_cons_APAC_sales)
decompose(ts_cons_APAC_sales_agg)
decompose(ts_cons_APAC_quan)
decompose(ts_cons_APAC_quan_agg)
#Smoothing the series - Moving Average Smoothing
w <-2
smoothedseries <- stats::filter(ts_cons_APAC_sales,
filter=rep(1/(2*w+1),(2*w+1)),
method='convolution', sides=2)
smoothedseries
#Smoothing left end of the time series
diff <- smoothedseries[w+2] - smoothedseries[w+1]
for (i in seq(w,1,-1)) {
smoothedseries[i] <- smoothedseries[i+1] - diff
}
#Smoothing right end of the time series
n <- length(ts_cons_APAC_sales)
diff <- smoothedseries[n-w] - smoothedseries[n-w-1]
for (i in seq(n-w, n)) {
smoothedseries[i] <- smoothedseries[i-1] + diff
}
#Plot the smoothed time series
plot(ts_cons_APAC_sales)
lines(smoothedseries, col="blue", lwd=2)
timevals_in<-Consumer_APAC_df_agg[1:42,]$Month
smootheddf <- as.data.frame(cbind(timevals_in, as.vector(smoothedseries)))
colnames(smootheddf) <- c('Month', 'Sales')
View(smootheddf)
#Seasonality will be modeled using a sinusoid function
#sin(0.5*Month)*Month + cos(0.5*Month) + Month
lmfit <- lm(Sales ~ poly(Month,2)+cos(2*pi*Month)+sin(2*pi*Month)+sin(0.6*Month) +cos(0.6*Month), data=smootheddf)
summary(lmfit)
global_pred <- predict(lmfit, Month=timevals_in)
summary(global_pred)
lines(timevals_in, global_pred, col='red', lwd=2)
#Now, let's look at the locally predictable series
#We will model it as an ARMA series
local_pred <- ts_cons_APAC_sales-global_pred
plot(local_pred, col='red', type = "l")
acf(local_pred)
acf(local_pred, type="partial")
armafit <- auto.arima(local_pred)
tsdiag(armafit)
armafit
#We'll check if the residual series is white noise
resi <- local_pred-fitted(armafit)
adf.test(resi,alternative = "stationary")
kpss.test(resi)
#Now, let's evaluate the model using MAPE
#First, let's make a prediction for the last 6 months
outdata <- Consumer_APAC_df_agg[43:48,]
timevals_out <- outdata$Month
global_pred_out <- predict(lmfit,data.frame(Month =timevals_out))
fcast <- global_pred_out
fcast
#Now, let's compare our prediction with the actual values, using MAPE
MAPE_class_dec <- accuracy(fcast,outdata$Sales)
MAPE_class_dec
#The MAPE Value is around 28.56 for the model
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
class_dec_pred <- c(ts(global_pred),ts(global_pred_out))
plot(ts_cons_APAC_sales_agg, col = "black")
lines(class_dec_pred, col = "red")
#The graph shows that the predicted values follow the trend of the original data plot series
#Predict the next 6 values for Month 49-54
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(armafit, h=12)
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[1:6],outdata$Sales)
MAPE_auto_arima
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Sales)
MAPE_auto_arima
data.frame(fcast_auto_arima$pred,outdata$Sales)
#So, that was classical decomposition, now let's do an ARIMA fit
autoarima <- auto.arima(ts_cons_APAC_sales)
autoarima
s1<-tsdiag(autoarima)
plot(autoarima$x, col="black")
lines(fitted(autoarima), col="red")
fitted(autoarima)
#Again, let's check if the residual series is white noise
resi_auto_arima <- ts_cons_APAC_sales - fitted(autoarima)
adf.test(resi_auto_arima,alternative = "stationary")
kpss.test(resi_auto_arima)
fcast_auto_arima <- forecast(autoarima, h=12)
fcast_auto_arima
#Lastly, let's plot the predictions along with original values, to
#get a visual feel of the fit
auto_arima_pred <- c(fitted(autoarima),ts(fcast_auto_arima$pred))
plot(ts_cons_APAC_quan_agg, col = "black")
lines(auto_arima_pred, col = "red")
#The prediction for the Auto Arima model gives constant value as 44898.7
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[1:6],outdata$Sales)
MAPE_auto_arima
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Sales)
MAPE_auto_arima
#The MAPE for the Uto Arima model is around 28.26
data.frame(fcast_auto_arima$pred,outdata$Sales)
#Using HOLTWinters Exponential smoothing technique to smoothen the model
#The Quantity time series for the APAC Consumer segment has only Trend and no seasonality,hence setting only the GAMMA Function to False
smoothedseries_APAC_sales <- HoltWinters(ts_cons_APAC_sales)
smoothedseries_APAC_sales
#Plot the smoothed time series
plot(ts_cons_APAC_sales)
plot(smoothedseries_APAC_sales, col="blue")
timevals_in<-Consumer_APAC_df_agg[43:48,]$Month
fcast_APAC_sales<-forecast::forecast(smoothedseries_APAC_sales,h=12)
fcast_APAC_sales
#Now, let's compare our prediction with the actual values, using MAPE
outdata <- Consumer_APAC_df_agg[43:48,]
MAPE_class_dec <- accuracy(fcast_APAC_sales$mean[1:6],outdata$Sales)
MAPE_class_dec
MAPE_class_dec <- accuracy(fcast_APAC_sales$mean[7:12],outdata$Sales)
MAPE_class_dec
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
plot(ts_cons_APAC_sales_agg)
plot(smoothedseries_APAC_sales, col = "red")
plot(fcast_APAC_sales,col = "Blue")
str(fcast_APAC_sales)
#ts_cons_APAC_quan
#Smoothing the series - Moving Average Smoothing
w <-2
smoothedseries_APAC_quan <- stats::filter(ts_cons_APAC_quan,
filter=rep(1/(2*w+1),(2*w+1)),
method='convolution', sides=2)
smoothedseries_APAC_quan
#Smoothing left end of the time series
diff <- smoothedseries_APAC_quan[w+2] - smoothedseries_APAC_quan[w+1]
for (i in seq(w,1,-1)) {
smoothedseries_APAC_quan[i] <- smoothedseries_APAC_quan[i+1] - diff
}
#Smoothing right end of the time series
n <- length(ts_cons_APAC_quan)
diff <- smoothedseries_APAC_quan[n-w] - smoothedseries_APAC_quan[n-w-1]
for (i in seq(n-w, n)) {
smoothedseries_APAC_quan[i] <- smoothedseries_APAC_quan[i-1] + diff
}
#Plot the smoothed time series
plot(ts_cons_APAC_quan)
lines(smoothedseries_APAC_quan, col="blue", lwd=2)
timevals_in<-Consumer_APAC_df_agg[1:42,]$Month
smootheddf_APAC_quan <- as.data.frame(cbind(timevals_in, as.vector(smoothedseries_APAC_quan)))
colnames(smootheddf_APAC_quan) <- c('Month', 'Quantity')
View(smootheddf_APAC_quan)
#Seasonality will be modeled using a sinusoid function
lmfit <- lm(Quantity ~ cos(0.6*Month) , data=smootheddf_APAC_quan)
global_pred <- predict(lmfit, Month=timevals_in)
global_pred
lines(timevals_in, global_pred, col='red', lwd=2)
#Now, let's look at the locally predictable series
#We will model it as an ARMA series
local_pred <- ts_cons_APAC_quan-global_pred
local_pred
plot(local_pred, col='red', type = "l")
acf(local_pred)
acf(local_pred, type="partial")
armafit <- auto.arima(local_pred)
tsdiag(armafit)
armafit
#We'll check if the residual series is white noise
resi <- local_pred-fitted(armafit)
adf.test(resi,alternative = "stationary")
kpss.test(resi)
#Now, let's evaluate the model using MAPE
#First, let's make a prediction for the last 6 months
outdata <- Consumer_APAC_df_agg[43:48,]
timevals_out <- outdata$Month
global_pred_out <- predict(lmfit,data.frame(Month =timevals_out))
fcast <- global_pred_out
#Now, let's compare our prediction with the actual values, using MAPE
MAPE_class_dec <- accuracy(fcast,outdata$Quantity)
MAPE_class_dec
#The MAPE using the Classical decomposition is around 41.49
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
class_dec_pred <- c(ts(global_pred),ts(global_pred_out))
plot(ts_cons_APAC_quan_agg, col = "black")
lines(ts(global_pred), col = "red")
global_pred_out
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(armafit,h=12)
fcast_auto_arima
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Quantity)
MAPE_auto_arima
#So, that was classical decomposition, now let's do an ARIMA fit
autoarima <- auto.arima(ts_cons_APAC_quan)
autoarima
s1<-tsdiag(autoarima)
plot(autoarima$x, col="black")
lines(fitted(autoarima), col="red")
fitted(autoarima)
#Again, let's check if the residual series is white noise
resi_auto_arima <- ts_cons_APAC_sales - fitted(autoarima)
adf.test(resi_auto_arima,alternative = "stationary")
kpss.test(resi_auto_arima)
fcast_auto_arima <- forecast(autoarima, h=12)
fcast_auto_arima
#Lastly, let's plot the predictions along with original values, to
#get a visual feel of the fit
auto_arima_pred <- c(fitted(autoarima),ts(fcast_auto_arima$pred))
plot(ts_cons_APAC_quan_agg, col = "black")
lines(auto_arima_pred, col = "red")
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[1:6],outdata$Quantity)
MAPE_auto_arima
#The MAPE for the Uto Arima model is around 23.24
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Quantity)
MAPE_auto_arima
#Using HOLTWinters Exponential smoothing technique to smoothen the model
#The Quantity time series for the APAC Consumer segment has only Trend and no seasonality,hence setting only the GAMMA Function to False
smoothedseries_APAC_quan <- HoltWinters(ts_cons_APAC_quan,gamma = F)
smoothedseries_APAC_quan$alpha
#Plot the smoothed time series
plot(ts_cons_APAC_quan)
plot(smoothedseries_APAC_quan, col="blue")
timevals_in<-Consumer_APAC_df_agg[43:48,]$Month
fcast_APAC_quan<-forecast::forecast(smoothedseries_APAC_quan,h=12)
#Now, let's compare our prediction with the actual values, using MAPE
outdata <- Consumer_APAC_df_agg[43:48,]
MAPE_class_dec <- accuracy(fcast_APAC_quan$mean[1:6],outdata$Quantity)
MAPE_class_dec
MAPE_class_dec <- accuracy(fcast_APAC_quan$mean[7:12],outdata$Quantity)
MAPE_class_dec
#The MAPE using the HOLT Winter series is around 23.45
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
plot(ts_cons_APAC_quan_agg)
plot(smoothedseries_APAC_quan, col = "red")
plot(fcast_APAC_quan,col = "Blue")
###########################################################Time Series Analysis for Consumer EU Dataset############################################
#Consumer EU
#Sales
ts_cons_EU_sales_agg <- ts(Consumer_EU_df_agg$Sales,frequency = 12)
plot(ts_cons_EU_sales_agg)
ts_cons_EU_sales <- ts(Consumer_EU_df_agg[1:42,]$Sales,frequency = 12)
plot(ts_cons_EU_sales)
#Consumer APAC
#Quantity
ts_cons_EU_quan_agg <- ts(Consumer_EU_df_agg$Quantity,frequency = 12)
plot(ts_cons_EU_quan_agg)
ts_cons_EU_quan <- ts(Consumer_EU_df_agg[1:42,]$Quantity,frequency = 12)
plot(ts_cons_EU_quan)
#Smoothing the series - Moving Average Smoothing
w <-1
smoothedseries <- stats::filter(ts_cons_EU_sales,
filter=rep(1/(2*w+1),(2*w+1)),
method='convolution', sides=2)
smoothedseries
#Smoothing left end of the time series
diff <- smoothedseries[w+2] - smoothedseries[w+1]
for (i in seq(w,1,-1)) {
smoothedseries[i] <- smoothedseries[i+1] - diff
}
#Smoothing right end of the time series
n <- length(ts_cons_EU_sales)
diff <- smoothedseries[n-w] - smoothedseries[n-w-1]
for (i in seq(n-w, n)) {
smoothedseries[i] <- smoothedseries[i-1] + diff
}
#Plot the smoothed time series
plot(ts_cons_EU_sales)
lines(smoothedseries, col="blue", lwd=2)
timevals_in<-Consumer_EU_df_agg[1:42,]$Month
smootheddf <- as.data.frame(cbind(timevals_in, as.vector(smoothedseries)))
colnames(smootheddf) <- c('Month', 'Sales')
View(smootheddf)
#Seasonality will be modeled using a sinusoid function
lmfit <- lm(Sales ~ poly(Month,1) *sin(2*pi*Month), data=smootheddf)
lmfit
global_pred <- predict(lmfit, Month=timevals_in)
summary(global_pred)
lines(timevals_in, global_pred, col='red', lwd=2)
#Now, let's look at the locally predictable series
#We will model it as an ARMA series
local_pred <- ts_cons_EU_sales-global_pred
plot(local_pred, col='red', type = "l")
acf(local_pred)
acf(local_pred, type="partial")
#The ACF Plot has the spike at the first occurence,hence it is a lag1
armafit <- auto.arima(local_pred)
tsdiag(armafit)
armafit
#We'll check if the residual series is white noise
resi <- local_pred-fitted(armafit)
adf.test(resi,alternative = "stationary")
kpss.test(resi)
#Now, let's evaluate the model using MAPE
#First, let's make a prediction for the last 6 months
outdata <- Consumer_EU_df_agg[43:48,]
timevals_out <- outdata$Month
global_pred_out <- predict(lmfit,data.frame(Month =timevals_out))
fcast <- global_pred_out
#Now, let's compare our prediction with the actual values, using MAPE
MAPE_class_dec <- accuracy(fcast,outdata$Sales)
MAPE_class_dec
#The MAPE Value is around 24.08 for the model
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
class_dec_pred <- c(ts(global_pred),ts(global_pred_out))
plot(ts_cons_EU_sales_agg, col = "black")
lines(class_dec_pred, col = "red")
#The graph shows that the predicted values follow the trend of the original data plot series
#Predict the next 6 values for Month 49-54
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(armafit, h=6)
fcast_auto_arima
data.frame(fcast_auto_arima$pred,outdata$Sales)
#So, that was classical decomposition, now let's do an ARIMA fit
autoarima <- auto.arima(ts_cons_EU_sales)
autoarima
s1<-tsdiag(autoarima)
plot(autoarima$x, col="black")
lines(fitted(autoarima), col="red")
fitted(autoarima)
#Again, let's check if the residual series is white noise
resi_auto_arima <- ts_cons_EU_sales - fitted(autoarima)
adf.test(resi_auto_arima,alternative = "stationary")
kpss.test(resi_auto_arima)
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(autoarima, h=12)
fcast_auto_arima
#Lastly, let's plot the predictions along with original values, to
#get a visual feel of the fit
auto_arima_pred <- c(fitted(autoarima),ts(fcast_auto_arima$pred))
plot(ts_cons_EU_quan_agg, col = "black")
lines(auto_arima_pred, col = "red")
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[1:6],outdata$Sales)
MAPE_auto_arima
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Sales)
MAPE_auto_arima
#The MAPE for the Uto Arima model is around 24.94
#Using HOLTWinters Exponential smoothing technique to smoothen the model
smoothedseries_EU_sales <- HoltWinters(ts_cons_EU_sales)
smoothedseries_EU_sales
#Plot the smoothed time series
plot(ts_cons_EU_sales)
plot(smoothedseries_EU_sales, col="blue")
timevals_in<-Consumer_EU_df_agg[43:48,]$Month
fcast_EU_sales<-forecast::forecast(smoothedseries_EU_sales,h=12)
fcast_EU_sales
#Now, let's compare our prediction with the actual values, using MAPE
outdata <- Consumer_EU_df_agg[43:48,]
MAPE_class_dec <- accuracy(fcast_EU_sales$mean[1:6],outdata$Sales)
MAPE_class_dec
MAPE_class_dec <- accuracy(fcast_EU_sales$mean[7:12],outdata$Sales)
MAPE_class_dec
#The MAPE for the forecasted valueis around 26.39
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
plot(ts_cons_EU_sales_agg)
plot(smoothedseries_EU_sales, col = "red")
plot(fcast_EU_sales,col = "Blue")
str(fcast_EU_sales)
#ts_cons_EU_quan
#Smoothing the series - Moving Average Smoothing
w <-1
smoothedseries_EU_quan <- stats::filter(ts_cons_EU_quan,
filter=rep(1/(2*w+1),(2*w+1)),
method='convolution', sides=2)
smoothedseries_EU_quan
#Smoothing left end of the time series
diff <- smoothedseries_EU_quan[w+2] - smoothedseries_EU_quan[w+1]
for (i in seq(w,1,-1)) {
smoothedseries_EU_quan[i] <- smoothedseries_EU_quan[i+1] - diff
}
#Smoothing right end of the time series
n <- length(ts_cons_EU_quan)
diff <- smoothedseries_EU_quan[n-w] - smoothedseries_EU_quan[n-w-1]
for (i in seq(n-w, n)) {
smoothedseries_EU_quan[i] <- smoothedseries_EU_quan[i-1] + diff
}
#Plot the smoothed time series
plot(ts_cons_EU_quan)
lines(smoothedseries_EU_quan, col="blue", lwd=2)
timevals_in<-Consumer_EU_df_agg[1:42,]$Month
smootheddf_EU_quan <- as.data.frame(cbind(timevals_in, as.vector(smoothedseries_EU_quan)))
colnames(smootheddf_EU_quan) <- c('Month', 'Quantity')
View(smootheddf_EU_quan)
#Seasonality will be modeled using a sinusoid function
lmfit <- lm(Quantity ~ sin(0.5*Month) * poly(Month,3) + cos(0.5*Month) * poly(Month,3) + Month, data=smootheddf_EU_quan)
global_pred <- predict(lmfit, Month=timevals_in)
global_pred
lines(timevals_in, global_pred, col='red', lwd=2)
#Now, let's look at the locally predictable series
#We will model it as an ARMA series
local_pred <- ts_cons_EU_quan-global_pred
local_pred
plot(local_pred, col='red', type = "l")
acf(local_pred)
acf(local_pred, type="partial")
armafit <- auto.arima(local_pred)
tsdiag(armafit)
armafit
#We'll check if the residual series is white noise
resi <- local_pred-fitted(armafit)
adf.test(resi,alternative = "stationary")
kpss.test(resi)
#Now, let's evaluate the model using MAPE
#First, let's make a prediction for the last 6 months
outdata <- Consumer_EU_df_agg[43:48,]
timevals_out <- outdata$Month
global_pred_out <- predict(lmfit,data.frame(Month =timevals_out))
fcast <- global_pred_out
#Now, let's compare our prediction with the actual values, using MAPE
MAPE_class_dec <- accuracy(fcast,outdata$Quantity)
MAPE_class_dec
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
class_dec_pred <- c(ts(global_pred),ts(global_pred_out))
plot(ts_cons_EU_quan_agg, col = "black")
lines(class_dec_pred, col = "red")
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(armafit, h=12)
fcast_auto_arima
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean,outdata$Quantity)
MAPE_auto_arima
data.frame(fcast_auto_arima$pred,outdata$Quantity)
#So, that was classical decomposition, now let's do an ARIMA fit
autoarima <- auto.arima(ts_cons_EU_quan)
autoarima
s1<-tsdiag(autoarima)
plot(autoarima$x, col="black")
lines(fitted(autoarima), col="red")
fitted(autoarima)
#Again, let's check if the residual series is white noise
resi_auto_arima <- ts_cons_EU_quan - fitted(autoarima)
adf.test(resi_auto_arima,alternative = "stationary")
kpss.test(resi_auto_arima)
#Also, let's evaluate the model using MAPE
fcast_auto_arima <- forecast(autoarima, h=12)
fcast_auto_arima
#Lastly, let's plot the predictions along with original values, to
#get a visual feel of the fit
auto_arima_pred <- c(fitted(autoarima),ts(fcast_auto_arima$pred))
plot(ts_cons_EU_quan_agg, col = "black")
lines(auto_arima_pred, col = "red")
#The prediction for the Auto Arima model gives constant value as 44898.7
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[1:6],outdata$Quantity)
MAPE_auto_arima
#The MAPE for the Uto Arima model is around 17.42
MAPE_auto_arima <- accuracy(fcast_auto_arima$mean[7:12],outdata$Quantity)
MAPE_auto_arima
data.frame(fcast_auto_arima$pred,outdata$Sales)
#Using HOLTWinters Exponential smoothing technique to smoothen the model
smoothedseries_EU_quan <- HoltWinters(ts_cons_EU_quan)
smoothedseries_EU_quan$alpha
#Plot the smoothed time series
plot(ts_cons_EU_quan)
plot(smoothedseries_EU_quan, col="blue")
timevals_in<-Consumer_EU_df_agg[1:42,]$Month
fcast_EU_quan<-forecast::forecast(smoothedseries_EU_quan,h=12)
fcast_EU_quan
#Now, let's compare our prediction with the actual values, using MAPE
outdata <- Consumer_EU_df_agg[43:48,]
MAPE_class_dec <- accuracy(fcast_EU_quan$mean[1:6],outdata$Quantity)
MAPE_class_dec
#The MAPE for the Uto Arima model is around 18.78
MAPE_class_dec <- accuracy(fcast_EU_quan$mean[7:12],outdata$Quantity)
MAPE_class_dec
#The MAPE for the Uto Arima model is around 26.51
#Let's also plot the predictions along with original values, to
#get a visual feel of the fit
plot(ts_cons_EU_quan_agg)
plot(smoothedseries_EU_quan, col = "red")
plot(fcast_EU_quan,col = "Blue")
#Below are the final results of the Values of MAPE for each of the techniques used:
# Classical Decomposition Auto Arima Holt Winter
#Consumer_APAC_Sales 28.56 11.87 15.28
#Consumer_APAC_Quan 41.49 23.34 23.39
#Consumer_EU_Sales 27.72 24.94 12.23
#Consumer_APAC_Quan 31.46 17.42 18.78