LOAD DATA

library(readxl)
data = read_excel("C:/Users/naura/Downloads/Tekanan udara bandung.xlsx")
data
## # A tibble: 60 × 1
##    `Tekanan Udara (mb)`
##                   <dbl>
##  1                 924 
##  2                 924.
##  3                 924.
##  4                 923.
##  5                 924.
##  6                 924.
##  7                 924.
##  8                 924.
##  9                 925.
## 10                 925.
## # ℹ 50 more rows

MENGUBAH DATASET TERSEBUT MENJADI DATA TIME SERIES

datats <- ts(data)

#Menampilkan data teratas
head(datats)
##      Tekanan Udara (mb)
## [1,]              924.0
## [2,]              923.8
## [3,]              924.1
## [4,]              923.2
## [5,]              924.1
## [6,]              923.9
#Membaca data
View(datats)
str(datats)
##  Time-Series [1:60, 1] from 1 to 60: 924 924 924 923 924 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr "Tekanan Udara (mb)"
mean(datats)
## [1] 923.445

PLOT DATA TIME SERIES DATATS

plot.ts(datats)

PLOT DAN HASIL KORELASI ACF

acf(datats)
print(acf(datats))

## 
## Autocorrelations of series 'datats', by lag
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  1.000  0.593  0.465  0.371  0.285  0.169  0.064  0.003  0.044  0.010 -0.030 
##     11     12     13     14     15     16     17 
##  0.103  0.168  0.107  0.018  0.059 -0.021 -0.038

PLOT DAN HASIL KORELASI PACF

pacf(datats)
print(pacf(datats))

## 
## Partial autocorrelations of series 'datats', by lag
## 
##      1      2      3      4      5      6      7      8      9     10     11 
##  0.593  0.174  0.064  0.005 -0.083 -0.093 -0.040  0.117 -0.008 -0.048  0.213 
##     12     13     14     15     16     17 
##  0.103 -0.115 -0.166  0.066 -0.129  0.009

DIFFERENCING 1

datadiff1 <- diff(datats, differences = 1)

#Menampilkan plot time series datadiff1
plot.ts(datadiff1)

#Menampilkan plot dan hasil korelasi acf
acf(datadiff1, lag.max = 20)

acf(datadiff1, lag.max = 20, plot = FALSE)
## 
## Autocorrelations of series 'datadiff1', by lag
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  1.000 -0.342 -0.049  0.004  0.024 -0.010 -0.065 -0.126  0.088  0.001 -0.186 
##     11     12     13     14     15     16     17     18     19     20 
##  0.074  0.149  0.042 -0.163  0.158 -0.071  0.061 -0.044  0.016 -0.007
#Menampilkan plot dan hasil korelasi pacf
pacf(datadiff1, lag.max = 20)

pacf(datadiff1, lag.max = 20, plot = FALSE)
## 
## Partial autocorrelations of series 'datadiff1', by lag
## 
##      1      2      3      4      5      6      7      8      9     10     11 
## -0.342 -0.188 -0.094 -0.020 -0.013 -0.082 -0.219 -0.084 -0.048 -0.256 -0.153 
##     12     13     14     15     16     17     18     19     20 
##  0.053  0.109 -0.133  0.068 -0.062 -0.035  0.004  0.080 -0.007

PEMODELAN DENGAN AUTO.ARIMA

#Pemodelan dengan otomatis menghasilkan model terbaik
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
dt = auto.arima(datats, trace = TRUE)
## 
##  ARIMA(2,1,2) with drift         : Inf
##  ARIMA(0,1,0) with drift         : 135.1564
##  ARIMA(1,1,0) with drift         : 130.1289
##  ARIMA(0,1,1) with drift         : 127.4932
##  ARIMA(0,1,0)                    : 133.0202
##  ARIMA(1,1,1) with drift         : 128.2819
##  ARIMA(0,1,2) with drift         : 129.333
##  ARIMA(1,1,2) with drift         : Inf
##  ARIMA(0,1,1)                    : 125.2808
##  ARIMA(1,1,1)                    : 126.0686
##  ARIMA(0,1,2)                    : 127.0392
##  ARIMA(1,1,0)                    : 127.9171
##  ARIMA(1,1,2)                    : Inf
## 
##  Best model: ARIMA(0,1,1)
dt
## Series: datats 
## ARIMA(0,1,1) 
## 
## Coefficients:
##           ma1
##       -0.4604
## s.e.   0.1337
## 
## sigma^2 = 0.4764:  log likelihood = -60.53
## AIC=125.07   AICc=125.28   BIC=129.22
#MENGHITUNG NILAI AIC
dt$aic
## [1] 125.0665
#TEST DIAGNOSIS MODEL TERBAIK
#Pemodelan terbaik dengan auto.arima
tsdiag(dt)

