0

I'm trying to understand how filter function works in R.I saw below code

x <- 1:100
filter(x, filter=.50,method="recursive")

And I got output as

 [1]   1.00000   2.50000   4.25000   6.12500   8.06250  10.03125  12.01562  14.00781  16.00391
 [10]  18.00195  20.00098  22.00049  24.00024  26.00012  28.00006  30.00003  32.00002  34.00001
 [19]  36.00000  38.00000  40.00000  42.00000  44.00000  46.00000  48.00000  50.00000  52.00000
 [28]  54.00000  56.00000  58.00000  60.00000  62.00000  64.00000  66.00000  68.00000  70.00000
 [37]  72.00000  74.00000  76.00000  78.00000  80.00000  82.00000  84.00000  86.00000  88.00000
 [46]  90.00000  92.00000  94.00000  96.00000  98.00000 100.00000 102.00000 104.00000 106.00000
 [55] 108.00000 110.00000 112.00000 114.00000 116.00000 118.00000 120.00000 122.00000 124.00000
 [64] 126.00000 128.00000 130.00000 132.00000 134.00000 136.00000 138.00000 140.00000 142.00000
 [73] 144.00000 146.00000 148.00000 150.00000 152.00000 154.00000 156.00000 158.00000 160.00000
 [82] 162.00000 164.00000 166.00000 168.00000 170.00000 172.00000 174.00000 176.00000 178.00000
 [91] 180.00000 182.00000 184.00000 186.00000 188.00000 190.00000 192.00000 194.00000 196.00000
[100] 198.00000

I'm not getting how this output generated.Can you please help me understand?

R Learner
  • 545
  • 1
  • 7
  • 13

1 Answers1

2

According to the ?stats::filter, if we specify the method as "recursive" an autoregression is used and the filter argument takes a vector of filter coefficients in reverse time order. The recursive filter is based on

enter image description here

So, using the same principle, we multiply the filter coefficient i.e. 0.5 with the previous value and add with the current value

x1 <- x[1]
x2 <- x[2] + 0.5 * x1
x2
#[1] 2.5
x3 <- x[3] + 0.5 * x2
x3
#[1] 4.25
akrun
  • 874,273
  • 37
  • 540
  • 662