1 Introduction

Hello everyone !!!

The Dataset is Indian climate, this dataset provides data from 1st January 2013 to 24th April 2017 in the city of Delhi, India. The 4 parameters here are meantemp, humidity, wind_speed, meanpressure. At this moment we will build some models for predict temperature using this dataset.

Source: https://www.kaggle.com/sumanthvrao/daily-climate-time-series-data.

2 Data Preparation

2.1 Import Library

library(dplyr) # for data wrangling
library(lubridate) # date manipulation
library(TSstudio) # time series interactive viz
library(forecast) # time series library
library(tseries) # for adf.test

2.2 Read Data

From the dataset there are already train and data test, we don’t have to split them into two data anymore.

2.2.1 Import data train

climate_train <- read.csv("DailyDelhiClimateTrain.csv")
head(climate_train)

2.2.2 Import data test

climate_test <- read.csv("DailyDelhiClimateTest.csv")
head(climate_test)

2.3 Check data type

glimpse(climate_train)
## Rows: 1,462
## Columns: 5
## $ date         <chr> "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04", "…
## $ meantemp     <dbl> 10.000000, 7.400000, 7.166667, 8.666667, 6.000000, 7.0000…
## $ humidity     <dbl> 84.50000, 92.00000, 87.00000, 71.33333, 86.83333, 82.8000…
## $ wind_speed   <dbl> 0.0000000, 2.9800000, 4.6333333, 1.2333333, 3.7000000, 1.…
## $ meanpressure <dbl> 1015.667, 1017.800, 1018.667, 1017.167, 1016.500, 1018.00…

Time series is a method of analyzing and processing data in which the values are affected by time. The action of predicting future values based on its value in the previous period of time is called forecasting.

The data which formatted into a time series (ts) object must have some characteristics:

  • no missing intervals
  • no missing values
  • data should be ordered by time

2.4 Check Missing Value

We have to check if there is any missing values in our dataset. There is no missing value on our dataset.

anyNA(climate_train)
## [1] FALSE
anyNA(climate_test)
## [1] FALSE

2.5 Check data range

We have to check our data range. This is important for time series prediction, it can help us for choosing right frequency.

range(climate_train$date)
## [1] "2013-01-01" "2017-01-01"
range(climate_test$date)
## [1] "2017-01-01" "2017-04-24"

2.6 Check missing interval

interval(start = head(climate_train$date)[1], end = tail(climate_train$date)[1])
## [1] 2013-01-01 UTC--2016-12-27 UTC
interval(start = head(climate_test$date)[1], end = tail(climate_test$date)[1])
## [1] 2017-01-01 UTC--2017-04-19 UTC

3 Data Pre-processing

3.1 Feature Engineering

3.1.1 Selecting column

Take date and meantemp column form test data.

test_clean <- climate_test %>% 
                select(date, meantemp)

Take date and meantemp column for train data.

train_clean <- climate_train %>% 
                select(date, meantemp)

3.1.2 Create time series object for data train

After creating ts object, we can plot our object and see the graph.

ts_train <- ts(data = train_clean$meantemp,
                start = c(2013,01),
                frequency = 365)
autoplot(ts_train)

### Create time series object for data test

After creating ts object, we can plot our object and see the graph.

ts_test <- ts(data = test_clean$meantemp,
                start = c(2017,01),
                frequency = 365)
autoplot(ts_test)

### Decompose

Decomposing a time series means separating it into its constituent components.

climate_dc <- decompose(ts_train)
plot(climate_dc)

From our graph above we conclude our dataset have trend and seasonal. This is important to know what is our data characteristic, we choose their model based on their character.

4 Modeling

4.1 Holt-Winters Model

4.1.1 Create Holt-Winters Model

