我在论坛上找到了一个宏,它是用于时间序列数据的平滑过滤器,名为 Loess。但是,我无法正确运行该宏,因为我不确定参数是什么。这里是我想要平滑的数据。宏代码如下:
Public Function Loess(x As Variant, y As Variant, xnew As Variant, alpha As Variant, lambda As Integer)
Dim n As Integer
n = x.Rows.count
n2 = xnew.Rows.count
Dim q As Variant
q = Application.WorksheetFunction.Floor(alpha * n, 1)
q = Application.WorksheetFunction.Max(q, 1)
q = Application.WorksheetFunction.Min(q, n)
Dim z() As Variant
ReDim z(n2)
Dim i As Integer
For i = 0 To n2 - 1
Dim deltax() As Variant
ReDim deltax(n)
Dim j As Integer
For j = 0 To n - 1
deltax(j) = Abs(xnew(i + 1) - x(j + 1))
Next j
Dim qthdeltax As Variant
qthdeltax = Application.WorksheetFunction.Small(deltax, q)
Dim arg() As Variant
ReDim arg(n)
For j = 0 To n - 1
arg(j) = Application.WorksheetFunction.Min(deltax(j) / (qthdeltax * Application.WorksheetFunction.Max(alpha, 1)), 1)
Next j
Dim tricube() As Variant
ReDim tricube(n)
Dim count As Integer
count = 0
For j = 0 To n - 1
tricube(j) = (1 - Abs(arg(j)) ^ 3) ^ 3
If tricube(j) > 0 Then
count = count + 1
End If
Next j
Dim weight() As Variant
ReDim weight(count - 1)
Dim count1, zeroes As Integer
count1 = 0
zeroes = 0
For j = 0 To n - 1
If tricube(j) > 0 Then
weight(count1) = tricube(j)
count1 = count1 + 1
Else
If count1 = 0 Then
zeroes = zeroes + 1
End If
End If
Next j
Dim weightx(), weighty() As Variant
ReDim weightx(count - 1), weighty(count - 1)
For j = 0 To count - 1
weightx(j) = x(zeroes + j + 1)
weighty(j) = y(zeroes + j + 1)
Next j
Dim p() As Variant
ReDim p(lambda + 1, lambda + 1)
p = Least2(weightx, weighty, lambda, weight)
z(i) = Polyval(Application.WorksheetFunction.Transpose(p), xnew(i + 1))
Next i
Loess = Application.WorksheetFunction.Transpose(z)
End Function