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]

st: adjacency matrix from neighbors for sppack


From   [email protected]
To   [email protected]
Subject   st: adjacency matrix from neighbors for sppack
Date   Thu, 21 Apr 2011 14:42:03 -0500

László Sándor <[email protected]> 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
[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