MrRenstrom
New Member
- Joined
- Dec 27, 2013
- Messages
- 4
Hi,
I am totally puzzled by the behaviour of VBA application.linest. I am trying to create a function that is dynamic in respect of the order of a polynom that is fitted to a known set of data. That is, with a least square approximation trying to fit f(x)=a*x^i+b*x^(i-1)+...+constant term. Now, it works fine for polynoms up to quadratic order but then it implies that there is collinearity by consistently returning zeros for all orders lower than x^(i-1), except for the constant term. I can not figure out why, can you? The function below does not give me any errors or so otherwise...
Function LeastSquarePoly(Data() As Variant, Xindx As Integer, Yindx As Integer, Posindx As Integer, _
order As Integer, Period As Integer) As Variant
' Calculates the least square approximation with a polynom of order "order"
' Returns the coefficients to the polynom, or Empty if the period is longer than the
' target array of values or if any value is Empty
' The Calculation is done backwards from each data value, i.e. the actual position
' and the n-1 previous values of InArray
Dim FoundEmpty As Boolean
FoundEmpty = False
If Period > Posindx Then
LeastSquarePoly = Empty
Else
Dim Coeff, XkPow As Variant
Dim Xk(), Yk() As Variant
Dim PolyArr() As Variant
ReDim PolyArr(1 To order)
Dim i As Integer
For i = 1 To order
PolyArr(i) = i
Next
ReDim Xk(1 To Period)
ReDim Yk(1 To Period)
For i = 1 To Period
Xk(i) = CDbl(Data(Xindx, Posindx - Period + i))
Yk(i) = CDbl(Data(Yindx, Posindx - Period + i))
If Xk(i) = Empty Or Yk(i) = Empty Then
LeastSquarePoly = Empty
FoundEmpty = True
End If
Next
If Not FoundEmpty Then
XkPow = Application.Power(Application.Transpose(Xk), PolyArr)
Coeff = Application.LinEst(Yk, Application.Transpose(XkPow))
LeastSquarePoly = Coeff
End If
End If
End Function
I am totally puzzled by the behaviour of VBA application.linest. I am trying to create a function that is dynamic in respect of the order of a polynom that is fitted to a known set of data. That is, with a least square approximation trying to fit f(x)=a*x^i+b*x^(i-1)+...+constant term. Now, it works fine for polynoms up to quadratic order but then it implies that there is collinearity by consistently returning zeros for all orders lower than x^(i-1), except for the constant term. I can not figure out why, can you? The function below does not give me any errors or so otherwise...
Function LeastSquarePoly(Data() As Variant, Xindx As Integer, Yindx As Integer, Posindx As Integer, _
order As Integer, Period As Integer) As Variant
' Calculates the least square approximation with a polynom of order "order"
' Returns the coefficients to the polynom, or Empty if the period is longer than the
' target array of values or if any value is Empty
' The Calculation is done backwards from each data value, i.e. the actual position
' and the n-1 previous values of InArray
Dim FoundEmpty As Boolean
FoundEmpty = False
If Period > Posindx Then
LeastSquarePoly = Empty
Else
Dim Coeff, XkPow As Variant
Dim Xk(), Yk() As Variant
Dim PolyArr() As Variant
ReDim PolyArr(1 To order)
Dim i As Integer
For i = 1 To order
PolyArr(i) = i
Next
ReDim Xk(1 To Period)
ReDim Yk(1 To Period)
For i = 1 To Period
Xk(i) = CDbl(Data(Xindx, Posindx - Period + i))
Yk(i) = CDbl(Data(Yindx, Posindx - Period + i))
If Xk(i) = Empty Or Yk(i) = Empty Then
LeastSquarePoly = Empty
FoundEmpty = True
End If
Next
If Not FoundEmpty Then
XkPow = Application.Power(Application.Transpose(Xk), PolyArr)
Coeff = Application.LinEst(Yk, Application.Transpose(XkPow))
LeastSquarePoly = Coeff
End If
End If
End Function