Runtime Error 1004 on Linest

July 27th, 2011 - 02:56 pm ET by marston.gould | Report spam
I'm trying to create a VBA macro that calculates the coefficients/T-stats and R-square for each variable with in a regression. The trick is that I have 10 candidate independent variables and I'd like to be able to test each combination of these variables.

I created some code that used fixed ranges for the Linest calc and that worked.
I got some help creating code that selected each unique combination.
But when I put these together - I get a runtime error 1004 on the linest.
I know that it has something to do with how I'm assigning the unique combination to a range that will be used in the Linest calculation.

I would appreciate any thoughts on how to correct this.

Regards - Marston

Here is the code below:

Option Base 1
Option Explicit
Sub combinKofN()
Dim rngs
Dim nRngs As Long, maxCombin As Long, nCombin As Long
Dim nSelect As Long, i As Long, j As Long
Dim r As String
Dim xRng As Range
Dim yRng As Range
Dim v
Dim k As Integer

rngs = Array("A2:A112", "B2:B112", "C2:C112", "D2:D112", "E2:E112", "F2:F112", "G2:G112", "H2:H112", "I2:I112", "J2:J112")
Set yRng = Range("K2:K112")
yRng.Select
nRngs = UBound(rngs)
k = 0
For nSelect = nRngs To 1 Step -1
maxCombin = WorksheetFunction.Combin(nRngs, nSelect)
ReDim idx(1 To nSelect) As Long
For i = 1 To nSelect: idx(i) = i: Next

nCombin = 0
Do
' generate next combination
nCombin = nCombin + 1
r = rngs(idx(1))
For i = 2 To nSelect
r = r & "," & rngs(idx(i))
Next
Set xRng = Range(r)
'visually make sure the correct range is being selected
'xRng = Range("A2:J112") ' this works when I utilize this instead
xRng.Select
v = Application.WorksheetFunction.LinEst(yRng, xRng, 0, True)
' ...coefficient
Range("M2").Offset(4 * k + 2, 0) = v(1, 1)
' ...T-stat
Range("M2").Offset(4 * k + 3, 0) = Abs(v(1, 1) / v(2, 1))
' ...R-squared
Range("M2").Offset(4 * k + 4, 0) = v(3, 1)
k = k + 1

If nCombin = maxCombin Then Exit Do

' next combination index
i = nSelect: j = 0
While idx(i) = nRngs - j
i = i - 1: j = j + 1
Wend
idx(i) = idx(i) + 1
For j = i + 1 To nSelect
idx(j) = idx(j - 1) + 1
Next
Loop
Next
email Follow the discussionReplies 7 repliesReplies Make a reply

Similar topics

Replies

#1 Cimjet
July 27th, 2011 - 03:06 pm ET | Report spam
Marston
Do you realize you have posted the same message 4 times.
Do you not see them?
They are all there, just wait for someone to answer.
Cimjet
wrote in message
news:
I'm trying to create a VBA macro that calculates the coefficients/T-stats and
R-square for each variable with in a regression. The trick is that I have 10
candidate independent variables and I'd like to be able to test each
combination of these variables.

I created some code that used fixed ranges for the Linest calc and that
worked.
I got some help creating code that selected each unique combination.
But when I put these together - I get a runtime error 1004 on the linest.
I know that it has something to do with how I'm assigning the unique
combination to a range that will be used in the Linest calculation.

I would appreciate any thoughts on how to correct this.

Regards - Marston

Here is the code below:

Option Base 1
Option Explicit
Sub combinKofN()
Dim rngs
Dim nRngs As Long, maxCombin As Long, nCombin As Long
Dim nSelect As Long, i As Long, j As Long
Dim r As String
Dim xRng As Range
Dim yRng As Range
Dim v
Dim k As Integer

rngs = Array("A2:A112", "B2:B112", "C2:C112", "D2:D112", "E2:E112", "F2:F112",
"G2:G112", "H2:H112", "I2:I112", "J2:J112")
Set yRng = Range("K2:K112")
yRng.Select
nRngs = UBound(rngs)
k = 0
For nSelect = nRngs To 1 Step -1
maxCombin = WorksheetFunction.Combin(nRngs, nSelect)
ReDim idx(1 To nSelect) As Long
For i = 1 To nSelect: idx(i) = i: Next

