library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(fpp)
## Loading required package: fma
## Loading required package: expsmooth
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: tseries
library(readr)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(neuralnet)
goldprice <- read.csv("Downloads/1979-2021.csv")
summary(goldprice)
##      Date                USD          Europe.EUR.       Japan.JPY.    
##  Length:511         Min.   : 233.7   Min.   : 144.8   Min.   : 27938  
##  Class :character   1st Qu.: 354.2   1st Qu.: 306.9   1st Qu.: 43096  
##  Mode  :character   Median : 415.1   Median : 364.1   Median : 70456  
##                     Mean   : 693.7   Mean   : 572.9   Mean   : 82948  
##                     3rd Qu.:1166.7   3rd Qu.: 929.2   3rd Qu.:124847  
##                     Max.   :1964.9   Max.   :1661.7   Max.   :207845  
##                                                                       
##  United.Kingdom.GBP.  Canada.CAD.     Switzerland.CHF.   India.INR.    
##  Min.   : 116.2      Min.   : 267.1   Min.   : 379.3   Min.   :  1841  
##  1st Qu.: 218.4      1st Qu.: 454.3   1st Qu.: 501.6   1st Qu.:  6274  
##  Median : 256.4      Median : 532.3   Median : 698.2   Median : 13027  
##  Mean   : 450.1      Mean   : 843.5   Mean   : 826.1   Mean   : 33124  
##  3rd Qu.: 742.1      3rd Qu.:1327.0   3rd Qu.:1127.0   3rd Qu.: 61885  
##  Max.   :1497.1      Max.   :2632.0   Max.   :1787.5   Max.   :146999  
##                                                                        
##    China.CNY.       Turkey.TRY.        Saudi.Arabia.SAR. Indonesia.IDR.    
##  Min.   :  821.1   Min.   :    0.100   Min.   : 745.2    Min.   :  139739  
##  1st Qu.: 2171.3   1st Qu.:    2.475   1st Qu.:1311.3    1st Qu.:  697974  
##  Median : 3287.8   Median :  446.050   Median :1502.6    Median : 2536803  
##  Mean   : 4883.3   Mean   : 1739.876   Mean   :2580.5    Mean   : 6207427  
##  3rd Qu.: 7991.1   3rd Qu.: 2595.025   3rd Qu.:4375.9    3rd Qu.:12021637  
##  Max.   :13714.0   Max.   :16105.400   Max.   :7369.2    Max.   :28687539  
##  NA's   :72        NA's   :47                                              
##  United.Arab.Emirates.AED. Thailand.THB.    Vietnam.VND.        Egypt.EGP.     
##  Min.   : 856.1            Min.   : 4511   Min.   : 1571728   Min.   :  870.1  
##  1st Qu.:1297.5            1st Qu.: 9612   1st Qu.: 4090357   1st Qu.: 1176.1  
##  Median :1528.9            Median :12140   Median : 7502084   Median : 2578.9  
##  Mean   :2549.2            Mean   :21660   Mean   :15126333   Mean   : 6759.6  
##  3rd Qu.:4285.0            3rd Qu.:38774   3rd Qu.:27547811   3rd Qu.: 9149.5  
##  Max.   :7217.4            Max.   :61270   Max.   :45539503   Max.   :31379.5  
##                                            NA's   :131        NA's   :119      
##  South.Korean.KRW. Australia.AUD.   South.Africa.ZAR.
##  Min.   : 108236   Min.   : 197.2   Min.   :  194.5  
##  1st Qu.: 301252   1st Qu.: 474.6   1st Qu.:  972.5  
##  Median : 374980   Median : 554.1   Median : 1864.7  
##  Mean   : 717692   Mean   : 869.9   Mean   : 5950.8  
##  3rd Qu.:1304939   3rd Qu.:1372.6   3rd Qu.: 9549.4  
##  Max.   :2340982   Max.   :2739.9   Max.   :33459.8  
## 
gold.ts <- ts(goldprice$USD, start = 1979, end = 2021, frequency = 12)
plot(gold.ts, ylab="Monthly Gold Price", main="Monthly Gold Price, USA, 1979-1921")

gold.train=ts(goldprice$USD, start = 1979, end = 2000, frequency = 12)
plot(gold.train, ylab="Monthly Gold Price", main="Monthly Gold Price, USA, 1979-2000")

gold.test=ts(goldprice$USD, start = 2000, end = 2021, frequency = 12)
plot(gold.test, ylab="Monthly Gold Price", main="Monthly Gold Price, USA, 2000-2021")

