Need VBA for SumProduct of Array and Constant raised by Array for Regression

LRobert

New Member
Joined
Nov 12, 2019
Messages
4
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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi LRobert,

if this works:
'Forecast_PolyTest = Application.Sum(k(1) * X ^ 2, k(2) * X, k(3)) 'works, but is only accurate for Order=2
You can try an if statement:
Code:
If Order = 2 Then
    Forecast_PolyTest = Application.Sum(k(1) * X ^ 2, k(2) * X, k(3))
ElseIf Order = 3 Then
    Forecast_PolyTest = Application.Sum(k(1) * X ^ 3, k(2) * X ^ 2, k(3) * X, k(4))
Else
    'etc
End If
Does that work?
Koen
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top