-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project-first.R
1660 lines (1427 loc) · 60 KB
/
Project-first.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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Importing required Libraries#
library(dplyr)
library(cran)
library(tidyverse)
library(lattice)
library(caret)
library(ggplot2)
library(corrplot)
library(forecast)
library(zoo)
library(lmtest)
library(fpp2)
library(minpack.lm)
library(numDeriv)
library(reshape2)
library(deSolve)
library(DIMORA)
library(splines)
library(foreach)
library(gam)
#---Importing the Complete Data set---#
data <- read.csv("oil data2.csv", sep=";")
colnames(data)[2] <- "country_code"
data$country_code <- as.factor(data$country)
colnames(data)[3] <- "indicator"
data$indicator <- as.factor(data$indicator)
data$Country.Name <- NULL
data$Indicator.Code <- NULL
levels(data$indicator)
data[,3:ncol(data)] <- sapply(data[,3:ncol(data)], as.numeric)
head(data,10)
# oil production #
Production <- data %>% filter (data$indicator == "Oil production (TWh)")
Production$indicator <- NULL
Years <- data.frame(c(1960:2021))
coun = Production$country_code
Production <- as.data.frame(t(as.matrix(Production)))
colnames(Production) <- coun
Production <- Production[-1,]
rownames(Production) <- NULL
Production <- cbind(Production, Years)
colnames(Production)[ncol(Production)] <- "Years"
Production <- as.data.frame(sapply(Production, as.numeric))
Production
# Exploring #
options(repr.plot.width=25, repr.plot.height=6)
ggplot(Production, aes(x=Years))+
geom_line(aes(y=USA, color="USA"),na.rm = TRUE)+
geom_line(aes(y=RUS, color="RUS"),na.rm = TRUE)+
geom_line(aes(y=SAU, color="SAU"),na.rm = TRUE)+
geom_line(aes(y=CHN, color="CHN"),na.rm = TRUE)+
geom_line(aes(y=IRN, color="IRN"),na.rm = TRUE)+
geom_line(aes(y=IRQ, color="IRQ"),na.rm = TRUE)+
geom_line(aes(y=ARE, color="ARE"),na.rm = TRUE)+
geom_line(aes(y=KWT, color="KWT"),na.rm = TRUE)+
geom_line(aes(y=CAN, color="CAN"),na.rm = TRUE)+
scale_color_discrete(name = "Countries")+
ggtitle("Oil Production in TWh")+
ylab("Production")+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE))
# GDP #
GDP <- data %>% filter (data$indicator == "GDP (current US$)")
GDP$indicator <- NULL
Years <- data.frame(c(1960:2021))
coun = GDP$country_code
GDP <- as.data.frame(t(as.matrix(GDP)))
colnames(GDP) <- coun
GDP <- GDP[-1,]
rownames(GDP) <- NULL
GDP <- cbind(GDP, Years)
colnames(GDP)[ncol(GDP)] <- "Years"
GDP <- as.data.frame(sapply(GDP, as.numeric))
head(GDP)
# exploring #
options(repr.plot.width=25, repr.plot.height=6)
ggplot(GDP, aes(x=Years))+
geom_line(aes(y=USA, color="USA"),na.rm = TRUE)+
geom_line(aes(y=RUS, color="RUS"),na.rm = TRUE)+
geom_line(aes(y=SAU, color="SAU"),na.rm = TRUE)+
geom_line(aes(y=CHN, color="CHN"),na.rm = TRUE)+
geom_line(aes(y=IRN, color="IRN"),na.rm = TRUE)+
geom_line(aes(y=IRQ, color="IRQ"),na.rm = TRUE)+
geom_line(aes(y=ARE, color="ARE"),na.rm = TRUE)+
geom_line(aes(y=KWT, color="KWT"),na.rm = TRUE)+
geom_line(aes(y=CAN, color="CAN"),na.rm = TRUE)+
scale_color_discrete(name = "Countries")+
ylab("GDP")+
ggtitle("GDP in USD$")
# Oil Consumption #
Consumption <- data %>% filter (data$indicator == "Oil Consumption TWh")
Consumption$indicator <- NULL
Years <- data.frame(c(1960:2021))
coun = Consumption$country_code
Consumption <- as.data.frame(t(as.matrix(Consumption)))
colnames(Consumption) <- coun
Consumption <- Consumption[-1,]
rownames(Consumption) <- NULL
Consumption <- cbind(Consumption, Years)
colnames(Consumption)[ncol(Consumption)] <- "Years"
Consumption <- as.data.frame(sapply(Consumption, as.numeric))
head(Consumption)
# exploring #
options(repr.plot.width=25, repr.plot.height=6)
ggplot(Consumption, aes(x=Years))+
geom_line(aes(y=USA, color="USA"),na.rm = TRUE)+
geom_line(aes(y=RUS, color="RUS"),na.rm = TRUE)+
geom_line(aes(y=SAU, color="SAU"),na.rm = TRUE)+
geom_line(aes(y=CHN, color="CHN"),na.rm = TRUE)+
geom_line(aes(y=IRN, color="IRN"),na.rm = TRUE)+
geom_line(aes(y=IRQ, color="IRQ"),na.rm = TRUE)+
geom_line(aes(y=ARE, color="ARE"),na.rm = TRUE)+
geom_line(aes(y=KWT, color="KWT"),na.rm = TRUE)+
geom_line(aes(y=CAN, color="CAN"),na.rm = TRUE)+
scale_color_discrete(name = "Countries")+
ylab("Oil Consumption")+
ggtitle("Oil Consumption in TWh")
# Oil Reserves #
Reserves <- data %>% filter (data$indicator == "Oil reserves")
Reserves$indicator <- NULL
Years <- data.frame(c(1960:2021))
coun = Reserves$country_code
Reserves <- as.data.frame(t(as.matrix(Reserves)))
colnames(Reserves) <- coun
Reserves <- Reserves[-1,]
rownames(Reserves) <- NULL
Reserves <- cbind(Reserves, Years)
colnames(Reserves)[ncol(Reserves)] <- "Years"
Reserves <- as.data.frame(sapply(Reserves, as.numeric))
Reserves[c(21:61),]
typeof(Reserves)
head(Reserves)
# exploring #
options(repr.plot.width=25, repr.plot.height=6)
ggplot(Reserves[c(21:61),], aes(x=Years))+
geom_line(aes(y=USA, color="USA"),na.rm = TRUE)+
geom_line(aes(y=RUS, color="RUS"),na.rm = TRUE)+
geom_line(aes(y=SAU, color="SAU"),na.rm = TRUE)+
geom_line(aes(y=CHN, color="CHN"),na.rm = TRUE)+
geom_line(aes(y=IRN, color="IRN"),na.rm = TRUE)+
geom_line(aes(y=IRQ, color="IRQ"),na.rm = TRUE)+
geom_line(aes(y=ARE, color="ARE"),na.rm = TRUE)+
geom_line(aes(y=KWT, color="KWT"),na.rm = TRUE)+
geom_line(aes(y=CAN, color="CAN"),na.rm = TRUE)+
scale_color_discrete(name = "Countries")+
ylab("Reserves")+
ggtitle("Total reserves of each country by 2020")
#-------------------------------------DATA PREPARATION------------------------------------------#
#---Making dataframe for selected countries---#
# USA #
production <- Production$USA
consumption <- Consumption$USA
reserves <- Reserves$USA
gdp <- GDP$USA
years <- Production$Years
USA <- cbind.data.frame(production, consumption, reserves, gdp, years)
USA
min(USA$years)
typeof(USA)
USA_ts <- ts(USA[-5],start = min(USA$years),end=max(USA$years))
# checking on NAn value #
cat("NAs in Production:",sum(is.na(USA$production)),
"\nNAs in GDP:",sum(is.na(USA$gdp)),
"\nNAs in Consumption:", sum(is.na(USA$consumption)),
"\nNAs in Reserves:", sum(is.na(USA$reserves)),
"\nNAs in years:", sum(is.na(USA$years)))
USA = USA[complete.cases(USA),]
#complete.cases() function in R Language is used to return a logical vector with cases which are complete, i.e., no missing value
row.names(USA) <- NULL #reset the index
head(USA)
USA
Production$USA
USA$production
Year$USA
# Plotting time series for USA #
options(repr.plot.width=8, repr.plot.height=7)
par(mfrow=c(1,3))
plot(USA_ts)
# Checking on the ACF of USA #
options(repr.plot.width=10, repr.plot.height=10)
par(mfrow=c(2,2))
acf(USA$production)
acf(USA$consumption)
acf(USA$gdp)
acf(USA$reserves)
# Plottig all the variables of the USA dataset #
options(repr.plot.width=25, repr.plot.height=6)
norm.USA <- cbind.data.frame(scale(USA[-5]), USA$years)
ggplot(norm.USA, aes(x=USA$years))+
geom_line(aes(y=production, color="production"),na.rm = TRUE) +
geom_line(aes(y=gdp, color="GDP"),na.rm = TRUE)+
geom_line(aes(y=consumption, color="consumption"),na.rm = TRUE)+
geom_line(aes(y=reserves, color="reserves"),na.rm = TRUE)+
scale_color_discrete(name = "Features", labels = c(
"#CD5C5C"="production", "#4B0082"="gdp", "#000080"="consumption", "#7FFF00"="reserves"))+
ggtitle("Features after Normalization")
# Saudi Arabia #
production <- Production$SAU
consumption <- Consumption$SAU
reserves <- Reserves$SAU
gdp <- GDP$SAU
years <- Production$Years
SAU <- cbind.data.frame(production, consumption, reserves, gdp, years)
SAU
min(SAU$years)
typeof(SAU)
SAU_ts <- ts(SAU[-5],start = min(SAU$years),end=max(SAU$years))
# checking on NAn value #
cat("NAs in Production:",sum(is.na(SAU$production)),
"\nNAs in GDP:",sum(is.na(SAU$gdp)),
"\nNAs in Consumption:", sum(is.na(SAU$consumption)),
"\nNAs in Reserves:", sum(is.na(SAU$reserves)),
"\nNAs in years:", sum(is.na(SAU$years)))
SAU = SAU[complete.cases(SAU),]
row.names(SAU) <- NULL #reset the index
head(SAU)
# Plotting time series for Saudi Arabia #
options(repr.plot.width=8, repr.plot.height=7)
par(mfrow=c(1,3))
plot(SAU_ts)
# Checking on the ACF of Saudi Arabia #
par(mfrow=c(2,2))
acf(SAU$production)
acf(SAU$consumption)
acf(SAU$gdp)
acf(SAU$reserves)
# Plottig all the variables of the Saudi Arabia dataset #
par(mfrow=c(1,1))
options(repr.plot.width=25, repr.plot.height=6)
norm.SAU <- cbind.data.frame(scale(SAU[-5]), SAU$years)
ggplot(norm.SAU, aes(x=SAU$years))+
geom_line(aes(y=production, color="production"),na.rm = TRUE) +
geom_line(aes(y=gdp, color="GDP"),na.rm = TRUE)+
geom_line(aes(y=consumption, color="consumption"),na.rm = TRUE)+
geom_line(aes(y=reserves, color="reserves"),na.rm = TRUE)+
scale_color_discrete(name = "Features", labels = c(
"#CD5C5C"="production", "#4B0082"="gdp", "#000080"="consumption", "#7FFF00"="reserves"))+
ggtitle("Features after Normalization")
# Iran #
production <- Production$IRN
consumption <- Consumption$IRN
reserves <- Reserves$IRN
gdp <- GDP$IRN
years <- Production$Years
IRN <- cbind.data.frame(production, consumption, reserves, gdp, years)
IRN
# filling the years 1991 and 1992 of gdp with average of the years 1990 and 1993
IRN$gdp[c(32,33)] = c(94278460000, 94278460000)
# filling the year 2021 of gdp with value of 2020
IRN$gdp[c(62)] = c(231547600000)
# filling the year 2021 of reserve with value of 2020
IRN$reserves[c(62)] = c(21523920416)
IRN_ts <- ts(IRN[-5],start = min(IRN$years),end=max(IRN$years))
# checking on NAn value #
cat("NAs in Production:",sum(is.na(IRN$production)),
"\nNAs in GDP:",sum(is.na(IRN$gdp)),
"\nNAs in Consumption:", sum(is.na(IRN$consumption)),
"\nNAs in Reserves:", sum(is.na(IRN$reserves)),
"\nNAs in years:", sum(is.na(IRN$years)))
IRN = IRN[complete.cases(IRN),]
row.names(IRN) <- NULL #reset the index
head(IRN)
IRN_ts
# Plotting time series for Iran #
options(repr.plot.width=8, repr.plot.height=7)
par(mfrow=c(1,3))
plot(IRN_ts)
# Checking on the ACF of Iran #
par(mfrow=c(2,2))
acf(IRN$production)
acf(IRN$consumption)
acf(IRN$gdp)
acf(IRN$reserves)
# Plottig all the variables of the Iran dataset #
par(mfrow=c(1,1))
options(repr.plot.width=25, repr.plot.height=6)
norm.IRN <- cbind.data.frame(scale(IRN[-5]), IRN$years)
ggplot(norm.IRN, aes(x=IRN$years))+
geom_line(aes(y=production, color="production"),na.rm = TRUE) +
geom_line(aes(y=gdp, color="GDP"),na.rm = TRUE)+
geom_line(aes(y=consumption, color="consumption"),na.rm = TRUE)+
geom_line(aes(y=reserves, color="reserves"),na.rm = TRUE)+
scale_color_discrete(name = "Features", labels = c(
"#CD5C5C"="production", "#4B0082"="gdp", "#000080"="consumption", "#7FFF00"="reserves"))+
ggtitle("Features after Normalization")
############ A plot including all the things together ##########
autoplot(USA_ts[,1], series = 'United States', lwd = 0.7) +
autolayer(SAU_ts[,1], series = 'Saudi Arabia', lwd = 0.7)+
autolayer(IRN_ts[,1], series = 'Iran', lwd = 0.7)+
scale_color_manual(values = c('#6F1D1B', '#CD5C5C', '#000080'))+
labs(title = 'Crude Oil Production Plot', x = '\nYear', y = 'Value\n',
subtitle = 'in United States, Saudi Arabia and Iran')+
geom_hline(yintercept = 2009.201, linetype = 'longdash', color = '#6F1D1B')+
annotate('text', x = 1960, y = 2000, label = 'Avg. Value for Iran:\n2009.201', size = 3, color = '#6F1D1B')+
annotate('text', x = 1970, y = 600, label = '1960 - Lowest crude oil value for Iran\n(622.1003)', size = 3, fontface = 'italic',
color = '#6F1D1B')+
annotate(geom = 'curve', x = 1970, y = 600, xend = 1961, yend = 600, curvature = -0.1, arrow = arrow(length = unit(0.5, 'cm')),
color = '#6F1D1B')+
annotate('text', x = 1965, y = 3000, label = '1974 - Highest crude\noil value for Iran (3526.183)', size = 3, fontface = 'italic',
color = '#6F1D1B')+
annotate(geom = 'curve', x = 1974, y = 3570, xend = 1965, yend = 3250, curvature = 0.3, arrow = arrow(length = unit(0.5, 'cm')),
color = '#6F1D1B')+
geom_hline(yintercept = 5178.294, linetype = 'longdash', color = '#000080')+
annotate('text', x = 1962, y = 5200, label = 'Avg. Value for United States:\n5178.294', size = 3, color = '#000080')+
annotate('text', x = 1997, y = 2800, label = '2008 - Lowest crude\noil value for United States (3514.844) and from this year\nCrude Oil production starts to increase until the year 2014.', size = 3, fontface = 'italic',
color = '#000080')+
annotate(geom = 'curve', x = 2008, y = 3514.844, xend = 2000, yend = 3000, curvature = 0.1, arrow = arrow(length = unit(0.5, 'cm')),
color = '#000080')+
annotate('text', x = 2010, y = 8300, label = '2019 - Highest crude\noil value for United States (8721.28)', size = 3, fontface = 'italic',
color = '#000080')+
annotate(geom = 'curve', x = 2019, y = 8760, xend = 2010, yend = 8500, curvature = 0.2, arrow = arrow(length = unit(0.5, 'cm')),
color = '#000080')+
annotate('text', x = 1985, y = 6810, label = 'From 1985 the Oil production value in USA\ncontinues to decrease for the next 22 years until the year 2007 to reach\nUSAs lowest value.', size = 3, fontface = 'italic',
color = '#000080')+
annotate(geom = 'curve', x = 1985, y = 5850, xend = 1985, yend = 6500, curvature = 0.2, arrow = arrow(length = unit(0.5, 'cm')),
color = '#000080')+
geom_hline(yintercept = 4361.518, linetype = 'longdash', color = '#CD5C5C')+
annotate('text', x = 2020, y = 4380, label = 'Avg. Value for Saudi Arabia:\n4361.518', size = 3, color = '#CD5C5C')+
annotate('text', x = 1960, y = 1300, label = '1960 - Lowest crude\noil value for Saudi Arabia (750.4141)', size = 2.7, fontface = 'italic',
color = '#CD5C5C')+
annotate('text', x = 2005, y = 7300, label = '2016- Highest crude\noil value for Saudi Arabia (6823.463)', size = 3, fontface = 'italic',
color = '#CD5C5C')+
annotate(geom = 'curve', x = 2016, y = 6850, xend = 2005, yend = 6990, curvature = -0.2, arrow = arrow(length = unit(0.5, 'cm')),
color = '#CD5C5C')+
theme(panel.background = element_rect(fill = '#EDE0D4', colour = '#EDE0D4'),
plot.background = element_rect(fill = '#EDE0D4', colour = '#EDE0D4'),
plot.title = element_text(size = 22, colour = 'gray15', face = 'bold', hjust = 0.5, vjust = 0.5),
plot.subtitle = element_text(size = 14, colour = 'gray15', face = 'italic', hjust = 0.5, vjust = 0.5),
plot.caption = element_text(color = 'gray25', face = 'italic', size = 12),
panel.grid.major.y = element_line(linetype = 'dotted', colour = 'gray70'),
panel.grid.minor.y = element_line(linetype = 'dotted', colour = 'gray75'),
panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),
axis.title = element_text(size = 14, face = 'bold', colour = 'gray15'),
axis.text = element_text(size = 12, colour = 'gray8'), legend.position = 'bottom',
axis.line = element_line(), legend.text = element_text(size = 10),
legend.title = element_text(colour = 'gray5', size = 14, face = 'bold'),
legend.background = element_rect(fill = '#EDE0D4', linetype = 'blank'))
#####################
####Correlations#####
#####################
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
####USA####
corr_USA <- cor(USA[-5], use="pairwise.complete.obs")
corrplot(corr_USA, method="color", col=col(200),
type="lower", order="hclust",
addCoef.col = "black",
tl.col="black", tl.srt=40,
sig.level = 0.01, insig = "blank",
diag=FALSE)
####SAUDI ARABIA#####
corr_SAU <- cor(SAU[-5], use="pairwise.complete.obs")
corrplot(corr_SAU, method="color", col=col(200),
type="lower", order="hclust",
addCoef.col = "black",
tl.col="black", tl.srt=40,
sig.level = 0.01, insig = "blank",
diag=FALSE)
####IRAN####
corr_IRN <- cor(IRN[-5], use="pairwise.complete.obs")
corrplot(corr_IRN, method="color", col=col(200),
type="lower", order="hclust",
addCoef.col = "black",
tl.col="black", tl.srt=40,
sig.level = 0.01, insig = "blank",
diag=FALSE)
#-- <- <- <- <- <- <- <- <- Splitting the data into Train and Test sets <- <- <- <- <- <- <- <- #
set.seed(1)
train_usa <- USA %>% filter(USA$years >= min(USA$years) & USA$years<=2008)
test_usa <- USA %>% filter(USA$years >= 2008)
train_sau <- SAU %>% filter(SAU$years >= min(SAU$years) & SAU$years<=2016)
test_sau <- SAU %>% filter(SAU$years >= 2016)
train_irn <- IRN %>% filter(IRN$years >= min(IRN$years) & IRN$years<=2016)
test_irn <- IRN %>% filter(IRN$years >= 2016 )
#----------------------------------Modelling and the Analysis--------------------------------------#
#---------------------------------------------------------#
#------------------ Linear Regression---------------------#
#---------------------------------------------------------#
#---------Linear Regression model for USA-------#
#Fitting linear model using only with the highest correlated variable with the target column#
MLR_USA_res <- tslm(production ~reserves, data=window(USA_ts, start=1980, end=2018 -.1))
summary(MLR_USA_res)
# On train set of USA #
LM1_USA_res <- lm(production~reserves, data=train_usa)
summary(LM1_USA_res)
# Stepwise Regression
LM2_USA_res <- step(LM1_USA_res, direction="both")
summary(LM2_USA_res)
AIC(MLR_USA_res)
AIC(LM1_USA_res)
AIC(LM2_USA_res)
accuracy(LM2_USA_res, test_usa)
fit_LM_USA_res <- fitted.values(LM2_USA_res)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
usa_fit <- cbind.data.frame(train_usa$production,fit_LM_USA_res, train_usa$years)
colnames(usa_fit) <- c("production", "fitted","years")
ggplot(usa_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
usa_pred <- predict(LM2_USA_res, test_usa)
summary(usa_pred)
usa_pred <- cbind.data.frame(test_usa$production, usa_pred, test_usa$years)
colnames(usa_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=usa_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=usa_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = usa_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = usa_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for UNITED STATES")
# Useful plots #
par(mfrow=c(2,2))
plot(LM2_USA_res)
par(mfrow=c(1,1))
#analysis of residuals
residuals_usa_res<- residuals(LM2_USA_res)
plot(residuals_usa_res, type = "b")
#the form of residuals indicates the presence of positive autocorrelation
Acf(residuals_usa_res)
dw<- dwtest(LM2_USA_res, alt="two.sided")
dw
###### <- <- Analysis on the degree polynomial of the variables <- <- <- <- <- ##############
############
##reserves##
############
LM_USA_res1 <- lm(production~reserves, data=train_usa)
summary(LM_USA_res1)
ggplot(train_usa, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'reserves of usa 1', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_res1)
par(mfrow=c(1,1))
#with 2 degrees
LM_USA_res2 <- lm(production~poly(reserves,2), data=train_usa)
summary(LM_USA_res2)
ggplot(train_usa, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'reserves of usa 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_res2)
par(mfrow=c(1,1))
LM_USA_res_poly <- lm(data=train_usa, production ~ reserves + I(reserves**2) + I(reserves**3))
summary(LM_USA_res_poly)
ggplot(train_usa, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'reserves of usa degree 5', y = 'production in TWh')
anova(LM_USA_res_poly) ##degree equal to 1 is better and significant
# result for reserves: we will go on with degree 1 #
###############
##consumption##
###############
LM_USA_con1 <- lm(production~consumption, data=train_usa)
summary(LM_USA_con1)
ggplot(train_usa, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'consumption in TWh 1', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_con1)
par(mfrow=c(1,1))
# seems degree 2 would be good idea let's check it #
LM_USA_con2 <- lm(production~ poly(consumption,2), data=train_usa)
summary(LM_USA_con2)
ggplot(train_usa, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'consumption in TWh 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_con2)
par(mfrow=c(1,1))
LM_USA_cons_multi <- lm(data=train_usa, production ~ consumption + I(consumption**2) + I(consumption**3))
summary(LM_USA_cons_multi)
ggplot(train_usa, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'consumption in TWh 3', y = 'production in TWh')
anova(LM_USA_cons_multi) ##degree equal to 1 is better and significant for consumption also
# result for conumption: we will go on with degree 1 #
###############
######gdp######
###############
LM_USA_gdp1 <- lm(production~gdp, data=train_usa)
summary(LM_USA_gdp1)
ggplot(train_usa, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'GDP 1', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_gdp1)
par(mfrow=c(1,1))
# seems polynomial degree equal to 2 would be good idea #
LM_USA_gdp2 <- lm(production~ poly(gdp,2), data=train_usa)
summary(LM_USA_gdp2)
ggplot(train_usa, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'GDP 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_USA_gdp2)
anova(LM_USA_gdp1, LM_USA_gdp2)
# degree 2 better fits the data #
par(mfrow=c(1,1))
LM_USA_gdp_multi <- lm(data=train_usa, production ~ gdp + I(gdp**2) + I(gdp**3))
ggplot(train_usa, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'GDP 3', y = 'production in TWh')
summary(LM_USA_gdp_multi)
anova(LM_USA_gdp2, LM_USA_gdp_multi)
#result for gdp : we will go on with degree 2 #
######## <- <- <- <- <- <- <- <- <- ##########
#Fitting linear model using all the variable with their best degree of polynomial#
MLR_USA_full <- tslm(production ~ consumption + poly(gdp,2) + reserves, data=window(USA_ts, start=1980, end=2018 -.1))
summary(MLR_USA_full)
# On train set of USA #
LM_USA_full <- lm(production~consumption + poly(gdp,2) + reserves, data=train_usa)
summary(LM_USA_full)
# Stepwise Regression
LM2_USA_full <- step(LM_USA_full, direction="both")
summary(LM2_USA_full)
AIC(MLR_USA_full)
AIC(LM_USA_full)
AIC(LM2_USA_full)
fit_LM_USA <- fitted.values(LM2_USA_full)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
usa_fit <- cbind.data.frame(train_usa$production,fit_LM_USA, train_usa$years)
colnames(usa_fit) <- c("production", "fitted","years")
ggplot(usa_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
usa_pred <- predict(LM2_USA_full, test_usa)
usa_pred <- cbind.data.frame(test_usa$production, usa_pred, test_usa$years)
colnames(usa_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=usa_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=usa_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = usa_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = usa_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for UNITED STATES with full model")
# Useful plots #
par(mfrow=c(2,2))
plot(LM2_USA_full)
par(mfrow=c(1,1))
#analysis of residuals
residuals_usa<- residuals(LM2_USA_full)
plot(residuals_usa, type = "b")
#the form of residuals indicates the presence of positive autocorrelation
Acf(residuals_usa)
dw<- dwtest(LM2_USA_full, alt="two.sided")
dw
#------------Linear Regression model for Saudi Arabia ---------#
#Fitting linear model using only with the highest correlated variable with the target column#
MLR_SAU_gdp <- tslm(formula = production ~ gdp, data=window(SAU_ts, start=1980, end=2009 -.1))
summary(MLR_SAU_gdp)
# Linear Model On train set of Saudi Arabia #
LM1_SAU_gdp <- lm(formula= production ~ gdp, data=train_sau)
summary(LM1_SAU_gdp)
# Stepwise Regression
LM2_SAU_gdp <- step(LM1_SAU_gdp, direction="both")
summary(LM2_SAU_gdp)
AIC(MLR_SAU_gdp)
AIC(LM1_SAU_gdp)
AIC(LM2_SAU_gdp)
fit_LM_SAU_gdp <- fitted.values(LM2_SAU_gdp)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
sau_fit <- cbind.data.frame(train_sau$production,fit_LM_SAU_gdp, train_sau$years)
colnames(sau_fit) <- c("production", "fitted","years")
ggplot(sau_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
sau_pred <- predict(LM2_SAU_gdp, test_sau)
sau_pred <- cbind.data.frame(test_sau$production, sau_pred, test_sau$years)
colnames(sau_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=sau_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=sau_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = sau_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = sau_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for SAUDI ARABIA contatining only gdp")
par(mfrow=c(2,2))
plot(LM2_SAU_gdp)
par(mfrow=c(1,1))
#analysis of residuals
residuals_sau_gdp<- residuals(LM2_SAU_gdp)
plot(residuals_sau_gdp, type = "b")
#the form of residuals indicates the presence of positive autocorrelation
Acf(residuals_sau_gdp)
dw<- dwtest(LM2_SAU_gdp, alt="two.sided")
dw
###### <- <- Analysis on the degree polynomial of the variables <- <- <- <- <- ##############
############
##reserves##
############
LM_SAU_res1 <- lm(production~reserves, data=train_sau)
summary(LM_SAU_res1)
ggplot(train_sau, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'reserves of sau 1', y = 'production in TWh')
# From what is obvious from the plot the higher degree of polynomial would not be a good idea.
# Although that we are going to check higher degrees.
# Diagnostic
par(mfrow=c(2,2))
plot(LM_SAU_res1)
par(mfrow=c(1,1))
#with 2 degrees
LM_SAU_res2 <- lm(production~poly(reserves,2), data=train_sau)
summary(LM_SAU_res2)
# Diagnostic
par(mfrow=c(2,2))
plot(LM_SAU_res2)
par(mfrow=c(1,1))
ggplot(train_sau, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'reserves of sau 2', y = 'production in TWh')
anova(LM_SAU_res1, LM_SAU_res2)
##degree equal to 1 is better and is significant#
# result for reserves: we will go on with degree 1 #
###############
##consumption##
###############
LM_SAU_con1 <- lm(production~consumption, data=train_sau)
summary(LM_SAU_con1)
ggplot(train_sau, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'consumption in TWh 1', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_SAU_con1)
par(mfrow=c(1,1))
# seems degree 2 or 3 would be good idea let's check them #
LM_SAU_con2 <- lm(production~ poly(consumption,2), data=train_sau)
summary(LM_SAU_con2)
ggplot(train_sau, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'consumption in TWh 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_SAU_con2)
anova(LM_SAU_con1, LM_SAU_con2)
par(mfrow=c(1,1))
LM_SAU_cons_multi <- lm(data=train_sau, production ~ consumption + I(consumption**2) + I(consumption**3))
summary(LM_SAU_cons_multi)
ggplot(train_sau, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'consumption in TWh 3', y = 'production in TWh')
anova(LM_SAU_cons_multi, LM_SAU_con1) ##degree equal to 1 is better and significant for consumption also
# result for consumption: we will go on with degree 1 #
###############
#####GDP#######
###############
LM_SAU_gdp1 <- lm(production~gdp, data=train_sau)
summary(LM_SAU_gdp1)
ggplot(train_sau, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'GDP 1', y = 'production in TWh')
# seems degree 2 would be good idea let's check it #
LM_SAU_gdp2 <- lm(production~ poly(gdp,2), data=train_sau)
summary(LM_SAU_gdp2)
ggplot(train_sau, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'GDP 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_SAU_gdp2)
anova(LM_SAU_gdp1, LM_SAU_gdp2)
par(mfrow=c(1,1))
# degree 2 is better than 1 but to make sure we try degree 3 too #
LM_SAU_gdp_multi <- lm(data=train_sau, production ~ gdp + I(gdp**2) + I(gdp**3))
summary(LM_SAU_gdp_multi)
ggplot(train_sau, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'GDP 3', y = 'production in TWh')
anova(LM_SAU_gdp_multi, LM_SAU_gdp2) ##degree equal to 1 is better and significant for gdp
# result for gdp: we will go on with degree 1 #
######## <- <- <- <- <- <- <- <- <- ##########
#Fitting linear model using all the variable with their best degree of polynomial#
MLR_SAU_full <- tslm(production ~ consumption + gdp + reserves, data=window(SAU_ts, start=1980, end=2009 -.1))
summary(MLR_SAU_full)
# On train set of SAUDI ARABIA #
LM_SAU_full <- lm(production~consumption + gdp + reserves, data=train_sau)
summary(LM_SAU_full)
# Stepwise Regression
LM2_SAU_full <- step(LM_SAU_full, direction="both")
summary(LM2_SAU_full)
AIC(MLR_SAU_full)
AIC(LM_SAU_full)
AIC(LM2_SAU_full)
fit_LM_SAU <- fitted.values(LM2_SAU_full)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
sau_fit <- cbind.data.frame(train_sau$production,fit_LM_SAU, train_sau$years)
colnames(sau_fit) <- c("production", "fitted","years")
ggplot(sau_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
sau_pred <- predict(LM2_SAU_full, test_sau)
sau_pred <- cbind.data.frame(test_sau$production, sau_pred, test_sau$years)
colnames(sau_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=sau_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=sau_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = sau_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = sau_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for SAUDI ARABIA with full model")
# Useful plots #
par(mfrow=c(2,2))
plot(LM2_SAU_full)
par(mfrow=c(1,1))
#analysis of residuals
residuals_sau<- residuals(LM2_SAU_full)
plot(residuals_sau, type = "b")
#the form of residuals indicates the presence of positive autocorrelation
Acf(residuals_sau)
dw<- dwtest(LM2_SAU_full, alt="two.sided")
dw
#-----------------Linear Regression model for IRAN -----------#
#Fitting linear model using only with the highest correlated variable with the target column#
MLR_IRN_cons <- tslm(formula = production ~ consumption, data=window(IRN_ts, start=1980, end=2009 -.1))
summary(MLR_IRN_cons)
# Linear Model On train set of IRAN #
LM1_IRN_cons <- lm(formula= production ~ consumption, data=train_irn)
summary(LM1_IRN_cons)
# Stepwise Regression
LM2_IRN_cons <- step(LM1_IRN_cons, direction="both")
summary(LM2_IRN_cons)
AIC(MLR_IRN_cons)
AIC(LM1_IRN_cons)
AIC(LM2_IRN_cons)
fit_LM_IRN_cons <- fitted.values(LM2_IRN_cons)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
irn_fit <- cbind.data.frame(train_irn$production,fit_LM_IRN_cons, train_irn$years)
colnames(irn_fit) <- c("production", "fitted","years")
ggplot(irn_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
irn_pred <- predict(LM2_IRN_cons, test_irn)
irn_pred <- cbind.data.frame(test_irn$production, irn_pred, test_irn$years)
colnames(irn_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=irn_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=irn_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = irn_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = irn_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for IRAN containing only consumption variable.")
par(mfrow=c(2,2))
plot(LM2_IRN_cons)
par(mfrow=c(1,1))
#analysis of residuals
residuals_irn_cons<- residuals(LM2_IRN_cons)
plot(residuals_irn_cons, type = "b")
Acf(residuals_irn_cons)
dw<- dwtest(LM2_IRN_cons, alt="two.sided")
dw
###### <- <- Analysis on the degree polynomial of the variables <- <- <- <- <- ##############
############
##reserves##
############
LM_IRN_res1 <- lm(production~reserves, data=train_irn)
summary(LM_IRN_res1)
ggplot(train_irn, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'reserves of irn 1', y = 'production in TWh')
# From what is obvious from the plot the higher degree of polynomial would not be a good idea.
# Although that we are going to check higher degrees.
# Diagnostic
par(mfrow=c(2,2))
plot(LM_IRN_res1)
par(mfrow=c(1,1))
#with 2 degrees
LM_IRN_res2 <- lm(production~poly(reserves,2), data=train_irn)
summary(LM_IRN_res2)
ggplot(train_irn, aes(x = reserves, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'reserves of irn 2', y = 'production in TWh')
# there is not much improvement #
# Diagnostic
par(mfrow=c(2,2))
plot(LM_IRN_res2)
par(mfrow=c(1,1))
anova(LM_IRN_res1, LM_IRN_res2)
##degree equal to 1 is better and is significant#
# result for reserves: we will go on with degree 1 #
###############
##consumption##
###############
LM_IRN_con1 <- lm(production~consumption, data=train_irn)
summary(LM_IRN_con1)
ggplot(train_irn, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'consumption in TWh 1', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_IRN_con1)
par(mfrow=c(1,1))
# seems degree 2 or 3 would be good idea let's check them #
LM_IRN_con2 <- lm(production~ poly(consumption,2), data=train_irn)
summary(LM_IRN_con2)
ggplot(train_irn, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'consumption in TWh 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_IRN_con2)
# Residuals plot is now better #
anova(LM_IRN_con1, LM_IRN_con2)
par(mfrow=c(1,1))
LM_IRN_cons_multi <- lm(data=train_irn, production ~ consumption + I(consumption**2) + I(consumption**3))
summary(LM_IRN_cons_multi)
ggplot(train_irn, aes(x = consumption, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'consumption in TWh 3', y = 'production in TWh')
anova(LM_IRN_cons_multi, LM_IRN_con1) ##degree equal to 2 is better and significant for consumption also
# result for conumption: we will go on with degree 2 #
###############
######GDP######
###############
LM_IRN_gdp1 <- lm(production~gdp, data=train_irn)
summary(LM_IRN_gdp1)
ggplot(train_irn, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ x) + labs(x = 'GDP 1', y = 'production in TWh')
# seems degree 2 would be good idea let's check it #
LM_IRN_gdp2 <- lm(production~ poly(gdp,2), data=train_irn)
summary(LM_IRN_gdp2)
ggplot(train_irn, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,2)) + labs(x = 'GDP 2', y = 'production in TWh')
# Diagnostic
par(mfrow=c(2,2))
plot(LM_IRN_gdp2)
anova(LM_IRN_gdp1, LM_IRN_gdp2)
par(mfrow=c(1,1))
# degree 2 is not better than 1 but to make sure we try degree 3 too #
LM_IRN_gdp_multi <- lm(data=train_irn, production ~ gdp + I(gdp**2) + I(gdp**3))
summary(LM_IRN_gdp_multi)
ggplot(train_irn, aes(x = gdp, y = production)) + geom_point() + stat_smooth(method = "lm", formula = y ~ poly(x,3)) + labs(x = 'GDP 3', y = 'production in TWh')
anova(LM_IRN_gdp_multi, LM_IRN_gdp2) ##degree equal to 1 is better and significant for gdp
# gdp variable seems not to be helpful variable for IRAN analysis We will try not to have this variable in the main functions to see what happens.
######## <- < <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- - <- <- <- <- <- <- <- ##########
#Fitting linear model using all the variable with their best degree of polynomial#
MLR_IRN_full <- tslm(production ~ poly(consumption,2) + reserves, data=window(IRN_ts, start=1980, end=2009 -.1))
summary(MLR_IRN_full)
# On train set of IRAN #
LM_IRN_full <- lm(production~poly(consumption,2) + reserves, data=train_irn)
summary(LM_IRN_full)
# Stepwise Regression
LM2_IRN_full <- step(LM_IRN_full, direction="both")
summary(LM2_IRN_full)
AIC(MLR_IRN_full)
AIC(LM_IRN_full)
AIC(LM2_IRN_full)
fit_LM_IRN <- fitted.values(LM2_IRN_full)
# Plotting the Fitted and Real values #
options(repr.plot.width=25, repr.plot.height=6)
irn_fit <- cbind.data.frame(train_irn$production,fit_LM_IRN, train_irn$years)
colnames(irn_fit) <- c("production", "fitted","years")
ggplot(irn_fit, aes(x=years))+
geom_line(aes(y=production, color="Real"),na.rm = TRUE) +
geom_line(aes(y=fitted, color="Fitted"),na.rm = TRUE)
# Predicting of test set #
irn_pred <- predict(LM2_IRN_full, test_irn)
irn_pred <- cbind.data.frame(test_irn$production, irn_pred, test_irn$years)
colnames(irn_pred) <- c("production", "pred", "years")
# Plotting everything together #
ggplot() +
geom_line(data=irn_fit, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data=irn_fit, aes(x=years, y=fitted, color='Fitted'), na.rm=TRUE) +
geom_line(data = irn_pred, aes(x=years, y=production, color='Actual'), na.rm=TRUE) +
geom_line(data = irn_pred, aes(x=years, y=pred, color='Predicted'), na.rm=TRUE) +
labs(title = "Actual, fitted and predicted values for IRAN with chosen variables.")
# Useful plots #
par(mfrow=c(2,2))
plot(LM2_IRN_full)
par(mfrow=c(1,1))
#analysis of residuals
residuals_irn<- residuals(LM2_IRN_full)
plot(residuals_irn, type = "b")
Acf(residuals_irn)
dw<- dwtest(LM2_IRN_full, alt="two.sided")
dw