Announcement

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

  • create Adjacency matrix nwcommands

    Hi,
    thank you very much for the incredible nwcommands and thank you very much in advance for your help.
    I'd like to create an Adjacency Matrix when my data is
    id idf1 idf2 idf3....
    50 45 39 23
    45 50 23
    39 50 23
    23 50 45 39
    That is, I have students that were asked to name other students that they considered to be friends. The dataset contains an id number for each case and also variables that contain the id numbers of the friends each student identified.

    How can I get the Adjacency Matrix by using nwcommands with stata for this kind of data?

  • #2
    I believe this does what you want: It takes data in your format, and uses it to make a named network recognized by -nwcommands-.
    Code:
    * Example data in Flavia's format.nwclear // Clear Stata and the -nwcommands- space.
    nwclear
    input int(id idf1 idf2 idf3)
    1 2 6 4
    2 4 3 1
    3 2 5 4
    4 5 1 3
    5 6 2 1
    6 4 1 2
    end
    list
    //
    // -nwcommands- definitely accepts data as edgelists, so let's reshape the data into such. Perhaps there is a way
    // to take data as supplied and use it directly in -nwcommands-, but I *know* an edgelist works.
    reshape long idf, i(id) j(j)
    drop j
    list // just to verify
    //
    // Just one -nwcommand- to do what Flavia wants, given an edgelist.
    nwset id idf, name(friendnet) edgelist directed
    //
    // Must be a nicer way to list a network matrix in -nwcommands- but
    // I don't know it. Note that the representation of a network
    // in -nwcommands- *is* an adjacency matrix in Mata.
    mata nw_mata1

    Comment


    • #3
      Thank you very much Mike for this useful and clear help! It works!

      Comment


      • #4
        Dear Mike,
        Thanks for the explanation.
        What if there is a zero or a missing value in one of the idfs? we have a missing value in one of the rows and we get a 1 at the principal diagonal of the adjacency matrix for this row. for example if the data is like:
        input int(id idf1 idf2 idf3)
        1 2 6 4
        2 4 3 1
        3 2 5 4
        4 5 1 3
        5 6 2 1
        6 4 1 .

        the adjacency matrix looks like this:

        1 2 3 4 5 6
        +-------------------------+
        1 | 0 1 0 1 0 1 |
        2 | 1 0 1 1 0 0 |
        3 | 0 1 0 1 1 0 |
        4 | 1 0 1 0 1 0 |
        5 | 1 1 0 0 0 1 |
        6 | 1 0 0 1 0 1 |
        +-------------------------+

        where the value of (6,6) is 1.
        Best,
        Sarah






        Originally posted by Mike Lacy View Post
        I believe this does what you want: It takes data in your format, and uses it to make a named network recognized by -nwcommands-.
        Code:
        * Example data in Flavia's format.nwclear // Clear Stata and the -nwcommands- space.
        nwclear
        input int(id idf1 idf2 idf3)
        1 2 6 4
        2 4 3 1
        3 2 5 4
        4 5 1 3
        5 6 2 1
        6 4 1 2
        end
        list
        //
        // -nwcommands- definitely accepts data as edgelists, so let's reshape the data into such. Perhaps there is a way
        // to take data as supplied and use it directly in -nwcommands-, but I *know* an edgelist works.
        reshape long idf, i(id) j(j)
        drop j
        list // just to verify
        //
        // Just one -nwcommand- to do what Flavia wants, given an edgelist.
        nwset id idf, name(friendnet) edgelist directed
        //
        // Must be a nicer way to list a network matrix in -nwcommands- but
        // I don't know it. Note that the representation of a network
        // in -nwcommands- *is* an adjacency matrix in Mata.
        mata nw_mata1

        Comment

        Working...
        X