library(tseries)
## Warning: package 'tseries' was built under R version 4.5.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.5.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.5.3
## Registered S3 methods overwritten by 'forecast':
##   method       from
##   fitted.Arima TSA 
##   plot.Arima   TSA

Generating Data

set.seed(1)
n <- 100
AR <- 0.4
MA <- 1.5
ARIMA <- arima.sim(model = list(order= c(1,1,1), ar = AR, ma = MA), n=n)

ts.plot(ARIMA, main="ARIMA(1,1,1) Simulation")

# ACF and PACF

acf(ARIMA)

pacf(ARIMA)

# ADF Test

adf.test(ARIMA)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ARIMA
## Dickey-Fuller = -2.7027, Lag order = 4, p-value = 0.2854
## alternative hypothesis: stationary

p-value=0.2854>0.05. Data isnt stationair, need differencing

Differencing

differ <- diff(ARIMA)
acf(differ)

pacf(differ)

adf.test(differ)
## Warning in adf.test(differ): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  differ
## Dickey-Fuller = -4.4894, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary

p-value=0.01<0.05. Data is stationair

Candidate Model

data.ts <- ts(differ)
head(data.ts)
## Time Series:
## Start = 1 
## End = 6 
## Frequency = 1 
## [1]  1.438650  1.629159  3.309178  1.287196 -2.631682 -3.249792
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 x x o o o o o o  o  o  o 
## 1 x x o o o o o o o o o  o  o  o 
## 2 o 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 o x o o o o o o o o o  o  o  o 
## 5 o x o o o o o o o o o  o  o  o 
## 6 o x o o o o o o o o o  o  o  o 
## 7 o x o o o o o o o o o  o  o  o
auto.arima(data.ts)
## Series: data.ts 
## ARIMA(1,0,1) with zero mean 
## 
## Coefficients:
##          ar1     ma1
##       0.4470  0.6356
## s.e.  0.1063  0.0944
## 
## sigma^2 = 1.884:  log likelihood = -173.16
## AIC=352.32   AICc=352.57   BIC=360.14
auto.arima(ARIMA)
## Series: ARIMA 
## ARIMA(1,1,1) 
## 
## Coefficients:
##          ar1     ma1
##       0.4470  0.6356
## s.e.  0.1063  0.0944
## 
## sigma^2 = 1.884:  log likelihood = -173.16
## AIC=352.32   AICc=352.57   BIC=360.14

ARIMA Candidate ARIMA (1,1,1), ARIMA (2,1,1), ARIMA (1,0,1)

Best Model based on 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.3420  0.7367
## s.e.   0.1851  0.1386
## 
## sigma^2 estimated as 2.389:  log likelihood = -183.75,  aic = 371.49
arima(data.ts, order = c(2,1,1), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(2, 1, 1), method = "ML")
## 
## Coefficients:
##          ar1      ar2      ma1
##       0.9935  -0.4675  -1.0000
## s.e.  0.0879   0.0884   0.0277
## 
## sigma^2 estimated as 1.837:  log likelihood = -172.7,  aic = 351.4
arima(data.ts, order = c(1,0,1), method = "ML")
## 
## Call:
## arima(x = data.ts, order = c(1, 0, 1), method = "ML")
## 
## Coefficients:
##          ar1     ma1  intercept
##       0.4217  0.6428     0.5137
## s.e.  0.1080  0.0939     0.3786
## 
## sigma^2 estimated as 1.814:  log likelihood = -172.28,  aic = 350.56

The best model after comparing AIC on the candidate model was ARIMA (1,0,1). This model is data generating model used in the simulation. The consistency between the selected model and the true model indicates that the dependence structure embedded in the data is well captured, allowing the model selection procedure to correctly identify the underlying process.