过滤函数的简单示例,具体为递归选项

the*_*ail 25 r time-series filter

我正在寻找一些简单的(即 - 没有数学符号,长形式可重现的代码)filterR中的函数示例我认为我对卷积方法有所了解,但我坚持推广递归选项.我已阅读并与各种文档作斗争,但帮助对我来说有点不透明.

以下是我到目前为止所发现的例子:

# Set some values for filter components
f1 <- 1; f2 <- 1; f3 <- 1;
Run Code Online (Sandbox Code Playgroud)

我们走了:

# basic convolution filter
filter(1:5,f1,method="convolution")
[1] 1 2 3 4 5

#equivalent to:
x[1] * f1 
x[2] * f1 
x[3] * f1 
x[4] * f1 
x[5] * f1 

# convolution with 2 coefficients in filter
filter(1:5,c(f1,f2),method="convolution")
[1]  3  5  7  9 NA

#equivalent to:
x[1] * f2 + x[2] * f1
x[2] * f2 + x[3] * f1
x[3] * f2 + x[4] * f1 
x[4] * f2 + x[5] * f1 
x[5] * f2 + x[6] * f1

# convolution with 3 coefficients in filter
filter(1:5,c(f1,f2,f3),method="convolution")
[1] NA  6  9 12 NA

#equivalent to:
 NA  * f3 + x[1] * f2 + x[2] * f1  #x[0] = doesn't exist/NA
x[1] * f3 + x[2] * f2 + x[3] * f1
x[2] * f3 + x[3] * f2 + x[4] * f1 
x[3] * f3 + x[4] * f2 + x[5] * f1 
x[4] * f3 + x[5] * f2 + x[6] * f1
Run Code Online (Sandbox Code Playgroud)

现在是我伤害了我可怜的小脑干.我设法在这篇文章中找出使用信息的最基本的例子:https://stackoverflow.com/a/11552765/496803

filter(1:5, f1, method="recursive")
[1]  1  3  6 10 15

#equivalent to:

x[1]
x[2] + f1*x[1]
x[3] + f1*x[2] + f1^2*x[1]
x[4] + f1*x[3] + f1^2*x[2] + f1^3*x[1]
x[5] + f1*x[4] + f1^2*x[3] + f1^3*x[2] + f1^4*x[1]
Run Code Online (Sandbox Code Playgroud)

有人可以提供类似的代码,我的上面的卷积示例的递归版本与filter = c(f1,f2)filter = c(f1,f2,f3)

答案应该匹配函数的结果:

filter(1:5, c(f1,f2), method="recursive")
[1]  1  3  7 14 26

filter(1:5, c(f1,f2,f3), method="recursive")
[1]  1  3  7 15 30
Run Code Online (Sandbox Code Playgroud)

编辑

最后使用@ agstudy的简洁答案:

> filter(1:5, f1, method="recursive")
Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1]  1  3  6 10 15
> y1 <- x[1]                                            
> y2 <- x[2] + f1*y1      
> y3 <- x[3] + f1*y2 
> y4 <- x[4] + f1*y3 
> y5 <- x[5] + f1*y4 
> c(y1,y2,y3,y4,y5)
[1]  1  3  6 10 15
Run Code Online (Sandbox Code Playgroud)

和...

> filter(1:5, c(f1,f2), method="recursive")
Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1]  1  3  7 14 26
> y1 <- x[1]                                            
> y2 <- x[2] + f1*y1      
> y3 <- x[3] + f1*y2 + f2*y1
> y4 <- x[4] + f1*y3 + f2*y2
> y5 <- x[5] + f1*y4 + f2*y3
> c(y1,y2,y3,y4,y5)
[1]  1  3  7 14 26
Run Code Online (Sandbox Code Playgroud)

和...

> filter(1:5, c(f1,f2,f3), method="recursive")
Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1]  1  3  7 15 30
> y1 <- x[1]                                            
> y2 <- x[2] + f1*y1      
> y3 <- x[3] + f1*y2 + f2*y1
> y4 <- x[4] + f1*y3 + f2*y2 + f3*y1
> y5 <- x[5] + f1*y4 + f2*y3 + f3*y2
> c(y1,y2,y3,y4,y5)
[1]  1  3  7 15 30
Run Code Online (Sandbox Code Playgroud)

ags*_*udy 20

在递归的情况下,我认为不需要扩展xi方面的表达式.具有"递归"的关键是用前面的y表示右手表达.

我更喜欢考虑过滤器大小.

过滤器大小= 1

y1 <- x1                                            
y2 <- x2 + f1*y1      
y3 <- x3 + f1*y2 
y4 <- x4 + f1*y3 
y5 <- x5 + f1*y4 
Run Code Online (Sandbox Code Playgroud)

过滤器大小= 2

y1 <- x1                                            
y2 <- x2 + f1*y1      
y3 <- x3 + f1*y2 + f2*y1    # apply the filter for the past value and add current input
y4 <- x4 + f1*y3 + f2*y2
y5 <- x5 + f1*y4 + f2*y3
Run Code Online (Sandbox Code Playgroud)