climate_hw <- HoltWinters(ts_train,seasonal = "additive")
climate_hw
## Holt-Winters exponential smoothing with trend and additive seasonal component.
## 
## Call:
## HoltWinters(x = ts_train, seasonal = "additive")
## 
## Smoothing parameters:
##  alpha: 0.7137593
##  beta : 0
##  gamma: 1
## 
## Coefficients:
##                [,1]
## a     23.2059477677
## b      0.0007786026
## s1   -11.9533349288
## s2   -11.5762825721
## s3   -11.9041600160
## s4   -12.4673985971
## s5   -13.1139452002
## s6   -13.2679980348
## s7   -12.0927171628
## s8   -12.0040252582
## s9   -12.1629632245
## s10  -11.3536225915
## s11  -10.6396502270
## s12  -10.9244341162
## s13  -12.2023082443
## s14  -11.8208475341
## s15  -11.9809092479
## s16  -12.3595377689
## s17  -11.2499619086
## s18  -10.5631695277
## s19  -10.6830829034
## s20  -10.2530733677
## s21   -8.3192899413
## s22  -10.9730260524
## s23  -11.5427618181
## s24  -10.0070860460
## s25   -8.2999617144
## s26   -8.7416898276
## s27   -8.4677778994
## s28   -9.0600250071
## s29  -10.1338854775
## s30  -10.4019601337
## s31   -9.4295025389
## s32   -8.3670653559
## s33   -6.2581761224
## s34   -8.0808034346
## s35   -6.0450090793
## s36   -5.3108976158
## s37   -7.5865490457
## s38   -9.7553237548
## s39  -11.2452627919
## s40  -10.8940612387
## s41  -11.4719643135
## s42  -10.6581443171
## s43  -10.3079056927
## s44  -11.4328879207
## s45  -10.8393024232
## s46   -9.9227974251
## s47   -9.3916367352
## s48   -8.1300258555
## s49   -8.4156611915
## s50   -8.9802447564
## s51   -8.2996711082
## s52   -8.2080520219
## s53   -7.9519158717
## s54   -6.9645287947
## s55   -7.0270992122
## s56   -6.7382962631
## s57   -7.4565983426
## s58   -8.6502919766
## s59   -8.4726645473
## s60   -7.9685752684
## s61   -6.5038121729
## s62   -5.3471479675
## s63   -6.5707991319
## s64   -6.8523081602
## s65   -5.2529790618
## s66   -4.2471714469
## s67   -4.2773166541
## s68   -3.6217120194
## s69   -3.9199882738
## s70   -4.7641132309
## s71   -4.9982634903
## s72   -5.3215391112
## s73   -2.3243702283
## s74   -0.0489139852
## s75    0.8287095893
## s76   -1.0539904019
## s77   -1.8188309462
## s78   -1.5213577882
## s79   -0.6763270501
## s80   -1.7286295819
## s81   -1.4552254464
## s82   -0.0870460591
## s83    1.0133614400
## s84   -1.8844810998
## s85   -0.4959709723
## s86   -0.1611906946
## s87   -0.2861216386
## s88    0.5119136478
## s89    0.5594564710
## s90    1.9055083684
## s91    0.7736105401
## s92    0.2874735219
## s93    1.7401169781
## s94    3.8978791928
## s95    2.8749412061
## s96    1.8764836804
## s97    1.3452973093
## s98    2.1449682793
## s99    3.3896488692
## s100   2.3750537775
## s101   2.5549376929
## s102   3.4481553428
## s103   4.4784711616
## s104   3.0398550535
## s105   3.3453754384
## s106   1.3692100367
## s107   1.4494491543
## s108   1.8569422000
## s109   2.4546922347
## s110   3.3967071127
## s111   3.7792473731
## s112   4.0146718372
## s113   4.8055676787
## s114   5.6905145197
## s115   6.5945276553
## s116   6.7054350373
## s117   7.4301289501
## s118   8.2082276991
## s119   8.6843783131
## s120   9.5191949459
## s121   9.2974868584
## s122   7.1585020351
## s123   5.8281464143
## s124   4.8619236819
## s125   6.6205391506
## s126   6.9521602675
## s127   6.9173386065
## s128   5.4291911876
## s129   5.0247321200
## s130   3.3997263614
## s131   1.5934387122
## s132   2.3658549047
## s133   2.9982081049
## s134   5.2832293845
## s135   5.9350053517
## s136   6.8680051893
## s137   6.5293020058
## s138   6.5699266641
## s139   7.9210253091
## s140   8.0309578702
## s141   9.4441246013
## s142   6.4769600061
## s143   6.8473218168
## s144   6.0976052793
## s145   6.6476488911
## s146   8.6722829529
## s147   8.9179983308
## s148   6.8998571019
## s149   7.6389530184
## s150   8.8858755586
## s151   9.4224076904
## s152   9.3214190501
## s153  10.3785264895
## s154  11.6129365008
## s155  11.7321711819
## s156  12.9742597250
## s157  13.0773319082
## s158  13.7336374337
## s159  12.6537257837
## s160  12.5244259441
## s161   9.4230855641
## s162   6.9139707707
## s163   5.1504174658
## s164   6.7441390888
## s165  10.4241196522
## s166  11.2718822812
## s167  10.7009301561
## s168  11.2848422771
## s169  11.4775912622
## s170   9.8531130998
## s171  10.8850017820
## s172   8.8956297307
## s173   9.7004026035
## s174   7.0987930356
## s175   7.5832051071
## s176   9.0330545663
## s177   9.8805629485
## s178   7.4723028343
## s179   8.6689953788
## s180   7.5623280054
## s181   6.0934620948
## s182   5.2419631882
## s183   7.5027760095
## s184   9.0499563770
## s185   6.6441181276
## s186   6.6313390415
## s187   7.0396502466
## s188   4.9554063102
## s189   5.1638339427
## s190   5.5732146319
## s191   4.9706176022
## s192   5.7298356183
## s193   6.4426321334
## s194   6.9907099829
## s195   6.1980189366
## s196   4.4928123653
## s197   5.2952589296
## s198   5.9053587583
## s199   5.9957710003
## s200   5.5689810379
## s201   4.9593291850
## s202   5.4650448895
## s203   4.7458979358
## s204   4.2199294455
## s205   4.8183038053
## s206   4.4896476528
## s207   4.9758398802
## s208   6.0434903401
## s209   5.7037950194
## s210   5.4537212437
## s211   5.8054185986
## s212   5.7787790245
## s213   7.5114805599
## s214   7.5948860121
## s215   6.4819715451
## s216   5.2333787564
## s217   3.0468465282
## s218   4.2747468316
## s219   2.9961360395
## s220   3.2228100972
## s221   4.3319326884
## s222   4.2540936984
## s223   5.8869488708
## s224   5.4796110302
## s225   3.5550045076
## s226   3.6424514220
## s227   3.3664532908
## s228   2.7446098485
## s229   3.7869321043
## s230   4.0451945775
## s231   3.5362403605
## s232   3.5104921707
## s233   4.9952009355
## s234   6.0453668971
## s235   5.6977932995
## s236   6.7409064533
## s237   6.9808835875
## s238   5.9597472457
## s239   6.6034506579
## s240   4.4753191687
## s241   4.4213899508
## s242   4.2051391622
## s243   5.8339030049
## s244   7.5213658430
## s245   7.2899572926
## s246   6.9280758478
## s247   6.6356022972
## s248   6.7489591631
## s249   5.9259955296
## s250   5.8501354077
## s251   4.8344277320
## s252   5.1458490263
## s253   6.1616193217
## s254   6.8212883727
## s255   6.6916483961
## s256   6.4973924494
## s257   6.4524716747
## s258   5.3653715364
## s259   5.1409524251
## s260   5.0087932246
## s261   4.4727614549
## s262   3.4559668948
## s263   3.1648168398
## s264   2.8799870948
## s265   4.6837842141
## s266   4.8273321839
## s267   4.4928378886
## s268   4.4542693853
## s269   4.8645259430
## s270   4.6829309214
## s271   3.6626447454
## s272   3.3021703468
## s273   3.9073705317
## s274   4.4910274289
## s275   2.9421406928
## s276   2.0864277195
## s277   2.9303872477
## s278   4.8394469754
## s279   4.5109509851
## s280   4.8199823843
## s281   5.0051276318
## s282   2.4010500898
## s283   1.8754428484
## s284   2.3176374802
## s285   2.1265396513
## s286   1.6169099697
## s287   0.9873977803
## s288   2.1792894935
## s289   1.8473194237
## s290   1.3663638352
## s291   0.8528675138
## s292   0.4692370578
## s293   0.9664914830
## s294   1.1850402291
## s295   0.1402880078
## s296   0.0771678449
## s297  -1.0688901165
## s298  -1.7143292854
## s299  -2.7912807931
## s300  -2.2447994532
## s301  -1.7857973235
## s302  -1.7844624737
## s303  -1.4698081180
## s304  -1.8018534827
## s305  -2.6327810541
## s306  -2.4587241997
## s307  -4.5685637093
## s308  -4.8210824941
## s309  -4.5437651957
## s310  -4.6760050128
## s311  -5.1714856088
## s312  -6.3913610337
## s313  -6.6534148763
## s314  -6.9981905854
## s315  -7.1679142705
## s316  -6.8108920683
## s317  -7.2805746103
## s318  -7.8987860702
## s319  -8.7769504021
## s320  -8.0435008251
## s321  -7.2348864422
## s322  -6.5457276185
## s323  -6.0133370448
## s324  -6.4546942688
## s325  -6.7360615844
## s326  -5.9696488834
## s327  -5.1028510358
## s328  -2.7756515001
## s329  -3.8883653732
## s330  -4.7552536689
## s331  -5.7897777464
## s332  -6.0363982296
## s333  -6.6339224993
## s334  -6.6840395647
## s335  -6.3950636429
## s336  -6.4529966861
## s337  -8.2525994742
## s338  -8.2628335893
## s339  -8.2945842861
## s340  -8.0966685171
## s341  -8.7323577343
## s342  -6.9342845435
## s343  -8.7618964015
## s344  -7.8645195740
## s345  -8.1332433557
## s346  -8.1465438054
## s347  -8.4818888358
## s348  -8.6899633762
## s349 -10.5462730195
## s350  -9.7266582312
## s351  -9.8526220257
## s352  -9.0038355291
## s353  -8.8693315666
## s354  -9.9496447456
## s355  -9.9875774545
## s356 -10.9735567202
## s357 -10.3108537618
## s358 -11.6891438193
## s359 -10.4388489818
## s360 -11.9443450632
## s361 -12.7326185746
## s362 -13.3302856296
## s363 -12.1500512417
## s364 -10.7694930750
## s365 -13.2059477677