plot(decompose(gold.train))

acf(gold.train)

pacf(gold.train)

Model #1

fit1<-auto.arima(gold.train, seasonal = TRUE)
fit1
## Series: gold.train 
## ARIMA(0,1,1)(2,0,1)[12] 
## 
## Coefficients:
##          ma1     sar1     sar2    sma1
##       0.0446  -0.6353  -0.1641  0.4719
## s.e.  0.0719   0.4704   0.1011  0.4676
## 
## sigma^2 estimated as 599.2:  log likelihood=-1161.69
## AIC=2333.39   AICc=2333.63   BIC=2351.04
f.arima=forecast(fit1, h=58)
plot(f.arima, xlab="Time" ,ylab="Monthly Gold Price")

accuracy(f.arima, gold.test)
##                       ME      RMSE       MAE        MPE     MAPE      MASE
## Training set   0.1997619  24.23525  14.92405 -0.1036188  3.74794 0.2611221
## Test set     153.7410766 187.88654 158.50275 30.8790983 32.78669 2.7732803
##                      ACF1 Theil's U
## Training set -0.002558343        NA
## Test set      0.886504288  3.769971
hist(residuals(f.arima))

plot(residuals(f.arima))

Model #2

fit2=nnetar(gold.train)
summary(fit2)
##           Length Class        Mode     
## x         253    ts           numeric  
## m           1    -none-       numeric  
## p           1    -none-       numeric  
## P           1    -none-       numeric  
## scalex      2    -none-       list     
## size        1    -none-       numeric  
## subset    253    -none-       numeric  
## model      20    nnetarmodels list     
## nnetargs    0    -none-       list     
## fitted    253    ts           numeric  
## residuals 253    ts           numeric  
## lags       13    -none-       numeric  
## series      1    -none-       character
## method      1    -none-       character
## call        2    -none-       call
f.nnetar=forecast(fit2, h=58)
plot(f.nnetar,xlab="Time" ,ylab="Monthly Gold Price")

accuracy(f.nnetar,gold.test)
##                       ME       RMSE        MAE         MPE      MAPE      MASE
## Training set  -0.0340266   8.386147   6.127924 -0.08960853  1.692649 0.1072187
## Test set     160.6827786 192.352752 164.663669 32.66580506 34.275485 2.8810762
##                     ACF1 Theil's U
## Training set -0.01188528        NA
## Test set      0.87761528  3.904474
hist(residuals(f.nnetar))

plot(residuals(f.nnetar))

Model #3

