Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: Re: st: Using -return matrix- to return other rclass matrix results

From   "Lacy,Michael" <>
To   "" <>
Subject   re: Re: st: Using -return matrix- to return other rclass matrix results
Date   Sat, 23 Feb 2013 03:01:44 +0000

Nick Cox <> had written:
>I would tend to go, within a program,
>tempname foo
>tab ..., matcell(`foo')
>return matrix foo = `foo'
>but that may not be your question.

You're right on target with the question, thanks.  Your approach would 
require that the -sub- program obtain the matrix name from the user, 
as -tab- does.  I have come to rely on and appreciate the convenience
of using r(), which saves the user the effort of supplying the matrix or
other parameter name to hold the return.  The advantage of the extra effort, 
though, is the more flexible use permitted by your approach.  This 
kind of call to a Stata subprogram make also makes subprogram calls more
like those in many other languages, which probably is a good thing.

I am purely curious, though, if anyone has a sense of  the construct
like -return matrix X = r(somematrix)- are not allowed, given that 
Stata has no problem with things like: -return scalar x = r(mean)-
I presume there is a good reason

Perhaps this prohibition has something to do with the fact that -return matrix-
"steals" the existing matrix.


Mike Lacy
Dept. of Sociology
Colorado State University
Fort Collins CO 80523

>On Fri, Feb 22, 2013 at 5:49 PM, Lacy,Michael
><> wrote:
>> Greetings,
>> I commonly want to return matrices in the midst of rclass programs, where such matrices come from
>> matrix results returned by other rclass programs.   What I usually do is clumsy, as it involves
>> -tempname- and various reassignments. Here's a toy illustration:
>> cap prog drop sub
>> prog sub, rclass
>>   matrix U = (runiform(), runiform())
>>   return matrix A = U
>>   return scalar extra = runiform()
>> end
>> //
>> cap prog drop main
>> prog main, rclass
>>   tempname temp1 temp2
>>   sub
>>   mat `temp1' = r(A)
>>   sub
>>   mat `temp2'  = r(A)
>>   // do lots of other stuff, including
>>   // e.g. CallSomeOtherProgram, matrix1(`temp1') matrix2(`temp2')
>>   //  do more stuff, and then at the end of main, return these and other results
>>   return matrix A1 = `temp1'
>>   return matrix A2 = `temp2'
>>   //return ... whatever ....
>> end
>> //
>> main
>> ret list
>> -----------------
>> If it were possible , I would do something in "main" like:
>> tab ..
>> return matrix A1 = r(A)
>> tab ...
>> return matrix A2 = r(A)
>> CallSomeOtherProgram ...,
>> which of course won't work, since -return matrix-, as I understand it,  doesn't allow
>> the direct assignment of matrices from r().
>> The following works and saves a bit of memory, but is equally prolix:
>> tempname temp1 temp2
>> sub
>> mat rename r(A) `temp1'
>> sub
>> mat rename r(A) `temp2'
>> ...etc.
>> return ... = `temp1'
>> return  etc.
>> Using -return add- is possiblem as in:
>> sub
>> return add
>> sub ...
>> return add
>> But this this would return any and all r(extra) material, which could be quiet a bit of
>> extraneous junk.
>> So, I'd 1) like to return matrices from r() as they are created without going
>> through the -tempname- and -mat rename- or assignment steps. and 2) to be able
>> to pass these matrices to another program before returning them.  It seems
>> that creating temp matrices for this purpose is clumsy. Is there another approach?
>> I would presume so, but ...
>*   For searches and help try:
>    References:
>        st: Using -return matrix- to return other rclass matrix results

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index