Reverting a linear filter for time series in R

Viewed 121

I'm using the stats::filter function in R in order to understand ARIMA simulations in R (as in the function stats::arima.sim) and estiamtion. I know that stats::filter applies a linear filter to a vector or time series, but I'm not sure how to "unfilter" my series.

Consider the following example: I want to use a recursive filter with value 0.7 to my series x = 1:5 (which is essentially generating an AR(1) with phi=0.7). I can do so by:

x    <- 1:5 
ar   <-0.7 
filt <- filter(x, ar, method="recursive")
filt 

Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1]  1.0000  2.7000  4.8900  7.4230 10.1961

Which returns me essentially c(y1,y2,y3,y4,y5) where:

y1 <- x[1]                                            
y2 <- x[2] + ar*y1      
y3 <- x[3] + ar*y2 
y4 <- x[4] + ar*y3 
y5 <- x[5] + ar*y4 

Now imagine I have the y = c(y1,y2,y3,y4,y5) series. How can I use the filter function to return me the original series x = 1:5?

I can write a code to do it like:

unfilt <- rep(NA, 5)
unfilt[1] <- filt[1]

for(i in 2:5){
   unfilt[i] <- filt[i] - ar*filt[i-1]
}
unfilt
[1] 1 2 3 4 5

But I do want to use the filter function to do so, instead of writing my own function. How can I do so? I tried stats::filter(filt, -ar, method="recursive"), which returns me [1] 1.0000 2.0000 3.4900 4.9800 6.7101 not what I desire.

1 Answers

stats::filter used with the recursive option is a particular case of an ARMA filter.

a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]  

You could implement this filter with the signal package which allows more options than stat::filter :

a = c(1,-ar)
b = 1

filt_Arma <- signal::filter(signal::Arma(b = b, a = a),x)
filt_Arma

# Time Series:
# Start = 1 
# End = 5 
# Frequency = 1 
# [1]  1.0000  2.7000  4.8900  7.4230 10.1961

identical(filt,filt_Arma)
# [1] TRUE

Reverting an ARMA filter can be done by switching b and a, provided that the inverse filter stays stable (which is the case here):

signal::filter(signal::Arma(b = a, a = b),filt)

# Time Series:
# Start = 2 
# End = 6 
# Frequency = 1 
# [1] 1 2 3 4 5

This corresponds to switching numerator and denominator in the z-transform:

Y(z) = a(z)/b(z) X(z)

X(z) = b(z)/a(z) Y(z)
Related