-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathModelBuilding_CameraAccessory.R
857 lines (561 loc) · 44.7 KB
/
ModelBuilding_CameraAccessory.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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
################################################################################################################################################################
# :::::::: Model Building [CameraAccessory] ::::::::
################################################################################################################################################################
#******************************************************** [Linear Regression Model]
### Preparing dataset
## Removing lag variables and Moving averages variables
## Also removing "list_price" and "promotional_offer" column as those are on dependent variable (i.e. gmv)
## Retaining those is not good idea as 'gmv' won't present in unseen data
LR_CA_data <- CameraAccessory_final[,-c(21:22,77:91)]
## Scaling the variables
LR_CA_data[,2:ncol(LR_CA_data)] <- scale(LR_CA_data[,2:ncol(LR_CA_data)])
## Checking the variables for linear relationship or multicollinearity
model <- lm(gmv~.,LR_CA_data)
alias(model)
## Removing the variables which were showing linear relationship or multicollinearity
LR_CA_data <- LR_CA_data[, -c(54:72)]
### Stepwise Regression to remove insignificant and correlated variables
LR_CA_base.mod <- lm(gmv ~ 1 , data= LR_CA_data) # base intercept only model
LR_CA_all.mod <- lm(gmv ~ . , data= LR_CA_data) # full model with all predictors
LR_CA_stepMod <- step(LR_CA_base.mod, scope = list(lower = LR_CA_base.mod, upper = LR_CA_all.mod), direction = "both", trace = 1, steps = 1000) # perform step-wise algorithm
LR_CA_shortlistedVars <- names(unlist(LR_CA_stepMod[[1]])) # get the shortlisted variable.
LR_CA_shortlistedVars <- LR_CA_shortlistedVars[!LR_CA_shortlistedVars %in% "(Intercept)"] # remove intercept
### Model Building::
## Building First model after short listing the variables[using LR_CA_stepMod]
LR_CA_model_1 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xCameraMount + week + Content_Marketing_adstock +
Sponsorship + product_analytic_vertical.xTeleconverter +
sla + product_analytic_vertical.xCameraBatteryGrip + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + Total_Investment + product_analytic_vertical.xStrap +
product_analytic_vertical.xCameraEyeCup, data = LR_CA_data)
summary(LR_CA_model_1)
vif(LR_CA_model_1)
## High VIF and Insignificant p-value columns: Total_Investment
## Slightly high VIF and Insignificant p-value columns: Sponsorship, product_analytic_vertical.xCameraMount
## Insignificant p-value columns: sla, product_analytic_vertical.xCameraEyeCup
LR_CA_model_2 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
week + Content_Marketing_adstock + product_analytic_vertical.xTeleconverter +
product_analytic_vertical.xCameraBatteryGrip + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + product_analytic_vertical.xStrap, data = LR_CA_data)
summary(LR_CA_model_2)
vif(LR_CA_model_2)
## Slightly high VIF and Insignificant p-value columns: week
## High VIF and Insignificant p-value columns: week
## Insignificant p-value columns: product_analytic_vertical.xCameraBatteryGrip
LR_CA_model_3 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube + Content_Marketing_adstock +
product_analytic_vertical.xTeleconverter + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
product_analytic_vertical.xStrap, data = LR_CA_data)
summary(LR_CA_model_3)
vif(LR_CA_model_3)
## High VIF and Insignificant p-value columns: product_analytic_vertical.xCameraBattery
## Insignificant p-value columns: Content_Marketing_adstock
LR_CA_model_4 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xTeleconverter + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xStrap, data = LR_CA_data)
summary(LR_CA_model_4)
vif(LR_CA_model_4)
## Slightly high VIF and Insignificant p-value columns: product_analytic_vertical.xCameraRemoteControl
## Insignificant p-value columns: product_analytic_vertical.xTeleconverter
LR_CA_model_5 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xStrap, data = LR_CA_data)
summary(LR_CA_model_5)
vif(LR_CA_model_5)
## Slightly High VIF and Less significant p-value columns: product_analytic_vertical.xStrap, product_analytic_vertical.xCameraFilmRolls
LR_CA_model_6 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xTelescope, data = LR_CA_data)
summary(LR_CA_model_6)
vif(LR_CA_model_6)
## Insignificant p-value columns: product_analytic_vertical.xTelescope
## less significant p-value columns: Online_Marketing_adstock, product_analytic_vertical.xExtensionTube
LR_CA_model_7 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella, data = LR_CA_data)
summary(LR_CA_model_7)
vif(LR_CA_model_7)
## Less significant p-value columns: product_analytic_vertical.xReflectorUmbrella
LR_CA_model_8 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger, data = LR_CA_data)
summary(LR_CA_model_8)
vif(LR_CA_model_8)
## Less significant p-value columns: product_analytic_vertical.xCameraBatteryCharger
LR_CA_model_9 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score, data = LR_CA_data)
summary(LR_CA_model_9)
vif(LR_CA_model_9)
## Removing the "units" variable and then check the Adjusted R-squared value
LR_CA_model_10 <- lm(formula = gmv ~ product_mrp + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score, data = LR_CA_data)
summary(LR_CA_model_10) # Adjusted R-squared value is changed at 3rd place of decimal, we'good to remove that variable
vif(LR_CA_model_10)
### Cross-validation
cv.lm(data = LR_CA_data, form.lm = LR_CA_model_10, m=5, dots = FALSE, seed=29, plotit=TRUE, printit=TRUE)
### Estimating the elasticity coefficients
elasticity_3 <- function(var){
LR_CA_elasticity <-as.numeric(LR_CA_model_10$coefficients[var]*mean(LR_CA_data[,var])/mean(LR_CA_data$gmv))
return(LR_CA_elasticity)
}
LR_CA_var_list <- list()
for(i in 2:length(LR_CA_model_10$coefficients)){
LR_CA_var_list[i-1] <-elasticity_3(names(LR_CA_model_10$coefficients)[i])
}
LR_CA_elasticity.outputs <- data.frame(names(LR_CA_model_10$coefficients[2:length(LR_CA_model_10$coefficients)]))
LR_CA_elasticity.outputs <- cbind(LR_CA_elasticity.outputs,do.call(rbind.data.frame, LR_CA_var_list))
colnames(LR_CA_elasticity.outputs) <- c("Variable","Elasticity")
LR_CA_elasticity.outputs$Direction <- ifelse(LR_CA_elasticity.outputs$Elasticity > 0, "Positive", "Negative")
# Plotting the elasticity
ggplot(LR_CA_elasticity.outputs, aes(x=reorder(Variable,Elasticity),y=Elasticity, fill = Direction)) +
geom_bar(position="dodge",stat="identity", width = 0.9) + theme_base() + coord_flip() +
scale_fill_manual(values=c(Positive="green3",Negative="red")) + geom_text(aes(label=Variable, y=-0.1),hjust = 1, color="black", size=5) +
theme(plot.title = element_text(hjust = 0.5), axis.title.y=element_blank(),axis.text.y=element_blank()) +
ggtitle("CameraAccessory - Linear Regression Model") +xlab("Variables")
#********************************************************[Multiplicative Model]
### Preparing dataset
## Removing lag variables and Moving averages variables
## Also removing "list_price" and "promotional_offer" column as those are on dependent variable (i.e. gmv)
## Retaining those is not good idea as 'gmv' won't present in unseen data
MM_CA_data <- CameraAccessory_final[,-c(21:22,77:91)]
## Replacing 0 value in column with '0.00001' as log(0) is undefined
MM_CA_data[MM_CA_data == 0] <- 0.00001
## Taking log of all the variable to buils to Multiplicative model
MM_CA_data <- log(MM_CA_data)
## Checking the variables for linear relationship or multicollinearity
MM_CA_model <- lm(gmv~.,MM_CA_data)
alias(MM_CA_model)
## Removing the variables which were showing linear relationship or multicollinearity
MM_CA_data <- MM_CA_data[, -c(54:72)]
### Stepwise Regression to remove insignificant and correlated variables
MM_CA_base.mod <- lm(gmv ~ 1 , data= MM_CA_data) # base intercept only model
MM_CA_all.mod <- lm(gmv ~ . , data= MM_CA_data) # full model with all predictors
MM_CA_stepMod <- step(MM_CA_base.mod, scope = list(lower = MM_CA_base.mod, upper = MM_CA_all.mod), direction = "both", trace = 1, steps = 1000) # perform step-wise algorithm
MM_CA_shortlistedVars <- names(unlist(MM_CA_stepMod[[1]])) # get the shortlisted variable.
MM_CA_shortlistedVars <- MM_CA_shortlistedVars[!MM_CA_shortlistedVars %in% "(Intercept)"] # remove intercept
### Model Building::
## Building First model after short listing the variables[using MM_CA_stepMod]
MM_CA_model_1 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + deliverycdays +
product_analytic_vertical.xTelescope + week + product_analytic_vertical.xFlashShoeAdapter +
SEM_adtock + Sponsorship, data = MM_CA_data)
summary(MM_CA_model_1)
vif(MM_CA_model_1)
## Insignificant p-value columns: product_analytic_vertical.xFlashShoeAdapter, deliverycdays, week
MM_CA_model_2 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + product_analytic_vertical.xTelescope +
SEM_adtock + Sponsorship, data = MM_CA_data)
summary(MM_CA_model_2)
vif(MM_CA_model_2)
## High VIF and insignificant p-value columns: product_analytic_vertical.xTelescope
MM_CA_model_3 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + SEM_adtock + Sponsorship, data = MM_CA_data)
summary(MM_CA_model_3)
vif(MM_CA_model_3)
## Insignificant p-value columns: SEM_adtock, Sponsorship
## Less significant p-value columns: product_analytic_vertical.xCameraEyeCup
MM_CA_model_4 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units, data = MM_CA_data)
summary(MM_CA_model_4)
vif(MM_CA_model_4)
## High VIF value columns: units
MM_CA_model_5 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount, data = MM_CA_data)
summary(MM_CA_model_5)
vif(MM_CA_model_5)
## High VIF value columns: product_analytic_vertical.xCameraMount, product_analytic_vertical.xCameraBatteryCharger
MM_CA_model_6 <- lm(formula = gmv ~ product_mrp + product_procurement_sla +
product_analytic_vertical.xCameraTripod , data = MM_CA_data)
summary(MM_CA_model_6)
vif(MM_CA_model_6)
### Cross-validation
cv.lm(data = MM_CA_data, form.lm = MM_CA_model_6, m=5, dots = FALSE, seed=29, plotit=TRUE, printit=TRUE)
### Estimating the elasticity coefficients
elasticity_6 <- function(var){
MM_CA_elasticity <- as.numeric(MM_CA_model_6$coefficients[var]*mean(MM_CA_data[,var])/mean(MM_CA_data$gmv))
return(MM_CA_elasticity)
}
MM_CA_var_list <- list()
for(i in 2:length(MM_CA_model_6$coefficients)){
MM_CA_var_list[i-1] <- elasticity_6(names(MM_CA_model_6$coefficients)[i])
}
MM_CA_elasticity.outputs <- data.frame(names(MM_CA_model_6$coefficients[2:length(MM_CA_model_6$coefficients)]))
MM_CA_elasticity.outputs <- cbind(MM_CA_elasticity.outputs,do.call(rbind.data.frame, MM_CA_var_list))
colnames(MM_CA_elasticity.outputs) <- c("Variable","Elasticity")
MM_CA_elasticity.outputs$Direction <- ifelse(MM_CA_elasticity.outputs$Elasticity > 0, "Positive", "Negative")
# Plotting the elasticity
ggplot(MM_CA_elasticity.outputs, aes(x=reorder(Variable,Elasticity),y=Elasticity, fill = Direction)) +
geom_bar(position="dodge",stat="identity") + theme_base() + coord_flip() +
scale_fill_manual(values=c(Positive="green3",Negative="red")) + geom_text(aes(label=Variable, y=-1),hjust = 0.1, color="black", size=5) +
theme(plot.title = element_text(hjust = 0.5), axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
ggtitle("CameraAccessory - Multiplicative Model") +xlab("Variables")
#********************************************************[Koyck Model]
### Preparing dataset
## Removing lag variables and Moving averages variables but considering the 1 week lag value of 'gmv'
## Also removing "list_price" and "promotional_offer" column as those are on dependent variable (i.e. gmv)
## Retaining those is not good idea as 'gmv' won't present in unseen data
KM_CA_data <- CameraAccessory_final[,-c(21:22,77:88,90:91)]
## Scaling the variables
KM_CA_data[,2:ncol(KM_CA_data)] <- scale(KM_CA_data[,2:ncol(KM_CA_data)])
## Checking the variables for linear relationship or multicollinearity
KM_CA_model <- lm(gmv~.,KM_CA_data)
alias(KM_CA_model)
## Removing the variables which were showing linear relationship or multicollinearity
KM_CA_data <- KM_CA_data[, -c(54:72)]
### Stepwise Regression to remove insignificant and correlated variables
KM_CA_base.mod <- lm(gmv ~ 1 , data= KM_CA_data) # base intercept only model
KM_CA_all.mod <- lm(gmv ~ . , data= KM_CA_data) # full model with all predictors
KM_CA_stepMod <- step(KM_CA_base.mod, scope = list(lower = KM_CA_base.mod, upper = KM_CA_all.mod), direction = "both", trace = 1, steps = 1000) # perform step-wise algorithm
KM_CA_shortlistedVars <- names(unlist(KM_CA_stepMod[[1]])) # get the shortlisted variable.
KM_CA_shortlistedVars <- KM_CA_shortlistedVars[!KM_CA_shortlistedVars %in% "(Intercept)"] # remove intercept
### Model Building::
## Building First model after short listing the variables[using KM_CA_stepMod]
KM_CA_model_1 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xCameraMount + week + Content_Marketing_adstock +
Sponsorship + product_analytic_vertical.xTeleconverter +
sla + product_analytic_vertical.xCameraBatteryGrip + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + Total_Investment + product_analytic_vertical.xStrap +
product_analytic_vertical.xCameraEyeCup, data = KM_CA_data)
summary(KM_CA_model_1)
vif(KM_CA_model_1)
## Insignificant p-value columns: product_analytic_vertical.xCameraEyeCup, product_analytic_vertical.xCameraBatteryGrip, sla
## High VIF and Insignificant p-value columns: product_analytic_vertical.xStrap
KM_CA_model_2 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xCameraMount + week + Content_Marketing_adstock +
Sponsorship + product_analytic_vertical.xTeleconverter +
product_analytic_vertical.xTelescope + product_analytic_vertical.xCameraFilmRolls +
product_analytic_vertical.xCameraBattery + Content_Marketing + Total_Investment, data = KM_CA_data)
summary(KM_CA_model_2)
vif(KM_CA_model_2)
## High VIF and Insignificant p-value columns: product_analytic_vertical.xCameraRemoteControl
## Slightly high VIF and Insignificant p-value columns: deliverycdays, product_analytic_vertical.xTelescope
KM_CA_model_3 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
week + Content_Marketing_adstock + Sponsorship + product_analytic_vertical.xTeleconverter +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + Total_Investment, data = KM_CA_data)
summary(KM_CA_model_3)
vif(KM_CA_model_3)
## Slightly High VIF and Less significant p-value columns: week
KM_CA_model_4 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
Content_Marketing_adstock + Sponsorship + product_analytic_vertical.xTeleconverter +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + Total_Investment, data = KM_CA_data)
summary(KM_CA_model_4)
vif(KM_CA_model_4)
## High VIF and insignificant p-value columns: product_analytic_vertical.xCameraBattery
## less significant p-value columns: product_analytic_vertical.xTeleconverter
KM_CA_model_5 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
Content_Marketing_adstock + Sponsorship + product_analytic_vertical.xCameraFilmRolls +
Content_Marketing + Total_Investment, data = KM_CA_data)
summary(KM_CA_model_5)
vif(KM_CA_model_5)
## High VIF and Insignificant p-value columns: Total_Investment, Content_Marketing
## Less significant p-value columns: product_analytic_vertical.xCameraFilmRolls
KM_CA_model_6 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
Content_Marketing_adstock + Sponsorship, data = KM_CA_data)
summary(KM_CA_model_6)
vif(KM_CA_model_6)
## Less significant p-value columns: product_analytic_vertical.xCameraMount, product_analytic_vertical.xReflectorUmbrella,
## product_analytic_vertical.xExtensionTube
KM_CA_model_7 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
Online_Marketing_adstock + Content_Marketing_adstock + Sponsorship, data = KM_CA_data)
summary(KM_CA_model_7)
vif(KM_CA_model_7)
## Less significant p-value columns: Online_Marketing_adstock, Content_Marketing_adstock
KM_CA_model_8 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger + Sponsorship, data = KM_CA_data)
summary(KM_CA_model_8)
vif(KM_CA_model_8)
## Removing "Sponsorship" variable and will check the change in Adjusted R-squared value
KM_CA_model_9 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger , data = KM_CA_data)
summary(KM_CA_model_9) # Slight change at 3rd place of decimal in Adjusted R-squared value
vif(KM_CA_model_9)
## Removing "product_analytic_vertical.xCameraBatteryCharger" variable and will check the change in Adjusted R-squared value
KM_CA_model_10 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score, data = KM_CA_data)
summary(KM_CA_model_10) # Slight change at 3rd place of decimal in Adjusted R-squared value
vif(KM_CA_model_10)
## Removing "units" [because of high VIF] variable
KM_CA_model_11 <- lm(formula = gmv ~ product_mrp + product_analytic_vertical.xCameraTripod +
NPS_Score, data = KM_CA_data)
summary(KM_CA_model_11)
vif(KM_CA_model_11)
##Futher removing the variable lead to decreasing in Adjusted R-squared and increased in residual error
### Cross-validation
cv.lm(data = KM_CA_data, form.lm = KM_CA_model_11, m=5, dots = FALSE, seed=29, plotit=TRUE, printit=TRUE)
### Estimating the elasticity coefficients
elasticity_9 <- function(var){
KM_CA_elasticity <- as.numeric(KM_CA_model_11$coefficients[var]*mean(KM_CA_data[,var])/mean(KM_CA_data$gmv))
return(KM_CA_elasticity)
}
KM_CA_var_list <- list()
for(i in 2:length(KM_CA_model_11$coefficients)){
KM_CA_var_list[i-1] <- elasticity_9(names(KM_CA_model_11$coefficients)[i])
}
KM_CA_elasticity.outputs <- data.frame(names(KM_CA_model_11$coefficients[2:length(KM_CA_model_11$coefficients)]))
KM_CA_elasticity.outputs <- cbind(KM_CA_elasticity.outputs,do.call(rbind.data.frame, KM_CA_var_list))
colnames(KM_CA_elasticity.outputs) <- c("Variable","Elasticity")
KM_CA_elasticity.outputs$Direction <- ifelse(KM_CA_elasticity.outputs$Elasticity > 0, "Positive", "Negative")
# Plotting the elasticity
ggplot(KM_CA_elasticity.outputs, aes(x=reorder(Variable,Elasticity),y=Elasticity, fill = Direction)) +
geom_bar(position="dodge",stat="identity") + theme_base() + coord_flip() +
scale_fill_manual(values=c(Positive="green3",Negative="red")) + geom_text(aes(label=Variable, y=-0.1),hjust = 1, color="black", size=5) +
theme(plot.title = element_text(hjust = 0.5), axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
ggtitle("CameraAccessory - Koyck Model") +xlab("Variables")
#********************************************************[Distributive Lag Model]
### Preparing dataset
## Removing lag variables and Moving averages variables but considering the 1, 2 and 3 weeks lag value of 'gmv'
## Also removing "list_price" and "promotional_offer" column as those are on dependent variable (i.e. gmv)
## Retaining those is not good idea as 'gmv' won't present in unseen data
DL_CA_data <- CameraAccessory_final[,-c(21:22,77:88)]
## Scaling the variables
DL_CA_data[,2:ncol(DL_CA_data)] <- scale(DL_CA_data[,2:ncol(DL_CA_data)])
## Checking the variables for linear relationship or multicollinearity
DL_CA_model <- lm(gmv~.,DL_CA_data)
alias(DL_CA_model)
## Removing the variables which were showing linear relationship or multicollinearity
DL_CA_data <- DL_CA_data[, -c(54:72)]
### Stepwise Regression to remove insignificant and correlated variables
DL_CA_base.mod <- lm(gmv ~ 1 , data= DL_CA_data) # base intercept only model
DL_CA_all.mod <- lm(gmv ~ . , data= DL_CA_data) # full model with all predictors
DL_CA_stepMod <- step(DL_CA_base.mod, scope = list(lower = DL_CA_base.mod, upper = DL_CA_all.mod), direction = "both", trace = 1, steps = 1000) # perform step-wise algorithm
DL_CA_shortlistedVars <- names(unlist(DL_CA_stepMod[[1]])) # get the shortlisted variable.
DL_CA_shortlistedVars <- DL_CA_shortlistedVars[!DL_CA_shortlistedVars %in% "(Intercept)"] # remove intercept
### Model Building::
## Building First model after short listing the variables[using DL_CA_stepMod]
DL_CA_model_1 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xCameraMount + week + Content_Marketing_adstock +
Sponsorship + product_analytic_vertical.xTeleconverter +
sla + product_analytic_vertical.xCameraBatteryGrip + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing + Total_Investment + product_analytic_vertical.xStrap +
product_analytic_vertical.xCameraEyeCup, data = DL_CA_data)
summary(DL_CA_model_1)
vif(DL_CA_model_1)
## Insignificant p-value columns: product_analytic_vertical.xCameraEyeCup
## High VIF and Insignificant p-value columns: product_analytic_vertical.xStrap, Total_Investment
DL_CA_model_2 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + product_analytic_vertical.xCameraRemoteControl +
Online_Marketing_adstock + product_analytic_vertical.xExtensionTube +
product_analytic_vertical.xCameraMount + week + Content_Marketing_adstock +
Sponsorship + product_analytic_vertical.xTeleconverter +
sla + product_analytic_vertical.xCameraBatteryGrip + product_analytic_vertical.xTelescope +
product_analytic_vertical.xCameraFilmRolls + product_analytic_vertical.xCameraBattery +
Content_Marketing, data = DL_CA_data)
summary(DL_CA_model_2)
vif(DL_CA_model_2)
## High VIF and Insignificant p-value columns: Content_Marketing, product_analytic_vertical.xCameraBattery
## Insignificant p-value columns: product_analytic_vertical.xCameraBatteryGrip
## Slightly High VIF and Insignificant p-value columns: product_analytic_vertical.xCameraRemoteControl
DL_CA_model_3 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
week + Content_Marketing_adstock + Sponsorship + product_analytic_vertical.xTeleconverter +
sla + product_analytic_vertical.xTelescope + product_analytic_vertical.xCameraFilmRolls, data = DL_CA_data)
summary(DL_CA_model_3)
vif(DL_CA_model_3)
## Insignificant p-value columns: sla
## Slightly high VIF and Less significant p-value columns: product_analytic_vertical.xTelescope
DL_CA_model_4 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
deliverycdays + NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
week + Content_Marketing_adstock + Sponsorship + product_analytic_vertical.xTeleconverter +
product_analytic_vertical.xCameraFilmRolls, data = DL_CA_data)
summary(DL_CA_model_4)
vif(DL_CA_model_4)
## Slightly high VIF and Insignificant p-value columns: week
## Insignificant p-value columns: product_analytic_vertical.xTeleconverter
## less significant p-value columns: deliverycdays
DL_CA_model_5 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
product_analytic_vertical.xReflectorUmbrella + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + product_analytic_vertical.xCameraMount +
Content_Marketing_adstock + Sponsorship +
product_analytic_vertical.xCameraFilmRolls, data = DL_CA_data)
summary(DL_CA_model_5)
vif(DL_CA_model_5)
## Less significant p-value columns: product_analytic_vertical.xCameraFilmRolls, product_analytic_vertical.xCameraMount,
## product_analytic_vertical.xReflectorUmbrella
DL_CA_model_6 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger + Online_Marketing_adstock +
product_analytic_vertical.xExtensionTube + Content_Marketing_adstock +
Sponsorship, data = DL_CA_data)
summary(DL_CA_model_6)
vif(DL_CA_model_6)
## Less significant p-value columns: product_analytic_vertical.xExtensionTube, Online_Marketing_adstock
DL_CA_model_7 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
Content_Marketing_adstock + Sponsorship, data = DL_CA_data)
summary(DL_CA_model_7)
vif(DL_CA_model_7)
## Insignificant p-value columns: Content_Marketing_adstock
DL_CA_model_8 <- lm(formula = gmv ~ product_mrp + units + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
Sponsorship, data = DL_CA_data)
summary(DL_CA_model_8)
vif(DL_CA_model_8)
## High VIF value columns: units
DL_CA_model_9 <- lm(formula = gmv ~ product_mrp + product_analytic_vertical.xCameraTripod +
NPS_Score + product_analytic_vertical.xCameraBatteryCharger +
Sponsorship, data = DL_CA_data)
summary(DL_CA_model_9)
vif(DL_CA_model_9)
## Insignificant p-value columns: product_analytic_vertical.xCameraBatteryCharger, Sponsorship
DL_CA_model_10 <- lm(formula = gmv ~ product_mrp + product_analytic_vertical.xCameraTripod +
NPS_Score, data = DL_CA_data)
summary(DL_CA_model_10)
vif(DL_CA_model_10)
##Futher removing the variable lead to decreasing in Adjusted R-squared and increased in residual error
### Cross-validation
cv.lm(data = DL_CA_data, form.lm = DL_CA_model_10, m=5, dots = FALSE, seed=29, plotit=TRUE, printit=TRUE)
### Estimating the elasticity coefficients
elasticity_12 <- function(var){
DL_CA_elasticity <- as.numeric(DL_CA_model_10$coefficients[var]*mean(DL_CA_data[,var])/mean(DL_CA_data$gmv))
return(DL_CA_elasticity)
}
DL_CA_var_list <- list()
for(i in 2:length(DL_CA_model_10$coefficients)){
DL_CA_var_list[i-1] <- elasticity_12(names(DL_CA_model_10$coefficients)[i])
}
DL_CA_elasticity.outputs <- data.frame(names(DL_CA_model_10$coefficients[2:length(DL_CA_model_10$coefficients)]))
DL_CA_elasticity.outputs <- cbind(DL_CA_elasticity.outputs,do.call(rbind.data.frame, DL_CA_var_list))
colnames(DL_CA_elasticity.outputs) <- c("Variable","Elasticity")
DL_CA_elasticity.outputs$Direction <- ifelse(DL_CA_elasticity.outputs$Elasticity > 0, "Positive", "Negative")
# Plotting the elasticity
ggplot(DL_CA_elasticity.outputs, aes(x=reorder(Variable,Elasticity),y=Elasticity, fill = Direction)) +
geom_bar(position="dodge",stat="identity", width = 0.8) + theme_base() + coord_flip() +
scale_fill_manual(values=c(Positive="green3",Negative="red")) + geom_text(aes(label=Variable, y=-0.1),hjust = 1, color="black", size=5) +
theme(plot.title = element_text(hjust = 0.5), axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
ggtitle("CameraAccessory - Distributive Lag Model") +xlab("Variables")
#********************************************************[Multiplicative + Distributive Lag Model]
### Preparing dataset
## Removing lag variables and Moving averages variables but considering the 1, 2 and 3 weeks lag value of 'gmv'
## Also removing "list_price" and "promotional_offer" column as those are on dependent variable (i.e. gmv)
## Retaining those is not good idea as 'gmv' won't present in unseen data
MD_CA_data <- CameraAccessory_final[,-c(21:22,77:88)]
## Replacing 0 value in column with '0.00001' as log(0) is undefined
MD_CA_data[MD_CA_data == 0] <- 0.00001
## Tranforming the negative values
MD_CA_data$GMV_lag_1_per <- 1 + MD_CA_data$GMV_lag_1_per - min(MD_CA_data$GMV_lag_1_per)
MD_CA_data$GMV_lag_2_per <- 1 + MD_CA_data$GMV_lag_2_per - min(MD_CA_data$GMV_lag_2_per)
MD_CA_data$GMV_lag_3_per <- 1 + MD_CA_data$GMV_lag_3_per - min(MD_CA_data$GMV_lag_3_per)
## Taking log of all the variable to buils to Multiplicative model
MD_CA_data <- log(MD_CA_data)
## Checking the variables for linear relationship or multicollinearity
MD_CA_model <- lm(gmv~.,MD_CA_data)
alias(MD_CA_model)
## Removing the variables which were showing linear relationship or multicollinearity
MD_CA_data <- MD_CA_data[, -c(54:72)]
### Stepwise Regression to remove insignificant and correlated variables
MD_CA_base.mod <- lm(gmv ~ 1 , data= MD_CA_data) # base intercept only model
MD_CA_all.mod <- lm(gmv ~ . , data= MD_CA_data) # full model with all predictors
MD_CA_stepMod <- step(MD_CA_base.mod, scope = list(lower = MD_CA_base.mod, upper = MD_CA_all.mod), direction = "both", trace = 1, steps = 1000) # perform step-wise algorithm
MD_CA_shortlistedVars <- names(unlist(MD_CA_stepMod[[1]])) # get the shortlisted variable.
MD_CA_shortlistedVars <- MD_CA_shortlistedVars[!MD_CA_shortlistedVars %in% "(Intercept)"] # remove intercept
### Model Building::
## Building First model after short listing the variables[using MD_CA_stepMod]
MD_CA_model_1 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + deliverycdays +
product_analytic_vertical.xTelescope + week + product_analytic_vertical.xFlashShoeAdapter +
SEM_adtock + Sponsorship, data = MD_CA_data)
summary(MD_CA_model_1)
vif(MD_CA_model_1)
## Insignificant p-value columns: product_analytic_vertical.xFlashShoeAdapter, deliverycdays, week
MD_CA_model_2 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + product_analytic_vertical.xTelescope +
SEM_adtock + Sponsorship, data = MD_CA_data)
summary(MD_CA_model_2)
vif(MD_CA_model_2)
## High VIF and insignificant p-value columns: product_analytic_vertical.xTelescope
MD_CA_model_3 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units + product_analytic_vertical.xCameraEyeCup + SEM_adtock + Sponsorship, data = MD_CA_data)
summary(MD_CA_model_3)
vif(MD_CA_model_3)
## Insignificant p-value columns: SEM_adtock, Sponsorship
## Less significant p-value columns: product_analytic_vertical.xCameraEyeCup
MD_CA_model_4 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger + product_analytic_vertical.xCameraMount +
units, data = MD_CA_data)
summary(MD_CA_model_4)
vif(MD_CA_model_4)
## High VIF value columns: units, product_analytic_vertical.xCameraMount
MD_CA_model_5 <- lm(formula = gmv ~ product_mrp + product_procurement_sla + product_analytic_vertical.xCameraTripod +
product_analytic_vertical.xCameraBatteryCharger, data = MD_CA_data)
summary(MD_CA_model_5)
vif(MD_CA_model_5)
## High VIF value column: product_analytic_vertical.xCameraBatteryCharger
MD_CA_model_6 <- lm(formula = gmv ~ product_mrp + product_procurement_sla +
product_analytic_vertical.xCameraTripod, data = MD_CA_data)
summary(MD_CA_model_6)
vif(MD_CA_model_6)
### Cross-validation
cv.lm(data = MD_CA_data, form.lm = MD_CA_model_6, m=5, dots = FALSE, seed=29, plotit=TRUE, printit=TRUE)
### Estimating the elasticity coefficients
elasticity_15 <- function(var){
MD_CA_elasticity <- as.numeric(MD_CA_model_6$coefficients[var]*mean(MD_CA_data[,var])/mean(MD_CA_data$gmv))
return(MD_CA_elasticity)
}
MD_CA_var_list <- list()
for(i in 2:length(MD_CA_model_6$coefficients)){
MD_CA_var_list[i-1] <- elasticity_15(names(MD_CA_model_6$coefficients)[i])
}
MD_CA_elasticity.outputs <- data.frame(names(MD_CA_model_6$coefficients[2:length(MD_CA_model_6$coefficients)]))
MD_CA_elasticity.outputs <- cbind(MD_CA_elasticity.outputs,do.call(rbind.data.frame, MD_CA_var_list))
colnames(MD_CA_elasticity.outputs) <- c("Variable","Elasticity")
MD_CA_elasticity.outputs$Direction <- ifelse(MD_CA_elasticity.outputs$Elasticity > 0, "Positive", "Negative")
# Plotting the elasticity
ggplot(MD_CA_elasticity.outputs, aes(x=reorder(Variable,Elasticity),y=Elasticity, fill = Direction)) +
geom_bar(position="dodge",stat="identity") + theme_base() + coord_flip() +
scale_fill_manual(values=c(Positive="green3",Negative="red")) + geom_text(aes(label=Variable, y=-0.5),hjust = 0.1, color="black", size=5) +
theme(plot.title = element_text(hjust = 0.5), axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
ggtitle("CameraAccessory - Multiplicative and Distributive Lag Model") +xlab("Variables")