Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking whether edges between two nodes of a network are reciprocal or not

    Hello! I have data on individuals and their relationships with other individuals in a network. Each observation is a relationship between an individual and an other one. I would like to build a variable that equals 0 if there is no connection between A and B, 1 if there is exactly one connection between A and B (i.e., if A declared that they share a connection with B but not the other way around), and 2 if the relationship is reciprocal, i.e. both A and B declared that they share a relationship witht their pair. Please note that I have different networks so I would like to build this variable by group. Here is an example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3(source target) byte connection
    "D1" "D2" 1
    "D1" "D3" 0
    "D1" "D4" 1
    "D2" "D1" 1
    "D2" "D3" 1
    "D2" "D4" 0
    "D3" "D1" 1
    "D3" "D2" 1
    "D3" "D4" 1
    "D4" "D1" 0
    end
    Let's say I want to create a status_connection variable. I would want it to be 2 for the D1-D2 and the D2-D1 pairs because there is a connection that is shared in the edgelist. However, D3 declared to have a connection with D1 while D1 doesn't have any connection with D3. I would want the variable to be 1 for both those inverted pairs. And if there is no connection between a pair of individuals in both ways, the variable would take a value of 0.

    My intuition tells me the code should have something to do with summing the connection variable, yet I can't seem to figure it out. I would appreciate any help!




  • #2
    For those who might face the same problem, I found the appropriate code:

    Code:
    gen lettersite = substr(source, 1,1)
    gen sourcenum = real(substr(source, 2,.))
    gen targetnum = real(substr(target, 2,.))
    
    by site, sort: gen pair = lettersite + " " + string(min(sourcenum, targetnum)) + " " + string(max(sourcenum, targetnum))
    by pair, sort : egen recip = total(connection)

    Comment

    Working...
    X