Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

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


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Using -return matrix- to return other rclass matrix results
Date   Fri, 22 Feb 2013 18:06:08 +0000

I would tend to go, within a program,

tempname foo
tab ..., matcell(`foo')
return matrix foo = `foo'

but that may not be your question.

Nick

On Fri, Feb 22, 2013 at 5:49 PM, Lacy,Michael
<[email protected]> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index