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: Re: st: Use of matrix values in generate statements


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: Re: st: Use of matrix values in generate statements
Date   Sun, 27 Mar 2011 10:49:55 +0100

I agree. My point was that Daniel was epsilon away from concise code,
even in Stata. He just needed the loop technique.

There are other trade-offs here. For example, the minutes spent
learning about loops in Stata will save you hours of coding time. The
hours spent learning Mata may add many pleasant days to your life as
it is such a satisfying language -- and, to pick up an underlying
theme in Daniel's post, one that is close to what many people will
know from work with other languages.

Nick

On Sun, Mar 27, 2011 at 2:40 AM, Kit Baum <[email protected]> wrote:
> <>
> Nick said
>
> I want to add a footnote. Here is a basic technique for using a lookup
> matrix to populate a variable z. I mix algebra with Stata. The idea is
> that variables x and y tell you which row and column of the matrix to
> use.
>
> matrix lookup = ...
> gen z = .
> forval i = 1/I {
>        forval j = 1/J {
>             quietly replace z = lookup[`i', `j'] if x == `i' & y == `j'
>        }
> }
>
> I am just using -forval- to turn Daniel's statements into a double
> loop over possibilities.
>
>
>
> Nick is of course quite correct in noting that Dan's frustration in translating logic into ado-file code can be handled very well this way by a double loop.
> For a relatively small problem (where there are not too many values in the lookup matrix, and where you're trying to apply the lookup
> to a sample of modest size) this will work just fine. However, it must enumerate all of the (in Dan's case, 144) possibilities and issue
> (in Dan's case, 144) -replace- statements for each possibility. Stata has an overhead, as Nick well knows, for -replace-, as Stata must keep track of how many changes are being made.
>
> The Mata logic which I presented is (horror!) a loop over observations, but the compiled code is so fast that I had to kick the sample size to
> 1 million before -rmsg- would register more than a fraction of one second to perform the loop. I haven't tried timing Nick's option (and to be fair I
> would have to generate more elaborate fake data to really test it) but I wager it will take more than 8 or 9 seconds to perform those 144
> replace commands on 1 million observations.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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