#PERAMALAN DATA TIME SERIES
df = forecast(dt)
plot(df)

plot.ts(df$residuals)

Box.test(df$residuals)
## 
##  Box-Pierce test
## 
## data:  df$residuals
## X-squared = 0.022546, df = 1, p-value = 0.8806
hist(df$residuals)

#MEMPREDIKSI DATASET SELAMA 12 BULAN KEDEPAN
predict(dt, n.ahead = 12)
## $pred
## Time Series:
## Start = 61 
## End = 72 
## Frequency = 1 
##  [1] 923.6543 923.6543 923.6543 923.6543 923.6543 923.6543 923.6543 923.6543
##  [9] 923.6543 923.6543 923.6543 923.6543
## 
## $se
## Time Series:
## Start = 61 
## End = 72 
## Frequency = 1 
##  [1] 0.6902302 0.7843057 0.8682471 0.9447596 1.0155237 1.0816681 1.1439946
##  [8] 1.2030965 1.2594280 1.3133455 1.3651352 1.4150307
Dataset_forecast = forecast(dt, h=12)
Dataset_forecast
##    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 61       923.6543 922.7697 924.5389 922.3015 925.0071
## 62       923.6543 922.6492 924.6594 922.1171 925.1915
## 63       923.6543 922.5416 924.7670 921.9526 925.3560
## 64       923.6543 922.4435 924.8651 921.8026 925.5060
## 65       923.6543 922.3529 924.9557 921.6639 925.6447
## 66       923.6543 922.2681 925.0405 921.5343 925.7743
## 67       923.6543 922.1882 925.1204 921.4121 925.8965
## 68       923.6543 922.1125 925.1961 921.2963 926.0123
## 69       923.6543 922.0403 925.2683 921.1859 926.1227
## 70       923.6543 921.9712 925.3374 921.0802 926.2284
## 71       923.6543 921.9048 925.4038 920.9787 926.3299
## 72       923.6543 921.8409 925.4677 920.8809 926.4277

PEMODELAN DATA TIME SERIES

fit1 = arima(datats, order = c(1, 1, 0))
fit2 = arima(datats, order = c(0, 1, 1))
fit3 = arima(datats, order = c(1, 1, 1))
fit4 = arima(datats, order = c(2, 1, 0))
fit5 = arima(datats, order = c(2, 1, 1))
fit6 = arima(datats, order = c(2, 1, 2))
fit7 = arima(datats, order = c(1, 1, 2))
fit8 = arima(datats, order = c(1, 1, 3))
fit9 = arima(datats, order = c(2, 1, 3))
fit10 = arima(datats, order = c(1, 1, 4))
fit11 = arima(datats, order = c(2, 1, 4))
fit12 = arima(datats, order = c(3, 1, 4))
fit13 = arima(datats, order = c(4, 1, 3))
fit14 = arima(datats, order = c(0, 1, 2))