lambda <- BoxCox.lambda(gold.train)
lambda
## [1] 1.114233
fit3<-stlf(gold.train, method="ets", lambda = lambda,  h=58)
summary(fit3)
## 
## Forecast method: STL +  ETS(M,Ad,N)
## 
## Model Information:
## ETS(M,Ad,N) 
## 
## Call:
##  ets(y = na.interp(x), model = etsmodel, allow.multiplicative.trend = allow.multiplicative.trend) 
## 
##   Smoothing parameters:
##     alpha = 0.9999 
##     beta  = 0.0083 
##     phi   = 0.9583 
## 
##   Initial states:
##     l = 309.5948 
##     b = 50.7586 
## 
##   sigma:  0.0563
## 
##      AIC     AICc      BIC 
## 3237.109 3237.451 3258.309 
## 
## Error measures:
##                     ME     RMSE      MAE        MPE    MAPE    MASE       ACF1
## Training set -1.650934 22.30299 14.02948 -0.5156365 3.51985 0.24547 0.02375412
## 
## Forecasts:
##          Point Forecast    Lo 80    Hi 80      Lo 95    Hi 95
## Feb 2000       284.8397 266.3693 303.1742 256.532766 312.8283
## Mar 2000       285.8344 259.5659 311.8295 245.538115 325.4896
## Apr 2000       286.8515 254.5117 318.7790 237.203622 335.5312
## May 2000       285.1887 247.6185 322.2001 227.468826 341.5941
## Jun 2000       283.0770 240.8156 324.6267 218.104406 346.3729
## Jul 2000       282.5521 236.0140 328.2262 210.958655 352.1069
## Aug 2000       277.5108 226.8832 327.0983 199.566457 352.9956
## Sep 2000       289.2072 235.1140 342.1613 205.910277 369.8088
## Oct 2000       285.9549 228.2327 342.3659 197.010024 371.7913
## Nov 2000       283.3053 222.1133 343.0108 188.951016 374.1281
## Dec 2000       283.0429 218.5740 345.8607 183.578956 378.5772
## Jan 2001       281.6730 214.0042 347.5144 177.206941 381.7807
## Feb 2001       283.2810 212.6060 351.9744 174.121972 387.7060
## Mar 2001       284.3413 210.7331 355.8084 170.595285 392.9631
## Apr 2001       285.4213 208.9612 359.5800 167.210542 398.1148
## May 2001       283.8173 204.4764 360.6664 161.072686 400.5736
## Jun 2001       281.7617 199.5790 361.2515 154.531508 402.5030
## Jul 2001       281.2914 196.3832 363.3202 149.761770 405.8665
## Aug 2001       276.3003 188.5178 360.9491 140.183957 404.8182
## Sep 2001       288.0527 198.1839 374.7712 148.751773 419.7262
## Oct 2001       284.8471 192.2810 374.0329 141.246155 420.2361
## Nov 2001       282.2426 187.0368 373.8390 134.425104 421.2615
## Dec 2001       282.0244 184.3265 375.9178 130.242431 424.5075
## Jan 2002       280.6965 180.4841 376.8860 124.890357 426.6382
## Feb 2002       282.3459 179.8062 380.6975 122.849396 431.5528
## Mar 2002       283.4456 178.5847 383.9428 120.255385 435.8907
## Apr 2002       284.5633 177.4147 387.1727 117.728201 440.1958
## May 2002       282.9946 173.4356 387.7797 112.262570 441.9000
## Jun 2002       280.9727 168.9965 387.9237 106.308960 443.1336
## Jul 2002       280.5352 166.2564 389.5696 102.143961 445.8323
## Aug 2002       275.5741 158.7241 386.8375  92.892338 444.2075
## Sep 2002       287.3601 169.0319 400.1979 102.575432 458.4117
## Oct 2002       284.1825 163.4501 399.1313  95.416585 458.3995
## Nov 2002       281.6052 158.5152 398.6240  88.923582 458.9270
## Dec 2002       281.4135 156.1409 400.3902  85.154411 461.6807
## Jan 2003       280.1107 152.5934 401.0697  80.120529 463.3544
## Feb 2003       281.7850 152.2538 404.5871  78.536750 467.8090
## Mar 2003       282.9083 151.3434 407.5560  76.343877 471.7135
## Apr 2003       284.0487 150.4704 410.5222  74.195858 475.6053
## May 2003       282.5011 146.7206 410.8943  68.920326 476.9369
## Jun 2003       280.4994 142.4880 410.8168  63.090821 477.8180
## Jul 2003       280.0816 139.9750 412.2410  59.124716 480.1666
## Aug 2003       275.1385 132.5603 409.3241  49.682048 478.2420
## Sep 2003       286.9447 143.3324 422.4066  60.451108 492.0290
## Oct 2003       283.7840 137.8912 421.1606  53.233846 491.7288
## Nov 2003       281.2228 133.0979 420.4775  46.670106 491.9752
## Dec 2003       281.0471 130.9069 422.0609  42.993900 494.4408
## Jan 2004       279.7594 127.5097 422.5706  37.911472 495.8450
## Feb 2004       281.4485 127.3784 425.9075  36.554051 500.0183
## Mar 2004       282.5861 126.6570 428.7051  34.515020 503.6553
## Apr 2004       283.7400 125.9665 431.5057  32.506595 507.2885
## May 2004       282.2052 122.3327 431.7322  27.014462 508.3884
## Jun 2004       280.2156 118.1979 431.5163  20.803256 509.0486
## Jul 2004       279.8095 115.8124 432.7982  16.596701 511.1731
## Aug 2004       274.8773 108.4074 429.7675   5.567460 509.0615
## Sep 2004       286.6956 119.5876 442.6530  18.762279 522.5581
## Oct 2004       283.5449 114.1951 441.2920  10.640766 522.0722
## Nov 2004       280.9935 109.4558 440.4945   2.652755 522.1349
plot(fit3,xlab="Time" ,ylab="Monthly Gold Price")

accuracy(fit3,gold.test)
##                      ME      RMSE       MAE        MPE     MAPE     MASE
## Training set  -1.650934  22.30299  14.02948 -0.5156365  3.51985 0.245470
## Test set     155.814565 189.59717 160.53840 31.3776255 33.28105 2.808898
##                    ACF1 Theil's U
## Training set 0.02375412        NA
## Test set     0.88538664  3.811567
hist(residuals(fit3))

plot(residuals(fit3))