S
Stacy35216
Windows XP, Excel 2003
So here's the problem: I have a VBA add-in that performs multiple
regression and I'm trying to add t-statistic calculations to it.
Therefore, I need to perform some matrix operations along the way.
Problem is, I keep getting Type Mismatch errors. The independent
variables are stored in an array (each to its own column) that is
declared as Double and therefore I would assume that manipulations of
this array would also be Double and could be declared as either Variant
or Double. There's some code below that defines a macro that duplicates
my problem.
If you declare XMatrix as variant, you throw the error at the ** line.
If you declare XMatrix and XTransposeMatrix as double, you throw the
error at the ** line. And if you declare XMatrix as double and
everything else as variant, you throw the error at the *** line. Go
ahead, play with it a little. It's fascinating and frustrating. I
can't seem to find the combination that will complete the calculations.
I've just been playing around with the variable types looking for a
solution because I can't reason what they should be. There must be
something I don't know. I also spent some time looking for a command
that would return the type of a variable, but couldn't find it, if
there is one. If anyone has any ideas, please help!
Thanks!
Sub Test()
Dim IndVariableValues(4, 3) As Double
Dim XMatrix() As Double
Dim XTransposeMatrix() As Variant
Dim XTransposeXMatrix() As Variant
Dim HatMatrix() As Variant
IndVariableValues(1, 1) = 1
IndVariableValues(1, 2) = 2
IndVariableValues(1, 3) = 3
IndVariableValues(2, 1) = 3
IndVariableValues(2, 2) = 2
IndVariableValues(2, 3) = 4
IndVariableValues(3, 1) = 2
IndVariableValues(3, 2) = 1
IndVariableValues(3, 3) = 5
IndVariableValues(4, 1) = 6
IndVariableValues(4, 2) = 3
IndVariableValues(4, 3) = 4
ReDim XMatrix(UBound(IndVariableValues, 1),
UBound(IndVariableValues, 2) + 1)
ReDim XTransposeMatrix(UBound(IndVariableValues, 2) + 1,
UBound(IndVariableValues, 1))
ReDim XTransposeXMatrix(UBound(IndVariableValues, 2) + 1,
UBound(IndVariableValues, 2) + 1)
For Index = 1 To UBound(IndVariableValues, 2)
XMatrix(Index, 1) = 1
Next Index
For Index = 1 To UBound(IndVariableValues, 1)
For Index2 = 2 To UBound(IndVariableValues, 2) + 1
XMatrix(Index, Index2) = IndVariableValues(Index,
Index2 - 1)
Next Index2
Next Index
XTransposeMatrix =
Application.WorksheetFunction.Transpose(XMatrix) '*
XTransposeXMatrix = Application.MMult(XTransposeMatrix,
XMatrix) '**
HatMatrix = Application.MInverse(XTransposeXMatrix)
'***
End Sub
So here's the problem: I have a VBA add-in that performs multiple
regression and I'm trying to add t-statistic calculations to it.
Therefore, I need to perform some matrix operations along the way.
Problem is, I keep getting Type Mismatch errors. The independent
variables are stored in an array (each to its own column) that is
declared as Double and therefore I would assume that manipulations of
this array would also be Double and could be declared as either Variant
or Double. There's some code below that defines a macro that duplicates
my problem.
If you declare XMatrix as variant, you throw the error at the ** line.
If you declare XMatrix and XTransposeMatrix as double, you throw the
error at the ** line. And if you declare XMatrix as double and
everything else as variant, you throw the error at the *** line. Go
ahead, play with it a little. It's fascinating and frustrating. I
can't seem to find the combination that will complete the calculations.
I've just been playing around with the variable types looking for a
solution because I can't reason what they should be. There must be
something I don't know. I also spent some time looking for a command
that would return the type of a variable, but couldn't find it, if
there is one. If anyone has any ideas, please help!
Thanks!
Sub Test()
Dim IndVariableValues(4, 3) As Double
Dim XMatrix() As Double
Dim XTransposeMatrix() As Variant
Dim XTransposeXMatrix() As Variant
Dim HatMatrix() As Variant
IndVariableValues(1, 1) = 1
IndVariableValues(1, 2) = 2
IndVariableValues(1, 3) = 3
IndVariableValues(2, 1) = 3
IndVariableValues(2, 2) = 2
IndVariableValues(2, 3) = 4
IndVariableValues(3, 1) = 2
IndVariableValues(3, 2) = 1
IndVariableValues(3, 3) = 5
IndVariableValues(4, 1) = 6
IndVariableValues(4, 2) = 3
IndVariableValues(4, 3) = 4
ReDim XMatrix(UBound(IndVariableValues, 1),
UBound(IndVariableValues, 2) + 1)
ReDim XTransposeMatrix(UBound(IndVariableValues, 2) + 1,
UBound(IndVariableValues, 1))
ReDim XTransposeXMatrix(UBound(IndVariableValues, 2) + 1,
UBound(IndVariableValues, 2) + 1)
For Index = 1 To UBound(IndVariableValues, 2)
XMatrix(Index, 1) = 1
Next Index
For Index = 1 To UBound(IndVariableValues, 1)
For Index2 = 2 To UBound(IndVariableValues, 2) + 1
XMatrix(Index, Index2) = IndVariableValues(Index,
Index2 - 1)
Next Index2
Next Index
XTransposeMatrix =
Application.WorksheetFunction.Transpose(XMatrix) '*
XTransposeXMatrix = Application.MMult(XTransposeMatrix,
XMatrix) '**
HatMatrix = Application.MInverse(XTransposeXMatrix)
'***
End Sub