# Filtering using ARMA model in R

I have two time-series, x and y. I would like to prewhiten x by fitting an ARMA(p,q) (or in my case ARMA(1,1)) process and then use the coefficients to filter y. This seems like a pretty standard thing to want to do. However, the stats:::filter function does only MA or AR filtering it looks like. What is the appropriate way to do this? Also, should one use the arima function in R to do this or are there other ways?

I think this does what you want:

library(forecast)
fit <- Arima(x,order=c(1,0,1))
yfiltered <- residuals(Arima(y,model=fit))

• could you please explain how Arima with model parameter works? in particular: if i filter x with $(1-aL)x_{t} = (1-bL)\varepsilon_{t}$, this can be accomplished by fit <- Arima(x,order=c(1,0,1)), correct? Then when I run residuals(Arima(y,model=fit)), does this produce $y_{t}-[(1-aL)y_{t} - (1-bL)u_{t}]$ where $u_{t}$ are residuals of $(1-aL)y_{t})$?
– Alex
Nov 12, 2012 at 21:03
• Yes. See help file: "model: Output from a previous call to Arima. If model is passed, this same model is fitted to x without re-estimating any parameters." Nov 12, 2012 at 22:51
• yes, took a look. was having trouble udnerstanding what was meant by residuals. think i have it now. thanks!
– Alex
Nov 12, 2012 at 23:07

I suggest three different functions:

stats:::arima

forecast:::Arima

forecast:::auto.arima

forecast:::auto.arima will automatically seelct the p and q lags for you.