Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
When modelling data with ARIMA models, it is sometimes useful to plot the inverse characteristic roots. The following functions will compute and plot the inverse roots for any fitted ARIMA model (including seasonal models).
# Compute AR roots arroots <- function(object) { if(class(object) != "Arima" & class(object) != "ar") stop("object must be of class Arima or ar") if(class(object) == "Arima") parvec <- object$model$phi else parvec <- object$ar if(length(parvec) > 0) { last.nonzero <- max(which(abs(parvec) > 1e-08)) if (last.nonzero > 0) return(structure(list(roots=polyroot(c(1,-parvec[1:last.nonzero])), type="AR"), class='armaroots')) } return(structure(list(roots=numeric(0),type="AR"),class='armaroots')) } # Compute MA roots maroots <- function(object) { if(class(object) != "Arima") stop("object must be of class Arima") parvec <- object$model$theta if(length(parvec) > 0) { last.nonzero <- max(which(abs(parvec) > 1e-08)) if (last.nonzero > 0) return(structure(list(roots=polyroot(c(1,parvec[1:last.nonzero])), type="MA"), class='armaroots')) } return(structure(list(roots=numeric(0),type="MA"),class='armaroots')) } plot.armaroots <- function(x, xlab="Real",ylab="Imaginary", main=paste("Inverse roots of",x$type,"characteristic polynomial"), ...) { oldpar <- par(pty='s') on.exit(par(oldpar)) plot(c(-1,1),c(-1,1),xlab=xlab,ylab=ylab, type="n",bty="n",xaxt="n",yaxt="n", main=main, ...) axis(1,at=c(-1,0,1),line=0.5,tck=-0.025) axis(2,at=c(-1,0,1),label=c("-i","0","i"),line=0.5,tck=-0.025) circx <- seq(-1,1,l=501) circy <- sqrt(1-circx^2) lines(c(circx,circx),c(circy,-circy),col='gray') lines(c(-2,2),c(0,0),col='gray') lines(c(0,0),c(-2,2),col='gray') if(length(x$roots) > 0) { inside <- abs(x$roots) > 1 points(1/x$roots[inside],pch=19,col='black') if(sum(!inside) > 0) points(1/x$roots[!inside],pch=19,col='red') } } |
The arroots
function will return the autoregressive roots from the AR characteristic polynomial while the maroots
function will return the moving average roots from the MA characteristic polynomial. Both functions take an Arima
object as their only argument. If a seasonal ARIMA model is passed, the roots from both polynomials are computed in each case.
The plot.armaroots
function will plot the inverse of the roots on the complex unit circle. A causal invertible model should have all the roots outside the unit circle. Equivalently, the inverse roots should like inside the unit circle.
Here are a couple of examples demonstrating their use.
A simple example with three AR roots:
library(forecast) plot(arroots(Arima(WWWusage,c(3,1,0)))) |
A more complicated example with ten AR roots and four MA roots. (This is not actually the best model for these data.)
library(forecast) fit <- Arima(woolyrnq,order=c(2,0,0),seasonal=c(2,1,1)) par(mfrow=c(1,2)) plot(arroots(fit),main="Inverse AR roots") plot(maroots(fit),main="Inverse MA roots") |
Finally, here is an example where two inverse roots lie outside the unit circle (shown in red).
library(fma) plot(arroots(ar.ols(jcars))) |
Note that the Arima
function will never return a model with inverse roots outside the unit circle. The auto.arima
function is even stricter and will not select a model with roots close to the unit circle either, as such models are unlikely to be good for forecasting.
I won’t add these functions to the forecast
package as I don’t think enough people would use them, and the package is big enough as it is. So I’m making them available here instead for anyone who wants to use them.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.