I am trying to create a UDF for forecasting with polynomial regression. Assistance with the final line of code will be appreciated. The code thus far is as follows:
Function Forecast_PolyTest(X As Single, knownYs As Range, knownXs As Range, Order As Integer)
Dim k, OrdArr, PwrArr
Dim Ord2c, Ord3c, Ord4c, Ord5c, Ord6col
Dim Ord2r, Ord3r, Ord4r, Ord5, Ord6row
Dim Pwr2, Pwr3, Pwr4, Pwr5, Pwr6
Dim Skip As Integer
Ord2c = "{1,2}"
Ord3c = "{1,2,3}"
Ord4c = "{1,2,3,4}"
Ord5c = "{1,2,3,4,5}"
Ord6c = "{1,2,3,4,5,6}"
Ord2r = "{1;2}"
Ord3r = "{1;2;3}"
Ord4r = "{1;2;3;4}"
Ord5r = "{1;2;3;4;5}"
Ord6r = "{1;2;3;4;5;6}"
Pwr2 = "{2,1,0}"
Pwr3 = "{3,2,1,0}"
Pwr4 = "{4,3,2,1,0}"
Pwr5 = "{5,4,3,2,1,0}"
Pwr6 = "{6,5,4,3,2,1,0}"
'SELECT ARRAYS BASED ON ORDER AND WHETHER DATA IS IN COLUMNS OR ROWS
If knownXs.Columns.Count > 1 Then Skip = 4 Else Skip = -1
OrdArr = Application.Choose(Order + Skip, Ord2c, Ord3c, Ord4c, Ord5c, Ord6c, Ord2r, Ord3r, Ord4r, Ord5r, Ord6r)
PwrArr = Application.Choose(Order - 1, Pwr2, Pwr3, Pwr4, Pwr5, Pwr6)
'APPLY LINEST EQUATION
k = Application.Evaluate("=linest(" & knownYs.Address & "," & knownXs.Address & " ^ " & OrdArr & ")")
'FORMULA IN NATIVE FUNCTIONS = SUMPRODUCT(k, X^PwrArr) ALSO = {SUM(k * X^PwrArr)}
'I NEED HELP WITH THIS FINAL STATEMENT:
'Forecast_PolyTest = ?
'NONE OF THESE STATEMENTS WORKED:
'Forecast_PolyTest = Application.Sum(k(1) * X ^ 2, k(2) * X, k(3)) 'works, but is only accurate for Order=2
'Forecast_PolyTest = Application.Evaluate("=Sum(k * X ^ PwrArr)")
'Forecast_PolyTest = Application.Evaluate("=Sum(" & k & " * " & X & " ^ " & PwrArr & ")")
'Forecast_PolyTest = Application.Evaluate("=Sumproduct(k, X ^ PwrArr)")
'Forecast_PolyTest = Application.Evaluate("=Sumproduct(" & k & " ," & X & " ^ " & PwrArr & ")")
'Forecast_PolyTest = Application.WorksheetFunction.SumProduct(" & k & ", " & X & " ^ " & PwrArr & ")
'Forecast_PolyTest = PwrArr 'showed the correct power array is returned and is therefore not the problem
End Function
Function Forecast_PolyTest(X As Single, knownYs As Range, knownXs As Range, Order As Integer)
Dim k, OrdArr, PwrArr
Dim Ord2c, Ord3c, Ord4c, Ord5c, Ord6col
Dim Ord2r, Ord3r, Ord4r, Ord5, Ord6row
Dim Pwr2, Pwr3, Pwr4, Pwr5, Pwr6
Dim Skip As Integer
Ord2c = "{1,2}"
Ord3c = "{1,2,3}"
Ord4c = "{1,2,3,4}"
Ord5c = "{1,2,3,4,5}"
Ord6c = "{1,2,3,4,5,6}"
Ord2r = "{1;2}"
Ord3r = "{1;2;3}"
Ord4r = "{1;2;3;4}"
Ord5r = "{1;2;3;4;5}"
Ord6r = "{1;2;3;4;5;6}"
Pwr2 = "{2,1,0}"
Pwr3 = "{3,2,1,0}"
Pwr4 = "{4,3,2,1,0}"
Pwr5 = "{5,4,3,2,1,0}"
Pwr6 = "{6,5,4,3,2,1,0}"
'SELECT ARRAYS BASED ON ORDER AND WHETHER DATA IS IN COLUMNS OR ROWS
If knownXs.Columns.Count > 1 Then Skip = 4 Else Skip = -1
OrdArr = Application.Choose(Order + Skip, Ord2c, Ord3c, Ord4c, Ord5c, Ord6c, Ord2r, Ord3r, Ord4r, Ord5r, Ord6r)
PwrArr = Application.Choose(Order - 1, Pwr2, Pwr3, Pwr4, Pwr5, Pwr6)
'APPLY LINEST EQUATION
k = Application.Evaluate("=linest(" & knownYs.Address & "," & knownXs.Address & " ^ " & OrdArr & ")")
'FORMULA IN NATIVE FUNCTIONS = SUMPRODUCT(k, X^PwrArr) ALSO = {SUM(k * X^PwrArr)}
'I NEED HELP WITH THIS FINAL STATEMENT:
'Forecast_PolyTest = ?
'NONE OF THESE STATEMENTS WORKED:
'Forecast_PolyTest = Application.Sum(k(1) * X ^ 2, k(2) * X, k(3)) 'works, but is only accurate for Order=2
'Forecast_PolyTest = Application.Evaluate("=Sum(k * X ^ PwrArr)")
'Forecast_PolyTest = Application.Evaluate("=Sum(" & k & " * " & X & " ^ " & PwrArr & ")")
'Forecast_PolyTest = Application.Evaluate("=Sumproduct(k, X ^ PwrArr)")
'Forecast_PolyTest = Application.Evaluate("=Sumproduct(" & k & " ," & X & " ^ " & PwrArr & ")")
'Forecast_PolyTest = Application.WorksheetFunction.SumProduct(" & k & ", " & X & " ^ " & PwrArr & ")
'Forecast_PolyTest = PwrArr 'showed the correct power array is returned and is therefore not the problem
End Function