library(tseries)
## Warning: package 'tseries' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(TSA)
## Warning: package 'TSA' was built under R version 4.3.3
## 
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
## 
##     acf, arima
## The following object is masked from 'package:utils':
## 
##     tar
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 methods overwritten by 'forecast':
##   method       from
##   fitted.Arima TSA 
##   plot.Arima   TSA

##Pembangkitan Data Time Series Coba bangkitkan data time series dengan model ARIMA(1,1,1). Tentukan nilai AR dan MA

# Set seed untuk reproduksi
set.seed(123)

#Panjang data
n <- 200

#Parameter ARIMA (p=1, d=1, q=1)
ar<- 0.7
ma<- -0.5

#Simulasi data
ts_arima <- arima.sim(model = list(order=c(1,1,1), ar = ar, ma=ma), n=n)

#Plot
ts.plot(ts_arima, main = "Simulasi Data Arima(1,1,1)")

## Melakukan pemodelan 1.Buat plot ACF dan PACF 2.cek stasioneritas dengan ADF Test 3.Melakukan Differencing 4.Buat plot ACF dan PACF 5.Cek kestasioneritas dengan ADF Test 6.Ubah ke data ts 7.Buat kandidat model melalui ACF,PACF dan EACF 8.Bandingkan dengan hasil auto arima 9.Cek AIC terkecil

acf(ts_arima)

pacf(ts_arima)

adf.test(ts_arima)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ts_arima
## Dickey-Fuller = -2.449, Lag order = 5, p-value = 0.388
## alternative hypothesis: stationary
diff <- diff(ts_arima)
acf(diff)

adf.test(diff)
## Warning in adf.test(diff): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff
## Dickey-Fuller = -5.4572, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
data.ts<- ts(diff)
head(data.ts)
## Time Series:
## Start = 1 
## End = 6 
## Frequency = 1 
## [1] -0.4362295 -1.1367886 -0.4798151 -1.2528876 -1.0929103 -1.0256309
acf(data.ts)

pacf(data.ts)

eacf(data.ts)
## AR/MA
##   0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x o o o o o o o o o o  o  o  o 
## 1 x o o o o o o o o o o  o  o  o 
## 2 x x o o o o o o o o o  o  o  o 
## 3 x x o o o o o o o o o  o  o  o 
## 4 x x o o o o o o o o o  o  o  o 
## 5 x o o o o o o o o o o  o  o  o 
## 6 x o o x o o o o o o o  o  o  o 
## 7 o x x x x o o o o o o  o  o  o
auto.arima(data.ts)
## Series: data.ts 
## ARIMA(2,0,2) with zero mean 
## 
## Coefficients:
##           ar1     ar2     ma1      ma2
##       -0.1116  0.6336  0.3108  -0.6250
## s.e.   0.2175  0.1701  0.2294   0.2122
## 
## sigma^2 = 0.8631:  log likelihood = -267.28
## AIC=544.57   AICc=544.88   BIC=561.06

KANDIDAT MODEL ARIMA (1,1,1) ARIMA (1,1,3) ARIMA (0,1,1) ARIMA (2,0,2)

#Penentuan model terbaik berdasarkan AIC

arima(data.ts, order=c(1,1,1), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(1, 1, 1), method = "ML")
## 
## Coefficients:
##          ar1      ma1
##       0.1488  -1.0000
## s.e.  0.0706   0.0164
## 
## sigma^2 estimated as 0.8926:  log likelihood = -273.56,  aic = 551.13
arima(data.ts, order=c(1,1,3), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(1, 1, 3), method = "ML")
## 
## Coefficients:
##           ar1     ma1      ma2      ma3
##       -0.8559  0.0335  -0.9642  -0.0693
## s.e.   0.0800  0.1018   0.0443   0.0772
## 
## sigma^2 estimated as 0.8611:  log likelihood = -270.25,  aic = 548.49
arima(data.ts, order=c(0,1,1), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(0, 1, 1), method = "ML")
## 
## Coefficients:
##           ma1
##       -0.9294
## s.e.   0.1078
## 
## sigma^2 estimated as 0.93:  log likelihood = -276.14,  aic = 554.28
arima(data.ts, order=c(2,0,2), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(2, 0, 2), method = "ML")
## 
## Coefficients:
##           ar1     ar2     ma1      ma2  intercept
##       -0.1096  0.6350  0.3087  -0.6269    -0.0214
## s.e.   0.2164  0.1692  0.2283   0.2112     0.0931
## 
## sigma^2 estimated as 0.8456:  log likelihood = -267.26,  aic = 544.51