4.1.2 Forecast model

climate_forecast <- forecast(climate_hw, h=114)
climate_forecast
##           Point Forecast      Lo 80    Hi 80       Lo 95    Hi 95
## 2017.0055      11.253391  8.8448898 13.66189  7.56990607 14.93688
## 2017.0082      11.631222  8.6721429 14.59030  7.10570078 16.15674
## 2017.0110      11.304124  7.8819279 14.72632  6.07032691 16.53792
## 2017.0137      10.741664  6.9119515 14.57138  4.88462432 16.59870
## 2017.0164      10.095896  5.8980430 14.29375  3.67583403 16.51596
## 2017.0192       9.942621  5.4064073 14.47884  3.00508059 16.88016
## 2017.0219      11.118681  6.2676490 15.96971  3.69966787 18.53769
## 2017.0247      11.208151  6.0615232 16.35478  3.33706280 19.07924
## 2017.0274      11.049992  5.6238466 16.47614  2.75141882 19.34857
## 2017.0301      11.860111  6.1681586 17.55206  3.15502087 20.56520
## 2017.0329      12.574862  6.6289731 18.52075  3.48140963 21.66831
## 2017.0356      12.290857  6.1014411 18.48027  2.82496228 21.75675
## 2017.0384      11.013761  4.5900444 17.43748  1.18953406 20.83799
## 2017.0411      11.396001  4.7462329 18.04577  1.22605851 21.56594
## 2017.0438      11.236718  4.3683347 18.10510  0.73243248 21.74100
## 2017.0466      10.858868  3.7786166 17.93912  0.03055815 21.68718
## 2017.0493      11.969222  4.6832612 19.25518  0.82630651 23.11214
## 2017.0521      12.656793  5.1707729 20.14281  1.20791326 24.10567
## 2017.0548      12.537658  4.8567880 20.21853  0.79078082 24.28454
## 2017.0575      12.968446  5.0975481 20.83934  0.93094620 25.00595
## 2017.0603      14.903008  6.8465631 22.95945  2.58173851 27.22428
## 2017.0630      12.250051  4.0122367 20.48787 -0.34859877 24.84870
## 2017.0658      11.681094  3.2658187 20.09637 -1.18895890 24.55115
## 2017.0685      13.217548  4.6284780 21.80662  0.08169883 26.35340
## 2017.0712      14.925451  6.1660334 23.68487  1.52907774 28.32182
## 2017.0740      14.484502  5.5579866 23.41102  0.83257496 28.13643
## 2017.0767      14.759192  5.6686508 23.84973  0.85640887 28.66198
## 2017.0795      14.167724  4.9160636 23.41938  0.01853058 28.31692
## 2017.0822      13.094642  3.6846213 22.50466 -1.29674265 27.48603
## 2017.0849      12.827346  3.2615861 22.39311 -1.80222116 27.45691
## 2017.0877      13.800582  4.0815784 23.51959 -1.06335123 28.66452
## 2017.0904      14.863798  4.9939294 24.73367 -0.23086328 29.95846
## 2017.0932      16.973466  6.9550040 26.99193  1.65155079 32.29538
## 2017.0959      15.151617  4.9867340 25.31650 -0.39422998 30.69746
## 2017.0986      17.188190  6.8789650 27.49741  1.42159109 32.95479
## 2017.1014      17.923080  7.4715064 28.37465  1.93877768 33.90738
## 2017.1041      15.648207  5.0561978 26.24022 -0.55087312 31.84729
## 2017.1068      13.480211  2.7496038 24.21082 -2.93083655 29.89126
## 2017.1096      11.991050  1.1236128 22.85849 -4.62926120 28.61136
## 2017.1123      12.343031  1.3404640 23.34560 -4.48394300 29.17000
## 2017.1151      11.765906  0.6298501 22.90196 -5.26522190 28.79703
## 2017.1178      12.580505  1.3125406 23.84847 -4.65235932 29.81337
## 2017.1205      12.931522  1.5331761 24.32987 -4.50074367 30.36379
## 2017.1233      11.807318  0.2800654 23.33457 -5.82209364 29.43673
## 2017.1260      12.401682  0.7469482 24.05642 -5.42269548 30.22606
## 2017.1288      13.318966  1.5381298 25.09980 -4.69826820 31.33620
## 2017.1315      13.850905  1.9453027 25.75651 -4.35714264 32.05895
## 2017.1342      15.113295  3.0842198 27.14237 -3.28358787 33.51018
## 2017.1370      14.828438  2.6771453 26.97973 -3.75536061 33.41224
## 2017.1397      14.264633  1.9923396 26.53693 -4.50422021 33.03349
## 2017.1425      14.945985  2.5538725 27.33810 -4.00611575 33.89809
## 2017.1452      15.038383  2.5275984 27.54917 -4.09521091 34.17198
## 2017.1479      15.295298  2.6669565 27.92364 -4.01808355 34.60868
## 2017.1507      16.283464  3.5386498 29.02828 -3.20804700 35.77497
## 2017.1534      16.221672  3.3614405 29.08190 -3.44635473 35.88970
## 2017.1562      16.511253  3.5366311 29.48588 -3.33171893 36.35423
## 2017.1589      15.793730  2.7057165 28.88174 -4.22265917 35.81012
## 2017.1616      14.600815  1.4003844 27.80125 -5.58750138 34.78913
## 2017.1644      14.779221  1.4673226 28.09112 -5.57957068 35.13801
## 2017.1671      15.284089  1.8616483 28.70653 -5.24376245 35.81194
## 2017.1699      16.749630  3.2175508 30.28171 -3.94589941 37.44516
## 2017.1726      17.907073  4.2662356 31.54791 -2.95478759 38.76893
## 2017.1753      16.684201  2.9354654 30.43294 -4.34267542 37.71108
## 2017.1781      16.403470  2.5476774 30.25926 -4.78713616 37.59408
## 2017.1808      18.003578  4.0415485 31.96561 -3.34950336 39.35666
## 2017.1836      19.010164  4.9427004 33.07763 -2.50416508 40.52449
## 2017.1863      18.980797  4.8086838 33.15291 -2.69358004 40.65518
## 2017.1890      19.637181  5.3611841 33.91318 -2.19607196 41.47043
## 2017.1918      19.339683  4.9605541 33.71881 -2.65129699 41.33066
## 2017.1945      18.496337  4.0148098 32.97786 -3.65124747 40.64392
## 2017.1973      18.262965  3.6797592 32.84617 -4.04012367 40.56605
## 2017.2000      17.940468  3.2562873 32.62465 -4.51704848 40.39798
## 2017.2027      20.938416  6.1539495 35.72288 -1.67247410 43.54931
## 2017.2055      23.214650  8.3305747 38.09873  0.45142100 45.97788
## 2017.2082      24.093053  9.1100295 39.07608  1.17849620 47.00761
## 2017.2110      22.211131  7.1298099 37.29245 -0.85375935 45.27602
## 2017.2137      21.447069  6.2680863 36.62605 -1.76718191 44.66132
## 2017.2164      21.745321  6.4693007 37.02134 -1.61733591 45.10798
## 2017.2192      22.591130  7.2186853 37.96358 -0.91899555 46.10126
## 2017.2219      21.539606  6.0713376 37.00788 -2.11706920 45.19628
## 2017.2247      21.813789  6.2502866 37.37729 -1.98853390 45.61611
## 2017.2274      23.182747  7.5245901 38.84090 -0.76433753 47.12983
## 2017.2301      24.283933  8.5316904 40.03618  0.19295681 48.37491
## 2017.2329      21.386869  5.5410994 37.23264 -2.84714454 45.62088
## 2017.2356      22.776158  6.8374098 38.71491 -1.60005390 47.15237
## 2017.2384      23.111717  7.0805296 39.14290 -1.40586840 47.62930
## 2017.2411      22.987565  6.8644681 39.11066 -1.67058360 47.64571
## 2017.2438      23.786378  7.5718938 40.00086 -1.01153582 48.58429
## 2017.2466      23.834700  7.5293393 40.14006 -1.10219716 48.77160
## 2017.2493      25.181530  8.7857976 41.57726  0.10642090 50.25664
## 2017.2521      24.050411  7.5648015 40.53602 -1.16215311 49.26298
## 2017.2548      23.565053  6.9900536 40.14005 -1.78422098 48.91433
## 2017.2575      25.018475  8.3545656 41.68238 -0.46677505 50.50372
## 2017.2603      27.177016 10.4246683 43.92936  1.55651130 52.79752
## 2017.2630      26.154856  9.3145352 42.99518  0.39980774 51.90990
## 2017.2658      25.157177  8.2293398 42.08501 -0.73171614 51.04607
## 2017.2685      24.626770  7.6118656 41.64167 -1.39528045 50.64882
## 2017.2712      25.427219  8.3256921 42.52875 -0.72730950 51.58175
## 2017.2740      26.672678  9.4849647 43.86039  0.38633872 52.95902
## 2017.2767      25.658862  8.3853917 42.93233 -0.75863106 52.07635
## 2017.2795      25.839524  8.4807213 43.19833 -0.70847392 52.38752
## 2017.2822      26.733521  9.2898021 44.17724  0.05565536 53.41139
## 2017.2849      27.764615 10.2363925 45.29284  0.95751195 54.57172
## 2017.2877      26.326777  8.7144563 43.93910 -0.60894331 53.26250
## 2017.2904      26.633076  8.9370563 44.32910 -0.43065085 53.69680
## 2017.2932      24.657690  6.8783646 42.43701 -2.53344156 51.84882
## 2017.2959      24.738707  6.8764659 42.60095 -2.57923359 52.05665
## 2017.2986      25.146979  7.2022042 43.09175 -2.29718574 52.59114
## 2017.3014      25.745508  7.7185774 43.77244 -1.82430303 53.31532
## 2017.3041      26.688301  8.5795882 44.79701 -1.00658544 54.38319
## 2017.3068      27.071620  8.8814920 45.26175 -0.74778014 54.89102
## 2017.3096      27.307823  9.0366428 45.57900 -0.63553581 55.25118
## 2017.3123      28.099498  9.7476229 46.45137  0.03272736 56.16627
## 2017.3151      28.985223 10.5530074 47.41744  0.79558180 57.17486

