{"id":411,"date":"2012-07-19T09:00:39","date_gmt":"2012-07-19T16:00:39","guid":{"rendered":"http:\/\/www.rensecorten.dds.nl\/?p=411"},"modified":"2012-07-19T09:00:39","modified_gmt":"2012-07-19T16:00:39","slug":"from-adjacancy-matrix-to-edgelist-in-stata","status":"publish","type":"post","link":"http:\/\/www.rensecorten.org\/index.php\/2012\/07\/from-adjacancy-matrix-to-edgelist-in-stata\/","title":{"rendered":"From adjacancy matrix to edgelist in Stata"},"content":{"rendered":"<p style=\"text-align: justify;\">Every once in a while, people e-mail me with questions about <a title=\"Drawing social networks in Stata with Netplot\" href=\"http:\/\/www.rensecorten.org\/index.php\/2010\/04\/drawing-social-networks-in-stata-with-netplot\/\">netplot<\/a>, and probably the most frequently asked question is how to convert data from an adjacency matrix into an edgelist, which is the data format that netplot expects.\u00a0 I finally took the time to write some decent code to do it, and I might as well share it here. In principle it&#8217;s quite simple. If you want your data as a simple edgelist (or arclist, strictly speaking), a reshape command is almost all that you need. However, isolated nodes will not show up in the resulting edgelist. If you want to create an &#8220;extended edgelist&#8221; that includes also isolates nodes (for example because you want them to be drawn by netplot), things become slightly more complicated. In the code below, section #3 is dedicated to solving that problem; I you don&#8217;t want or need it, you can just skip that part. My feeling is that section #3 could be further simplified; suggestions are welcome!<\/p>\n<pre><code>\r\n\/\/ #1 Example dataset; random adjacency matrix with N=4; node 4 is isolated<\/code>\r\n\r\nclear\r\nset seed 223344\r\nset obs 4\r\n\r\n<code>forval x = 1(1)4{\r\ngen v`x' =(uniform()&gt;.5)\r\nreplace v`x'=0 if _n==`x'\r\nreplace v`x' = 0 if _n == 4\r\n}\r\nreplace v4 = 0\r\ngen n1 = _n<\/code>\r\n\r\n<code>list\r\n\r\n\/\/ #2 Transform into edgelist\r\n\r\nreshape long v, i(n1) j(n2)\r\n\r\n\/\/ #3 This section only to keep isolates in the data. Skip if you want a\r\n\/\/    regular edgelist without isolates.\r\n\r\ngen id_arc = _n\r\nbys n1: egen degree1 = total(v)\r\nbys n2: egen degree2 = total(v)\r\n\r\nsort n1\r\n\r\nreshape long n degree, i(id_arc) j(nseq)\r\n\r\nbys n: egen totaldegree = total(degree)\r\n\r\ngen isolated = totaldegree == 0\r\ndrop totaldegree\r\n\r\nreshape wide n degree isolated, i(id_arc) j(nseq)\r\n\r\nreplace n2 = . if isolated1 == 1\r\n\r\nbys n1: gen tag = _n==1\r\ndrop if isolated1 &amp; tag==0\r\ndrop isolated*\r\nkeep n1 n2 v\r\n\r\n\/\/ #4\r\n\r\nkeep if v==1 | n2==.\r\ndrop v\r\n\r\nlist \/\/ Tada!\r\n\r\nnetplot n1 n2,label arrows \/\/ draw the network!\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Every once in a while, people e-mail me with questions about netplot, and probably the most frequently asked question is how to convert data from an adjacency matrix into an edgelist, which&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,9,22],"tags":[],"_links":{"self":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/411"}],"collection":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":6,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"predecessor-version":[{"id":417,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/posts\/411\/revisions\/417"}],"wp:attachment":[{"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rensecorten.org\/index.php\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}