#Menampilkan pemodelan
fit1
## 
## Call:
## arima(x = datats, order = c(1, 1, 0))
## 
## Coefficients:
##           ar1
##       -0.3374
## s.e.   0.1212
## 
## sigma^2 estimated as 0.4756:  log likelihood = -61.85,  aic = 127.7
fit2
## 
## Call:
## arima(x = datats, order = c(0, 1, 1))
## 
## Coefficients:
##           ma1
##       -0.4604
## s.e.   0.1337
## 
## sigma^2 estimated as 0.4539:  log likelihood = -60.53,  aic = 125.07
fit3
## 
## Call:
## arima(x = datats, order = c(1, 1, 1))
## 
## Coefficients:
##          ar1      ma1
##       0.4360  -0.8530
## s.e.  0.2063   0.1365
## 
## sigma^2 estimated as 0.4404:  log likelihood = -59.82,  aic = 125.63
fit4
## 
## Call:
## arima(x = datats, order = c(2, 1, 0))
## 
## Coefficients:
##           ar1      ar2
##       -0.4003  -0.1816
## s.e.   0.1271   0.1259
## 
## sigma^2 estimated as 0.4589:  log likelihood = -60.83,  aic = 127.66
fit5
## 
## Call:
## arima(x = datats, order = c(2, 1, 1))
## 
## Coefficients:
##          ar1     ar2      ma1
##       0.5048  0.1903  -1.0000
## s.e.  0.1283  0.1280   0.1603
## 
## sigma^2 estimated as 0.4184:  log likelihood = -59.2,  aic = 126.4
fit6
## 
## Call:
## arima(x = datats, order = c(2, 1, 2))
## 
## Coefficients:
##          ar1     ar2      ma1     ma2
##       0.8010  0.0119  -1.3095  0.3095
## s.e.  0.4698  0.3231   0.4567  0.4496
## 
## sigma^2 estimated as 0.4171:  log likelihood = -59.02,  aic = 128.04
fit7
## 
## Call:
## arima(x = datats, order = c(1, 1, 2))
## 
## Coefficients:
##          ar1      ma1     ma2
##       0.8176  -1.3245  0.3245
## s.e.  0.1204   0.1969  0.1802
## 
## sigma^2 estimated as 0.4171:  log likelihood = -59.02,  aic = 126.05
fit8
## 
## Call:
## arima(x = datats, order = c(1, 1, 3))
## 
## Coefficients:
##          ar1      ma1     ma2      ma3
##       0.8148  -1.3238  0.3303  -0.0065
## s.e.  0.1392   0.1993  0.2263   0.1507
## 
## sigma^2 estimated as 0.4171:  log likelihood = -59.02,  aic = 128.04
fit9
## 
## Call:
## arima(x = datats, order = c(2, 1, 3))
## 
## Coefficients:
##           ar1     ar2      ma1      ma2     ma3
##       -0.1061  0.7680  -0.3511  -1.0000  0.3511
## s.e.   0.1366  0.1158   0.1996   0.0772  0.1802
## 
## sigma^2 estimated as 0.4011:  log likelihood = -58.41,  aic = 128.82
fit10
## 
## Call:
## arima(x = datats, order = c(1, 1, 4))
## 
## Coefficients:
##           ar1     ma1      ma2      ma3      ma4
##       -0.9147  0.4956  -0.5553  -0.1918  -0.1409
## s.e.   0.0721  0.1515   0.1488   0.1718   0.1382
## 
## sigma^2 estimated as 0.4241:  log likelihood = -59.29,  aic = 130.57
fit11
## 
## Call:
## arima(x = datats, order = c(2, 1, 4))
## 
## Coefficients:
##           ar1     ar2      ma1      ma2     ma3      ma4
##       -0.1187  0.7446  -0.3508  -0.9546  0.3508  -0.0454
## s.e.   0.1503  0.1462   0.2117   0.1771  0.1891   0.1566
## 
## sigma^2 estimated as 0.3997:  log likelihood = -58.37,  aic = 130.74
fit12
## 
## Call:
## arima(x = datats, order = c(3, 1, 4))
## 
## Coefficients:
##           ar1      ar2     ar3      ma1     ma2      ma3     ma4
##       -0.2428  -0.1654  0.7426  -0.2270  0.0348  -1.0057  0.1980
## s.e.   0.1760   0.1426  0.1511   0.2685  0.1370   0.1098  0.2274
## 
## sigma^2 estimated as 0.377:  log likelihood = -57.44,  aic = 130.88
fit13
## 
## Call:
## arima(x = datats, order = c(4, 1, 3))
## 
## Coefficients:
##          ar1     ar2     ar3    ar4      ma1      ma2      ma3
##       -0.184  0.4036  0.2105  0.098  -0.3261  -0.5992  -0.0748
## s.e.   1.632  0.6782  0.6302  0.235   1.6370   1.2656   0.8025
## 
## sigma^2 estimated as 0.4159:  log likelihood = -58.94,  aic = 133.89
fit14
## 
## Call:
## arima(x = datats, order = c(0, 1, 2))
## 
## Coefficients:
##           ma1      ma2
##       -0.4446  -0.1101
## s.e.   0.1308   0.1641
## 
## sigma^2 estimated as 0.4498:  log likelihood = -60.3,  aic = 126.6

