在Stata中查找社交网络组件

时间:2015-06-19 18:04:01

标签: grouping social-networking stata

[我从单独的帖子中复制了以下示例的一部分,并根据我的具体需要进行了更改]

pos_1    pos_2
  2        4
  2        5
  1        2
  3        9
  4        2
  9        3

上面的内容是因为person_2连接到person_4,...,person_4连接到person_2,person_9连接到person_3。

我想创建第三个分类[已编辑]变量组件,它可以让我知道观察到的链接是否是此网络中连接组件(子网)的一部分。在这种情况下,网络中有两个连接的组件:

pos_1    pos_2    component
  2        4        1
  2        5        1
  1        2        1
  3        9        2
  4        2        1
  9        3        2

组件1中的所有节点彼此连接,但不连接到组件2中的节点,反之亦然。有没有办法在Stata中生成这个组件变量?我知道有替代程序可以执行此操作,但如果我可以将其集成到Stata中,我的代码将更加无缝。

1 个答案:

答案 0 :(得分:3)

如果您reshape数据为长形式,您可以使用group_id(来自SSC)获取您想要的内容:

clear
input pos_1    pos_2
  2        4
  2        5
  1        2
  3        9
  4        2
  9        3
end

gen id = _n
reshape long pos_, i(id) j(n)

clonevar comp = id
list, sepby(comp)

group_id comp, match(pos)

reshape wide pos_, i(id) j(n)

egen component = group(comp)
list
相关问题