4.1.3 Check Accuracy

forecast::accuracy(climate_forecast, ts_test)
##                        ME     RMSE      MAE       MPE      MAPE      MASE
## Training set -0.002952318 1.878509 1.317623 -0.254612  5.509781 0.5153414
## Test set      3.947982827 4.875027 4.125055 17.527790 18.548411 1.6133681
##                   ACF1 Theil's U
## Training set 0.1324813        NA
## Test set     0.7569823  2.528038

4.2 Arima Model

Before creating arima model, we have to make sure our data is stationary by using adf.test() from library tseries.

4.2.1 Check stationary data

adf.test(ts_train)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ts_train
## Dickey-Fuller = -1.8526, Lag order = 11, p-value = 0.6407
## alternative hypothesis: stationary

Our p-value > 0.05 that means our data is not stationer. If our data is not stationer we have to differencing.

ts_train_diff <- ts_train %>% diff()
adf.test(ts_train_diff)
## Warning in adf.test(ts_train_diff): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ts_train_diff
## Dickey-Fuller = -14.011, Lag order = 11, p-value = 0.01
## alternative hypothesis: stationary

After differencing our p-value < 0.05, it means our data already stationer.

4.2.2 Create Arima model

climate_auto <- auto.arima(ts_train, seasonal = T)
climate_auto
## Series: ts_train 
## ARIMA(3,1,1)(0,1,0)[365] 
## 
## Coefficients:
##          ar1     ar2      ar3      ma1
##       0.7188  0.0102  -0.0375  -0.9883
## s.e.  0.0311  0.0373   0.0311   0.0081
## 
## sigma^2 estimated as 4.591:  log likelihood=-2392.92
## AIC=4795.84   AICc=4795.9   BIC=4820.84

