Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Lacy,Michael" <Michael.Lacy@colostate.edu> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Efficient way to create adjacency matrix in Mata |
Date | Thu, 30 Jun 2011 18:55:43 +0000 |
On Wed, Jun 29, 2011 at 10:01 PM, Benjamin Allaire wrote: > Question: what is the most efficient way to create an adjacency matrix in mata, > given a unique identifier ("id") and a club id ("club_id") for folks in the same club. > > So the data looks like this: > > id club_id > 1 1 > 2 1 > 3 2 > 4 2 > 5 2 > > Is there a clever way to do it without looping over the people? Here's another approach: clear // create example data set seed 12579 set obs 10 local nclub = 3 gen int id = _n gen club_id = 1 + trunc(`nclub' * runiform()) // // Restructure data so that observations are clubs, variables are member ids w/in clubs sort club_id by club_id: gen int nmem = _N if _n == 1 quiet { summ nmem forval i = 1/`=r(max)' { by club_id: gen mem`i' = id[`i'] if (_n ==1) } } // // Make some stuff nicer for Mata local nsubj = _N keep if !missing(nmem) keep nmem mem* // // Mata loops over all non-duplicate pairs within club mata mata clear M = J(0,0,.) st_view(M, ., .) adj = J(`nsubj', `nsubj', 0) for (nc = 1; nc <= rows(M); nc++) { // each club nmem = M[nc,1] for (i = 2; i <= nmem; i++) { // all non-duplicating pairs of members w/in club for (j = i + 1; j <= nmem + 1 ; j++) { ego = trunc(M[nc,i]) alter = trunc(M[nc,j]) adj[ego, alter] = 1 adj[alter,ego] =1 } } } end Regards, Mike Lacy Assoc. Prof./Dir. Grad. Studies Dept. of Sociology Colorado State University Fort Collins CO 80523-1784 970.491.6721 (voice) * * 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/