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   Kit Baum <[email protected]>
To   [email protected]
Subject   re: Re: st: Use of matrix values in generate statements
Date   Sat, 26 Mar 2011 21:40:55 -0400

<>
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.

Kit Baum
[email protected]



*
*   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