4.2.3 Forecast Arima

climate_auto_f <- forecast(climate_auto, h=114)
climate_auto_f
##           Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## 2017.0055       12.12254  9.376510 14.86857  7.922850 16.32223
## 2017.0082       14.66313 11.262423 18.06384  9.462196 19.86407
## 2017.0110       15.76459 12.046968 19.48221 10.078977 21.45020
## 2017.0137       17.98450 14.127026 21.84196 12.085005 23.88399
## 2017.0164       18.18871 14.266076 22.11135 12.189558 24.18787
## 2017.0192       16.85897 12.904832 20.81310 10.811640 22.90629
## 2017.0219       17.40756 13.437174 21.37794 11.335382 23.47973
## 2017.0247       17.29901 13.319572 21.27845 11.212983 23.38504
## 2017.0274       17.50375 13.518768 21.48872 11.409248 23.59824
## 2017.0301       19.80514 15.816442 23.79385 13.704951 25.90534
## 2017.0329       20.10494 16.113490 24.09638 14.000546 26.20933
## 2017.0356       17.42212 13.428477 21.41576 11.314371 23.52986
## 2017.0384       14.87337 10.877862 18.86889  8.762766 20.98398
## 2017.0411       15.48224 11.485049 19.47943  9.369065 21.59541
## 2017.0438       15.88795 11.889204 19.88670  9.772396 22.00350
## 2017.0466       15.15830 11.158070 19.15853  9.040477 21.27612
## 2017.0493       14.25115 10.249483 18.25281  8.131131 20.37116
## 2017.0521       13.96220  9.959131 17.96526  7.840036 20.08436
## 2017.0548       14.08402 10.079565 18.08847  7.959736 20.20830
## 2017.0575       13.63048  9.624661 17.63631  7.504106 19.75686
## 2017.0603       16.33506 12.327873 20.34225 10.206596 22.46352
## 2017.0630       13.08532  9.076778 17.09387  6.954783 19.21586
## 2017.0658       13.56466  9.554760 17.57456  7.432048 19.69727
## 2017.0685       16.46060 12.449351 20.47185 10.325923 22.59528
## 2017.0712       19.48151 15.468905 23.49411 13.344762 25.61825
## 2017.0740       18.75536 14.741411 22.76931 12.616554 24.89417
## 2017.0767       21.46075 17.445450 25.47605 15.319879 27.60162
## 2017.0795       22.04112 18.024479 26.05777 15.898195 28.18405
## 2017.0822       19.27328 15.255287 23.29127 13.128291 25.41827
## 2017.0849       17.74444 13.725103 21.76378 11.597395 23.89149
## 2017.0877       17.16496 13.144276 21.18564 11.015855 23.31406
## 2017.0904       15.02329 11.001268 19.04532  8.872136 21.17445
## 2017.0932       18.26193 14.238562 22.28530 12.108718 24.41515
## 2017.0959       14.56496 10.540250 18.58968  8.409695 20.72023
## 2017.0986       17.23163 13.205575 21.25769 11.074308 23.38896
## 2017.1014       17.52330 13.495899 21.55070 11.363922 23.68268
## 2017.1041       18.98921 14.960466 23.01795 12.827779 25.15064
## 2017.1068       19.66753 15.637446 23.69762 13.504049 25.83101
## 2017.1096       20.03163 16.000208 24.06306 13.866100 26.19717
## 2017.1123       21.58580 17.553034 25.61857 15.418216 27.75339
## 2017.1151       21.09830 17.064193 25.13241 14.928666 27.26794
## 2017.1178       18.96497 14.929520 23.00041 12.793283 25.13665
## 2017.1205       19.54116 15.504371 23.57794 13.367425 25.71489
## 2017.1233       19.49830 15.460174 23.53643 13.322520 25.67408
## 2017.1260       20.11259 16.073121 24.15205 13.934758 26.29041
## 2017.1288       20.61259 16.571783 24.65339 14.432711 26.79246
## 2017.1315       21.36497 17.322826 25.40711 15.183046 27.54689
## 2017.1342       25.05215 21.008668 29.09563 18.868180 31.23611
## 2017.1370       25.52330 21.478485 29.56812 19.337290 31.70931
## 2017.1397       22.89830 18.852149 26.94445 16.710246 29.08636
## 2017.1425       22.89830 18.850813 26.94579 16.708202 29.08840
## 2017.1452       23.32687 19.278049 27.37570 17.134731 29.51901
## 2017.1479       23.58580 19.535642 27.63596 17.391618 29.77998
## 2017.1507       24.46080 20.409307 28.51229 18.264576 30.65702
## 2017.1534       24.69830 20.645473 28.75113 18.500036 30.89657
## 2017.1562       24.69830 20.644139 28.75246 18.497996 30.89861
## 2017.1589       24.89830 20.842806 28.95380 18.695957 31.10064
## 2017.1616       25.77330 21.716473 29.83013 19.568918 31.97768
## 2017.1644       26.81497 22.756807 30.87313 20.608547 33.02139
## 2017.1671       26.83163 22.772142 30.89113 20.623176 33.04009
## 2017.1699       27.89830 23.837477 31.95912 21.687806 34.10880
## 2017.1726       29.21080 25.148645 33.27296 22.998270 35.42333
## 2017.1753       25.83163 21.768148 29.89512 19.617068 32.04620
## 2017.1781       24.71080 20.645984 28.77562 18.494200 30.92740
## 2017.1808       25.61259 21.546440 29.67873 19.393952 31.83122
## 2017.1836       25.32687 21.259396 29.39435 19.106205 31.54754
## 2017.1863       25.89830 21.829496 29.96711 19.675601 32.12100
## 2017.1890       27.46080 23.390667 31.53093 21.236069 33.68553
## 2017.1918       26.96497 22.893506 31.03643 20.738204 33.19173
## 2017.1945       26.46080 22.388011 30.53359 20.232007 32.68959
## 2017.1973       26.14830 22.074184 30.22242 19.917477 32.37912
## 2017.2000       24.27330 20.197857 28.34874 18.040447 30.50615
## 2017.2027       25.96497 21.888197 30.04174 19.730085 32.19985
## 2017.2055       25.83580 21.757705 29.91390 19.598891 32.07271
## 2017.2082       28.21080 24.131379 32.29022 21.971864 34.44974
## 2017.2110       28.08580 24.005054 32.16655 21.844837 34.32676
## 2017.2137       28.68401 24.601943 32.76609 22.441025 34.92700
## 2017.2164       29.03163 24.948238 33.11503 22.786619 35.27665
## 2017.2192       28.52330 24.438581 32.60802 22.276261 34.77034
## 2017.2219       26.96080 22.874757 31.04684 20.711737 33.20986
## 2017.2247       28.09830 24.010934 32.18567 21.847213 34.34939
## 2017.2274       30.03163 25.942945 34.12032 23.778524 36.28474
## 2017.2301       31.77330 27.683290 35.86331 25.518169 38.02843
## 2017.2329       26.56497 22.473635 30.65630 20.307814 32.82212
## 2017.2356       28.14830 24.055647 32.24095 21.889127 34.40747
## 2017.2384       27.83163 23.737659 31.92561 21.570440 34.09283
## 2017.2411       29.02330 24.928005 33.11860 22.760087 35.28651
## 2017.2438       31.46973 27.373114 35.56634 25.204497 37.73496
## 2017.2466       31.89830 27.800366 35.99624 25.631050 38.16555
## 2017.2493       32.46973 28.370475 36.56898 26.200461 38.73900
## 2017.2521       34.21080 30.110228 38.31137 27.939516 40.48209
## 2017.2548       35.21080 31.108909 39.31269 28.937500 41.48410
## 2017.2575       34.71080 30.607592 38.81401 28.435484 40.98612
## 2017.2603       34.21080 30.106274 38.31533 27.933469 40.48813
## 2017.2630       33.27330 29.167457 37.37914 26.993955 39.55265
## 2017.2658       31.83163 27.724474 35.93879 25.550275 38.11299
## 2017.2685       31.16497 27.056491 35.27344 24.881595 37.44834
## 2017.2712       32.63163 28.521842 36.74143 26.346249 38.91702
## 2017.2740       34.14830 30.037193 38.25941 27.860904 40.43570
## 2017.2767       31.69830 27.585878 35.81072 25.408893 37.98771
## 2017.2795       32.09830 27.984564 36.21204 25.806883 38.38972
## 2017.2822       33.64830 29.533249 37.76335 27.354873 39.94173
## 2017.2849       35.02330 30.906936 39.13967 28.727864 41.31874
## 2017.2877       35.52330 31.405623 39.64098 29.225855 41.82075
## 2017.2904       37.58580 33.466810 41.70479 31.286348 43.88525
## 2017.2932       36.56497 32.444664 40.68527 30.263507 42.86643
## 2017.2959       36.52330 32.401685 40.64492 30.219834 42.82677
## 2017.2986       35.89830 31.775374 40.02123 29.592828 42.20377
## 2017.3014       35.96080 31.836562 40.08504 29.653323 42.26828
## 2017.3041       35.89830 31.772752 40.02385 29.588818 42.20778
## 2017.3068       35.14830 31.021441 39.27516 28.836814 41.45979
## 2017.3096       33.81497 29.686798 37.94314 27.501477 40.12846
## 2017.3123       33.21080 29.081322 37.34028 26.895308 39.52629
## 2017.3151       33.64830 29.517513 37.77909 27.330806 39.96580

