Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: adjacency matrix from neighbors for sppack


From   rraciborski@stata.com
To   statalist@hsphsun2.harvard.edu
Subject   st: adjacency matrix from neighbors for sppack
Date   Thu, 21 Apr 2011 14:42:03 -0500

László Sándor <sandorl@gmail.com> has a question about creating an adjacency
matrix using the user-written command -spmat-:

> I would like to use the -sppack- package for social network analysis....
> I have IDs of friends for each individual as an observation (variables:
> friend1-friend10). I would like to repeat basically this exercise, but
> for -spmat-: http://www.ats.ucla.edu/stat/stata/code/adj_matrix.htm

The data presented on the UCLA page is in what we call a 'neighbor list'
format, that is, each line lists a student ID followed by the IDs of friends.

As discussed in sections 13.4, 15, and 17.4 of the working paper László referred
to in his post, we can use -spmat import- with option -nlist- to create an
adjacency matrix from the above data.  The only requirement is that the friends
data are in a format that -spmat import- can understand.  The help file for
-spmat import- states that the data to be imported should be in a
space-delimited text file and that the option -nlist- 

    specifies that the text file is in the neighbor-list format.
    The first line of the file must contain the total number of
    places....  Each remaining line lists a place ID followed by
    its neighbors, if any.
   
Thus, we first save the data in the friends.txt file in the required format:

-------- begin friends.txt --------
10
44006 45611 55610 74158 55898
45611 44006 45623 45621 74158 55898
45621 71643 45611
45623 59642 71643 45611 73452 55610
55610 45623 45611 44006 55898 71643
55898 74158 55610 45621
59642 55898 71643 45621 45611 74158
71643 55898 73452 59642 45611 44006
73452 71643 45623
74158 55898 45611 59642 45621 44006
--------- end friends.txt ---------

Now we are ready to create an adjacency matrix using -spmat import-:

 . spmat import adj using friends.txt, nlist

We created a spatial-weighting object named 'adj' that contains the adjacency
matrix constructed from the data contained in the file friends.txt.  László can
verify that the resulting adjacency matrix is identical to the one presented on
the UCLA page by typing

 . spmat getmatrix adj W id
 . mata: id, W

--Rafal
rraciborski@stata.com
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index