I am attempting to replicate the proposed solution to creating an adjacency matrix provided by UCLA IDRE https://stats.idre.ucla.edu/stata/code/creating-an-adjacency-matrix/
However, my replication efforts have been unsuccessful. The line below
Code:
id = .id
Code:
clear all matrix drop _all mata: mata 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 mkmat friend*, mat(F) mkmat id, mat(id) scalar define n = _N gen fid = _n mata a = st_matrix("F") id = st_matrix("id") b = J(st_numscalar("n"), st_numscalar("n"), 0) 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 } } } } c = id, b id = .id c = id'c st_matrix("adj", c) end mat list adj
Thank you
0 Response to Issue creating adjacency matrix
Post a Comment