MENGHITUNG NILAI AIC

fit1$aic
## [1] 127.7028
fit2$aic
## [1] 125.0665
fit3$aic
## [1] 125.6322
fit4$aic
## [1] 127.6639
fit5$aic
## [1] 126.4048
fit6$aic
## [1] 128.0441
fit7$aic
## [1] 126.0454
fit8$aic
## [1] 128.0436
fit9$aic
## [1] 128.8193
fit10$aic
## [1] 130.5743
fit11$aic
## [1] 130.7369
fit12$aic
## [1] 130.8809
fit13$aic
## [1] 133.8881
fit14$aic
## [1] 126.6028

TEST BIC

BIC(fit1)
## [1] 131.8579
BIC(fit2)
## [1] 129.2216
BIC(fit3)
## [1] 131.8648
BIC(fit4)
## [1] 133.8965
BIC(fit5)
## [1] 134.715
BIC(fit6)
## [1] 138.4318
BIC(fit7)
## [1] 134.3556
BIC(fit8)
## [1] 138.4313
BIC(fit9)
## [1] 141.2845
BIC(fit10)
## [1] 143.0395
BIC(fit11)
## [1] 145.2797
BIC(fit12)
## [1] 147.5012
BIC(fit13)
## [1] 150.5084
BIC(fit14)
## [1] 132.8354
accuracy(fit2)
##                       ME      RMSE       MAE          MPE       MAPE      MASE
## Training set 0.005470459 0.6786289 0.5194214 0.0005535804 0.05625969 0.8831661
##                    ACF1
## Training set 0.01938488
accuracy(fit3)
##                        ME     RMSE       MAE           MPE       MAPE      MASE
## Training set -0.006550688 0.668799 0.5297182 -0.0007533817 0.05737588 0.9006736
##                     ACF1
## Training set -0.05958424
accuracy(fit5)
##                       ME      RMSE      MAE          MPE       MAPE      MASE
## Training set -0.06753628 0.6524346 0.504743 -0.007357698 0.05467395 0.8582086
##                     ACF1
## Training set -0.04177316
accuracy(fit14)
##                       ME      RMSE       MAE          MPE       MAPE      MASE
## Training set 0.005637226 0.6756432 0.5252431 0.0005695527 0.05689064 0.8930646
##                      ACF1
## Training set -0.004426878
accuracy(fit10)
##                       ME      RMSE       MAE          MPE       MAPE     MASE
## Training set 0.001628857 0.6567257 0.5239547 0.0001341016 0.05675024 0.890874
##                      ACF1
## Training set -0.004135243

TEST DIAGNOSIS MODEL TERBAIK

#Pemodelan terbaik, yaitu pada fit3 karena nilai AIC nya paling kecil
tsdiag(fit2)

PERAMALAN DATA TIME SERIES

library(forecast)
df = forecast(fit2)
plot(df)

plot.ts(df$residuals)

Box.test(df$residuals)
## 
##  Box-Pierce test
## 
## data:  df$residuals
## X-squared = 0.022546, df = 1, p-value = 0.8806
hist(df$residuals)

checkresiduals(fit2)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(0,1,1)
## Q* = 7.94, df = 9, p-value = 0.5402
## 
## Model df: 1.   Total lags used: 10

MEMPREDIKSI DATASET SELAMA 12 BULAN KEDEPAN

predict(fit2, n.ahead = 6)
## $pred
## Time Series:
## Start = 61 
## End = 66 
## Frequency = 1 
## [1] 923.6543 923.6543 923.6543 923.6543 923.6543 923.6543
## 
## $se
## Time Series:
## Start = 61 
## End = 66 
## Frequency = 1 
## [1] 0.6737003 0.7655228 0.8474540 0.9221341 0.9912035 1.0557639
Dataset_forecast = forecast(fit2, h=6)
Dataset_forecast
##    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 61       923.6543 922.7909 924.5177 922.3339 924.9747
## 62       923.6543 922.6732 924.6354 922.1539 925.1547
## 63       923.6543 922.5682 924.7404 921.9933 925.3153
## 64       923.6543 922.4725 924.8361 921.8469 925.4616
## 65       923.6543 922.3840 924.9246 921.7116 925.5970
## 66       923.6543 922.3013 925.0073 921.5850 925.7236