Skip to contents

Applies a recursive filter to a univariate time series.

Usage

rfilter(x, filter, init)

Arguments

x

A column vector of length T

filter

A column vector of length f

init

A column vector of length f that contains the initial values of the time series in reverse.

Value

x A column vector with its contents reversed.

Details

Note: The length of 'init' must be equal to the length of 'filter'. This is a port of the rfilter function harnessed by the filter function in stats. It is about 6-7 times faster than R's base function. The benchmark was done on iMac Late 2013 using vecLib as the BLAS.

Author

R Core Team and JJB

Examples

x = 1:100
# 
rfilter(x, rep(1, 3), rep(1, 3))
#>                [,1]
#>   [1,] 4.000000e+00
#>   [2,] 8.000000e+00
#>   [3,] 1.600000e+01
#>   [4,] 3.200000e+01
#>   [5,] 6.100000e+01
#>   [6,] 1.150000e+02
#>   [7,] 2.150000e+02
#>   [8,] 3.990000e+02
#>   [9,] 7.380000e+02
#>  [10,] 1.362000e+03
#>  [11,] 2.510000e+03
#>  [12,] 4.622000e+03
#>  [13,] 8.507000e+03
#>  [14,] 1.565300e+04
#>  [15,] 2.879700e+04
#>  [16,] 5.297300e+04
#>  [17,] 9.744000e+04
#>  [18,] 1.792280e+05
#>  [19,] 3.296600e+05
#>  [20,] 6.063480e+05
#>  [21,] 1.115257e+06
#>  [22,] 2.051287e+06
#>  [23,] 3.772915e+06
#>  [24,] 6.939483e+06
#>  [25,] 1.276371e+07
#>  [26,] 2.347613e+07
#>  [27,] 4.317935e+07
#>  [28,] 7.941923e+07
#>  [29,] 1.460747e+08
#>  [30,] 2.686734e+08
#>  [31,] 4.941674e+08
#>  [32,] 9.089155e+08
#>  [33,] 1.671756e+09
#>  [34,] 3.074839e+09
#>  [35,] 5.655511e+09
#>  [36,] 1.040211e+10
#>  [37,] 1.913246e+10
#>  [38,] 3.519007e+10
#>  [39,] 6.472464e+10
#>  [40,] 1.190472e+11
#>  [41,] 2.189619e+11
#>  [42,] 4.027337e+11
#>  [43,] 7.407427e+11
#>  [44,] 1.362438e+12
#>  [45,] 2.505915e+12
#>  [46,] 4.609096e+12
#>  [47,] 8.477449e+12
#>  [48,] 1.559246e+13
#>  [49,] 2.867900e+13
#>  [50,] 5.274891e+13
#>  [51,] 9.702037e+13
#>  [52,] 1.784483e+14
#>  [53,] 3.282176e+14
#>  [54,] 6.036862e+14
#>  [55,] 1.110352e+15
#>  [56,] 2.042256e+15
#>  [57,] 3.756294e+15
#>  [58,] 6.908902e+15
#>  [59,] 1.270745e+16
#>  [60,] 2.337265e+16
#>  [61,] 4.298900e+16
#>  [62,] 7.906910e+16
#>  [63,] 1.454308e+17
#>  [64,] 2.674889e+17
#>  [65,] 4.919887e+17
#>  [66,] 9.049083e+17
#>  [67,] 1.664386e+18
#>  [68,] 3.061283e+18
#>  [69,] 5.630577e+18
#>  [70,] 1.035625e+19
#>  [71,] 1.904811e+19
#>  [72,] 3.503493e+19
#>  [73,] 6.443928e+19
#>  [74,] 1.185223e+20
#>  [75,] 2.179965e+20
#>  [76,] 4.009581e+20
#>  [77,] 7.374770e+20
#>  [78,] 1.356432e+21
#>  [79,] 2.494867e+21
#>  [80,] 4.588775e+21
#>  [81,] 8.440074e+21
#>  [82,] 1.552372e+22
#>  [83,] 2.855256e+22
#>  [84,] 5.251635e+22
#>  [85,] 9.659264e+22
#>  [86,] 1.776616e+23
#>  [87,] 3.267705e+23
#>  [88,] 6.010247e+23
#>  [89,] 1.105457e+24
#>  [90,] 2.033252e+24
#>  [91,] 3.739734e+24
#>  [92,] 6.878443e+24
#>  [93,] 1.265143e+25
#>  [94,] 2.326960e+25
#>  [95,] 4.279948e+25
#>  [96,] 7.872051e+25
#>  [97,] 1.447896e+26
#>  [98,] 2.663096e+26
#>  [99,] 4.898197e+26
#> [100,] 9.009188e+26
# Using R's function
filter(x, rep(1, 3), method="recursive", init=rep(1, 3))
#> Time Series:
#> Start = 1 
#> End = 100 
#> Frequency = 1 
#>   [1] 4.000000e+00 8.000000e+00 1.600000e+01 3.200000e+01 6.100000e+01
#>   [6] 1.150000e+02 2.150000e+02 3.990000e+02 7.380000e+02 1.362000e+03
#>  [11] 2.510000e+03 4.622000e+03 8.507000e+03 1.565300e+04 2.879700e+04
#>  [16] 5.297300e+04 9.744000e+04 1.792280e+05 3.296600e+05 6.063480e+05
#>  [21] 1.115257e+06 2.051287e+06 3.772915e+06 6.939483e+06 1.276371e+07
#>  [26] 2.347613e+07 4.317935e+07 7.941923e+07 1.460747e+08 2.686734e+08
#>  [31] 4.941674e+08 9.089155e+08 1.671756e+09 3.074839e+09 5.655511e+09
#>  [36] 1.040211e+10 1.913246e+10 3.519007e+10 6.472464e+10 1.190472e+11
#>  [41] 2.189619e+11 4.027337e+11 7.407427e+11 1.362438e+12 2.505915e+12
#>  [46] 4.609096e+12 8.477449e+12 1.559246e+13 2.867900e+13 5.274891e+13
#>  [51] 9.702037e+13 1.784483e+14 3.282176e+14 6.036862e+14 1.110352e+15
#>  [56] 2.042256e+15 3.756294e+15 6.908902e+15 1.270745e+16 2.337265e+16
#>  [61] 4.298900e+16 7.906910e+16 1.454308e+17 2.674889e+17 4.919887e+17
#>  [66] 9.049083e+17 1.664386e+18 3.061283e+18 5.630577e+18 1.035625e+19
#>  [71] 1.904811e+19 3.503493e+19 6.443928e+19 1.185223e+20 2.179965e+20
#>  [76] 4.009581e+20 7.374770e+20 1.356432e+21 2.494867e+21 4.588775e+21
#>  [81] 8.440074e+21 1.552372e+22 2.855256e+22 5.251635e+22 9.659264e+22
#>  [86] 1.776616e+23 3.267705e+23 6.010247e+23 1.105457e+24 2.033252e+24
#>  [91] 3.739734e+24 6.878443e+24 1.265143e+25 2.326960e+25 4.279948e+25
#>  [96] 7.872051e+25 1.447896e+26 2.663096e+26 4.898197e+26 9.009188e+26