4.2.4 Check accuracy

accuracy(climate_auto_f, ts_test)
##                       ME     RMSE      MAE         MPE      MAPE     MASE
## Training set  0.01624947 1.851854 1.212622  -0.2772843  5.181115 0.474274
## Test set     -2.62195810 4.205774 3.563342 -13.6703883 18.427293 1.393674
##                     ACF1 Theil's U
## Training set 0.002660007        NA
## Test set     0.750725339  2.659278

4.3 STLM Model

4.3.1 Create STLM Model

climate_stlm <- stlm(y= ts_train,
                     s.window = 356,
                     method = "arima")

summary(climate_stlm$model)
## Series: x 
## ARIMA(1,1,1) 
## 
## Coefficients:
##          ar1      ma1
##       0.6827  -0.9818
## s.e.  0.0222   0.0070
## 
## sigma^2 estimated as 1.776:  log likelihood=-2492.66
## AIC=4991.32   AICc=4991.34   BIC=5007.18
## 
## Training set error measures:
##                      ME     RMSE      MAE         MPE     MAPE      MASE
## Training set 0.04844167 1.331441 1.013621 -0.07929947 4.019139 0.3964418
##                      ACF1
## Training set -0.002063678

4.3.2 Forecast

climate_stlm_forecast <- forecast(climate_stlm, h = 114)

