excel 数组多项式回归

bjg7j2ky  于 2023-02-05  发布在  其他
关注(0)|答案(3)|浏览(192)

看来,我不是唯一一个与此斗争,但我找不到一个好的答案,所以我在这里尝试我的机会!
我想找到一条最佳拟合的三次多项式直线,给定两组存储在数组中的数据。

Dim X(0 to 9) as Integer
Dim Y(0 to 9) as Integer
for k = 0 to 9
   X(k) = 'something
   Y(k) = 'something else
Next

到目前为止,我已经解决了如何用一次多项式Y = aX + B来解决我的问题:

a = Application.WorksheetFunction.LinEst(Y, X, True, False)(1)
b = Application.WorksheetFunction.LinEst(Y, X, True, False)(2)

我还发现,如果将值X和Y写入Sheet,可以用以下公式找到更高的多项式:

'x-axis values are entered in X column, y-values in Y column
 Z = Application.Evaluate("=linest(Y1:Y10,X1:X10^{1,2,3})")
 'The answer is in Z such that Y = Z(1)*Y^3+Z(2)*Y^2+Z(3)*Y+Z(4)

假设我的数组已经排序了,我如何将linest用于数组而不用于单元格中输入的值?

irlmq6kh

irlmq6kh1#

如果需要OLS最佳拟合系数(即线性回归)而不是插值,则可以对二阶多项式执行以下操作:

Sub test()

    Dim X(0 To 9, 0 To 1) As Integer
    Dim Y(0 To 9, 0 To 0) As Integer

    i = 0
    For n = 0 To 9
       X(n, 0) = i
       X(n, 1) = i * i
       Y(n, 0) = i * i + 3 * i - 7
       i = i + 1
    Next

    B = WorksheetFunction.LinEst(Y, X)
    B2 = B(1)
    B1 = B(2)
    B0 = B(3)

End Sub

对于B0,它正确地返回-7;对于B1,它正确地返回3;对于B2,它正确地返回1https://stackoverflow.com/a/27137957/1011724
你可以把它变得更一般,比如k阶多项式,如下所示:

Function f(X) As Integer ' This function is replaced by your data
    f = -2 * WorksheetFunction.Power(X, 3) + 3 * X - 7
End Function

Sub test2()

    Order = 3

    ReDim X(0 To 9, 0 To Order - 1)
    Dim Y(0 To 9, 0 To 0) As Integer

    'Note i is only to generate dummy data
    i = 1
    For n = 0 To 9 'Replace 9 with the length of your data
       X(n, 0) = i 'This line is replaced by your actual data
       ' Create the higher order features:
       For k = 1 To Order
           X(n, k-1) = Application.WorksheetFunction.Power(i, k)
       Next
       Y(n, 0) = f(i) 'This line is replaced by your actual data
       i = i + 1
    Next

    B = WorksheetFunction.LinEst(Y, X)

End Sub
6uxekuva

6uxekuva2#

我发现了另一种使用数组进行多项式回归的方法。代码在这个链接中。https://rosettacode.org/wiki/Polynomial_regression。因为网站上的代码由于数组不正确而无法工作,我做了一个小的更正。现在它可以工作了。

Function polynomial_regression(y As Variant, x As Variant, degree As Integer) As Variant
    Dim a() As Double
    ReDim a(1 To UBound(x), 1 To degree)
    For i = 1 To UBound(x)
        For j = 1 To degree
            a(i, j) = x(i) ^ j
        Next j
    Next i
    polynomial_regression = WorksheetFunction.LinEst(WorksheetFunction.Transpose(y), a, True, True)

End Function
kse8i1jr

kse8i1jr3#

我在上面的函数中添加了一个函数,它使用范围变量作为输入,相当于Excel函数linest()

Public Function RangeToArray(ByRef myRange As Variant) As Variant

    Dim individualCell As Range
    Dim i As Integer
    
    ReDim myArray(1 To myRange.Count)
    i = 1
    For Each individualCell In myRange
        myArray(i) = individualCell.Value     
        i = i + 1
    Next
    RangeToArray = myArray
    
End Function

Function PolyFit(yRange As Variant, xRange As Variant, degree As Integer) As Variant
    Dim xAry() As Variant
    Dim yAry() As Variant
    Dim a() As Double
    
    xAry() = RangeToArray(xRange)
    yAry() = RangeToArray(yRange)
    
    ReDim a(1 To (UBound(xAry)), 1 To (degree))
    For i = 1 To (UBound(xAry))
        For j = 1 To (degree)
            a(i, j) = xAry(i) ^ (j)
        Next j
    Next i
    PolyFit = WorksheetFunction.LinEst(WorksheetFunction.Transpose(yAry), a, True, False)
    
End Function

相关问题