I am attempting to replicate a function for WLS (Weighted Least Squares) that I have found in a textbook in excel. There is a value error coming up and I think that I am doing something wrong in using the function.

The following is the VBA code for a supporting function Diag(w) and the function itself WLSregress():

Function Diag(W) As Variant
Dim n, i, j, k As Integer
Dim temp As Variant
n = W.Count
ReDim temp(n, n)
For i = 1 To n
    For j = 1 To n
        If j = i Then temp(i, j) = W(i) Else temp(i, j) = 0
    Next j
Next i
Diag = temp

End Function

Function WLSregress(y As Variant, X As Variant, W As Variant) As Variant
Wmat = Diag(W)
n = W.Count
Dim Xtrans, Xw, XwX, XwXinv, Xwy As Variant
Dim m1, m2, m3, m4 As Variant
Dim output() As Variant
Xtrans = Application.Tranpose(X)
Xw = Application.MMult(Xtrans, Wmat)
XwX = Application.MMult(Xw, X)
XwXinv = Application.MInverse(XwX)
Xwy = Application.MMult(Xw, y)
b = Application.MMult(XwXinv, Xwy)
k = Application.Count(b)
ReDim output(k) As Variant
    For bcnt = 1 To k
        output(bcnt) = b(bcnt, 1)
    Next bcnt
WLSregress = Application.Transpose(output)
End Function

This function should return the WLS estimator for the explanatory variables of equation being estimated. I understand the code leading up to the k = Application.Count(b) line but not too sure how the output bit is working.

If anyone could help me figure out why this isn't working I would be very grateful.

The following is an example image of the function trying to work. example

up vote 3 down vote accepted

By default, Excel will start sizing its arrays with 0 if you don't indicate otherwise. For example,

Redim arr(2,2)

will actually give you a 3 x 3 array

      0       1       2
0    blank | blank | blank
1    blank | blank | blank
2    blank | blank | blank

Because of this, when you have ReDim temp(n, n), you're actually creating an array with one more row and column than you actually want. In your example, you would expect the Dialog for A3:18 to be a 16 x 16 dialog, but it will actually create a 17 x 17 dialog, throwing off your matrix multiplications (i.e. Application.MMult)

Replace this line

ReDim temp(n, n)

With this line

ReDim temp(1 to n, 1 to n)

And you should now get results returned. Up to you to determine whether the result is accurate or not.

  • nice catch... however, it is still not working for some reason. I'll have to have another look. – 1212__Hello Nov 7 '13 at 23:45
  • Interesting; I tested it with your data sample and I was able to get results. Try changing all of the code from Application.Transpose to Application.WorksheetFunction.Transpose – Jaycal Nov 8 '13 at 1:09
  • Didn't seem to do anything..Could it be to do with how I am entering the function? I have entered it as a normal formula and as an array formula and didn't get any luck. – 1212__Hello Nov 8 '13 at 3:21
  • Working! Thank you so much. I think it was a combination of leaving out WorksheetFunction and the matrix multiplication. – 1212__Hello Nov 8 '13 at 3:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.