4.3.3 Check Accuracy

accuracy(climate_stlm_forecast, ts_test)
##                       ME     RMSE      MAE        MPE      MAPE      MASE
## Training set  0.04844167 1.331441 1.013621 -0.1071771  4.393256 0.3964417
## Test set     -0.72793616 2.806880 2.294755 -5.1266987 12.153969 0.8975117
##                      ACF1 Theil's U
## Training set -0.002063678        NA
## Test set      0.723578346  1.779755

4.3.4 Visualization

ts_train %>% 
  autoplot(series = "Actual") +
  autolayer(climate_stlm_forecast$fitted, series = "Train") +
  autolayer(climate_stlm_forecast$mean, series = "Test")

From the analysis above, we can conclude that we have sucsesfully forecast mean temperature and found STLM as the best model with the lowest error (MAPE ~12.15).

5 Assumption

5.1 No auto-correlation residual

forecast::Acf(climate_stlm$residuals)

Based on acf above we can see there are some autocorellation. We have to check more detail our autocorelaltion is significant or not because our mind can trick us. We do Ljung-box test to make sure.

Box.test(climate_stlm$residuals, type = "Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  climate_stlm$residuals
## X-squared = 0.0062391, df = 1, p-value = 0.937

p-value >0.05 that means there is no autocorrelation.

5.2 Normality residual

hist(climate_stlm$residuals, breaks = 20)

shapiro.test(climate_stlm$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  climate_stlm$residuals
## W = 0.98986, p-value = 0.00000001546

p-value < 0.05, our forecast residuals are not distributed normally.

Based on the assumption check, there is no autocorrelation on our forecast residuals (p-value > 0.05), but our forecast’s residuals are not distributed normally, therefore it’s residuals may not be appeared around its mean as seen in the histogram. But, if we inspect the distribution of residuals through a line plot, it is actually resembles the error plot from our time series object decomposition.

In a time series, such errors might emerge from various unpredictable events and is actually quite unavoidable. One strategy to overcome it is to analyze what kinds of unpredictable events that might occur and occurs frequently.