nCombin = 0
Do
' generate next combination
nCombin = nCombin + 1
r = rngs(idx(1))
For i = 2 To nSelect
r = r & "," & rngs(idx(i))
Next
Set xRng = Range(r)
'visually make sure the correct range is being selected
'xRng = Range("A2:J112") ' this works when I utilize this instead
xRng.Select
v = Application.WorksheetFunction.LinEst(yRng, xRng, 0, True)
' ...coefficient
Range("M2").Offset(4 * k + 2, 0) = v(1, 1)
' ...T-stat
Range("M2").Offset(4 * k + 3, 0) = Abs(v(1, 1) / v(2, 1))
' ...R-squared
Range("M2").Offset(4 * k + 4, 0) = v(3, 1)
k = k + 1

If nCombin = maxCombin Then Exit Do

' next combination index
i = nSelect: j = 0
While idx(i) = nRngs - j
i = i - 1: j = j + 1
Wend
idx(i) = idx(i) + 1
For j = i + 1 To nSelect
idx(j) = idx(j - 1) + 1
Next
Loop
Next
Replies Reply to this message
#2 joeu2004
July 27th, 2011 - 04:34 pm ET | Report spam
See the response in m.p.e.newusers.

And please do not multi-post, especially across newsgroups.

You appear to be using Google Groups. Sometimes it is slow in displaying
new posts.
Replies Reply to this message
#3 joeu2004
July 27th, 2011 - 07:37 pm ET | Report spam
"Cimjet" wrote:
Marston
Do you realize you have posted the same message 4 times.
Do you not see them?



FYI, no, he doesn't.

It appears that Google Groups is having its problems again. None of
Marston's follow-up postings to this thread appear in GG, nor do any his
"duplicate" postings about the same problem in m.p.e.programming.

It is such problems that cause me recently to abandon GG. Instead, I use
the newserver news.eternal-september.org, which you can register to use for
free at http://eternal-september.org. Then I set up Outlook Express as a
newsreader on my computer.

news.eternal-september.org has been very reliable for the short time that I
have been using it, about one month.

(Of course, Marston will not see this until the GG problem is cleared up.)
Replies Reply to this message
#4 Cimjet
July 27th, 2011 - 08:59 pm ET | Report spam
Hi Joe
I also use Eternal-September, never had problem with it.
I've been using it since Microsoft closed there NG server.
It looks like GG is following in MS footsteps.
Best regards
Cimjet

"joeu2004" wrote in message
news:j0q7gc$2a4$
"Cimjet" wrote:
Marston
Do you realize you have posted the same message 4 times.
Do you not see them?



FYI, no, he doesn't.

It appears that Google Groups is having its problems again. None of Marston's
follow-up postings to this thread appear in GG, nor do any his "duplicate"
postings about the same problem in m.p.e.programming.

It is such problems that cause me recently to abandon GG. Instead, I use the
newserver news.eternal-september.org, which you can register to use for free
at http://eternal-september.org. Then I set up Outlook Express as a
newsreader on my computer.

news.eternal-september.org has been very reliable for the short time that I
have been using it, about one month.

(Of course, Marston will not see this until the GG problem is cleared up.)

Replies Reply to this message
#5 marston.gould
July 29th, 2011 - 11:46 am ET | Report spam
Hi -

I tried the suggested changes -

Still having a few difficulties.
First off I notice when I use

Dim v As Long - I get a compile error. I think this is because v is an array not a long. When I leave blank or use Dim v as Variant it appears to compile.

In either of the situations above, when I run with the suggested changes - I still get the Runtime Error.

When I watch what is happening in debug - a couple of things I notice.
First off - the first pass through - I see the xRng(0,i-1) = Range(rngs(idx(i)) being loaded with each of the column ranges.

However - when I throw in a xRng.Select before the v = Application.
it only selects the rows in column X. Additionally, it does not appear that the values from columns A-J get loaded into X-AH as I think is the intention.

I tried to add

Range("X2").Resize(nRows).Offset(0,i-1) = Range(rngs(idx(i))) in the For/Next loop that is For n = 1 to nSelect just before v calculation - but that doesn't seem to set the values either. I also tried a couple of other manipulations of loading the range into an array and then moving that array to a different set of columns - but that's not working.

I also think - should the xRng be set to something like

Range("X2").Resize(nRows, nSelect) ?

Thanks - advance -

And only posting once!
Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search