load("workspace.RData")
Stationary models are suitable for residual series that contain no obvious trends or seasonal cycles.
See the theory here
The code below simulates the MA process
set.seed(1)
b <- c(0.8,0.6,0.4)
x <- w <- rnorm(1000)
for (t in 4:1000){
for (j in 1:3) x[t] <- x[t]+b[j]*w[t-j]
}
Lets plot x
plot(x,type="l")
lets take the ACF of (x)
acf(x)
As expected, the first three autocorrelations are significantly different from 0.
c An MA(q) model can be fitted to data in R using arima function with order function parameters set to c(0,0,q)
x.ma <- arima(x,order=c(0,0,3))
x.ma
##
## Call:
## arima(x = x, order = c(0, 0, 3))
##
## Coefficients:
## ma1 ma2 ma3 intercept
## 0.7898 0.5665 0.3959 -0.0322
## s.e. 0.0307 0.0351 0.0320 0.0898
##
## sigma^2 estimated as 1.068: log likelihood = -1452.41, aic = 2914.83
We have taken coefficients as 0.8, 0.6 and 0.4, so the function works ok.
x <- read.table("ts/pounds_nz.dat",header=T)
x.ts <- ts(x,st=1991,fr=4)
x.ma <- arima(x.ts,order=c(0,0,1))
x.ma
##
## Call:
## arima(x = x.ts, order = c(0, 0, 1))
##
## Coefficients:
## ma1 intercept
## 1.000 2.8329
## s.e. 0.072 0.0646
##
## sigma^2 estimated as 0.04172: log likelihood = 4.76, aic = -3.53
lets plot ACF of residual of x
acf(x.ma$res[-1])
Thus the model doesn’t provide a satisfactory fit as the residual series is clearly not a realistic realization of white noise.
The ARMA process can be simulated using R function arima.sim, with the order function paramameter set to c(p,0,q). Let alpha = -0.6 and beta = 0.5
set.seed(1)
x <- arima.sim(n=10000,list(ar=-0.6,ma=0.5))
coef(arima(x,order=c(1,0,1)))
## ar1 ma1 intercept
## -0.596966371 0.502703368 -0.006571345
As we can see the sample estimates of alpha and beta are close to the model parameters
Lets try to fit the Exchange rate series using MA(1), AR(1) and ARMA(1,1)and compare it using AIC.
x.ma <- arima(x.ts,order=c(0,0,1))
x.ar <- arima(x.ts,order=c(1,0,0))
x.arma <- arima(x.ts,order=c(1,0,1))
AIC(x.ma);AIC(x.ar);AIC(x.arma)
## [1] -3.526895
## [1] -37.40417
## [1] -42.27357
AIC is lowest for x.arma
x.arma
##
## Call:
## arima(x = x.ts, order = c(1, 0, 1))
##
## Coefficients:
## ar1 ma1 intercept
## 0.8925 0.5319 2.9597
## s.e. 0.0759 0.2021 0.2435
##
## sigma^2 estimated as 0.01505: log likelihood = 25.14, aic = -42.27
Lets find the acf
acf(resid(x.arma))
This is fitting well.
You can choose the best fitting ARMA (p,q) model is chosen using the smallest AIC either by using a for loop with upper bound for p and q- let us say 2. In each step of the for loop, the AIC of the fitted model is compared with the currently stored smallest value. if the model has a smaller AIC, then the new value and model is stored. To start with best.aic is initialized to infinity (INF), after the loop is completed, the best model can be found in best order.
best.order <- c(0,0,0)
best.aic <- Inf
for (i in 0:2) for (j in 0:2) {
fit.aic <- AIC(arima(x.ts, order = c(i, 0,
j)))
if (fit.aic < best.aic) {
best.order <- c(i, 0, j)
best.arma <- arima(x.ts, order = best.order)
best.aic <- fit.aic
}
}
best.order
## [1] 2 0 1
Here we found that 2,0,1 is the best order, lets see its AIC
x.arma1 <- arima(x.ts,order=c(2,0,1))
AIC(x.arma1)
## [1] -42.80284
Here AIC is the least
save.image("workspace.Rdata")