Noldor130884
Noldor130884

Reputation: 994

Using LINEST with arrays as variable (not ranges)

I tried to google myself out of this problem and it seems I can't find any solution to it.

Naturally I thought to use LinEst. The syntax of LinEst lets you give "known ys", "known xs" and a couple other parameter I currently don't need.

  1. Now, if I use Application.LinEst it seems that my arrays are processed, but I can't force the procedure to give me the coefficient of a 3rd degree polynom (can I?)
  2. while if I try with WorksheetFunction.LinEst I can specify the degree, but I cannot apparently work with my variables.

Of course I COULD write my arrays somewhere and then use the second option, but I'd really like to understand if there is a better way...

Upvotes: 0

Views: 2032

Answers (1)

brettdj
brettdj

Reputation: 55682

(1) Polynomial using LINEST and a Chart

enter image description here

(2) Same result using VBA with variants
(note (i) Option 1 uses 1D variants, Option 2 uses 2D (ii) the key piece using Power I saw a while back on a different forum)

 Sub Test()
 Dim Y
 Dim X
 Dim Arr1
 Dim Arr2
 With Application
    Y = .Transpose([a1:a10])
    X = .Transpose([b1:b10])
    Arr1 = .Power(.Transpose(X), Array(1, 2, 3))
    Arr2 = .LinEst(Y, .Transpose(Arr1))
     End With
 MsgBox "coefficients are " & Chr(10) & Join(Arr2, Chr(10))
 End Sub

2B Same result using VBA with 2D variants

 Sub Test2()
 Dim Y
 Dim X
 Dim Arr1
 Dim Arr2
 With Application
    Y = [a1:a10]
    X = [b1:b10]
    Arr1 = .Power(X, Array(1, 2, 3))
    Arr2 = .LinEst(Y, Arr1)
 End With
 MsgBox "coefficients are " & Chr(10) & Join(Arr2, Chr(10))
 End Sub

Upvotes: 4

Related Questions