### Stata Code Fragment:  Creating an Adjacency Matrix

The following code creates a adjacency matrix. For the example, students were asked to name  up to five other students in their class that they considered to be friends. We start with a dataset that contains an id number for each case, as well as five variables that contain the id numbers of the friends each student identified. The code produces a square matrix with id numbers as row and column headings. The rows are which student was nominating friends, the columns are for nominees. A one indicates that the student for that row named the student for that column as a friend.

clear
input id friend1 friend2 friend3 friend4 friend5
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

* reset matsize, whether this is necessary will depend on the size of
* the dataset and the version of Stata.
set matsize 800

* place variables to be in the resulting matrix in one matrix
* you will need to replace friend* with the appropriate variable names
mkmat friend*, mat(F)

* place the case id in another matrix
* you will need to replace id with the name of your id variable
mkmat id, mat(id)

* get number of rows in dataset
scalar define n = _N

* create fid, which is each case's row in the dataset, the data must remain
* (or be sorted) in the order defined by this fid
gen fid = _n

mata
* get matrices from Stata into Mata
a=st_matrix("F")
id = st_matrix("id")

* make an n by n matrix of zeros, where n is number of rows in dataset (cases)
b = J(st_numscalar("n"),st_numscalar("n"), 0)

* loop through the rows and columns of the matrix b, setting the value in a cell
* equal to 1 if the two cases are associated
for(i = 1; i <= rows(a); i++) {
for(j = 1; j<=cols(a); j++) {
for(k=1; k<=rows(id); k++) {
if (st_data(k,"id")==a[i,j]) {
b[i, k]=1
}
}
}
}

* combine the vector of id numbers with the matrix of zeros and ones
c = id,b
id = .\id
c = id'\c

* make this combined matrix accessible in Stata as "adj"
end

c1     c2     c3     c4     c5     c6     c7     c8     c9    c10    c11
r1      .  44006  45611  45621  45623  55610  55898  59642  71643  73452  74158
r2  44006      0      1      0      0      1      1      0      0      0      1
r3  45611      1      0      1      1      0      1      0      0      0      1
r4  45621      0      1      0      0      0      0      0      1      0      0
r5  45623      0      1      0      0      1      0      1      1      1      0
r6  55610      1      1      0      1      0      1      0      1      0      0
r7  55898      0      0      1      0      1      0      0      0      0      1
r8  59642      0      1      1      0      0      1      0      1      0      1
r9  71643      1      1      0      0      0      1      1      0      1      0
r10  73452      0      0      0      1      0      0      0      1      0      0
r11  74158      1      1      1      0      0      1      1      0      0      0

preserve
set obs =_N+1'

* you may want to change temp.txt to another file name, you may also
* want to add a file path
outsheet c* using temp.txt, replace nonames

restore

The file temp.txt will contain the following matrix. Note that the rows are occupied by cases (as they were in the original dataset), while the columns correspond to the id numbers listed in the variables friend1-friend5. The matrix is not necessarily symmetric.

	44006	45611	45621	45623	55610	55898	59642	71643	73452	74158
44006	0	1	0	0	1	1	0	0	0	1
45611	1	0	1	1	0	1	0	0	0	1
45621	0	1	0	0	0	0	0	1	0	0
45623	0	1	0	0	1	0	1	1	1	0
55610	1	1	0	1	0	1	0	1	0	0
55898	0	0	1	0	1	0	0	0	0	1
59642	0	1	1	0	0	1	0	1	0	1
71643	1	1	0	0	0	1	1	0	1	0
73452	0	0	0	1	0	0	0	1	0	0
74158	1	1	1	0	0	1	1	0	0	0`

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.