Announcement

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

  • Reshape data in network mode

    Hi all,

    I have a database looking like this (mine is longer):

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3(afg usa fra ita esp zwe deu chn)
    "CHN" "FRA" "ITA" "USA" "ITA" "ESP" "ITA" "ZWE"
    end
    What I would like to do is to end up with an incidence matrix. Please notice that the countries as variable names are also those in the first row.
    So the first step would be to transfer the variable name to the first observation like this:

    Code:
    input str3(v1 v2 v3 v4 v5 v6 v7 v8)
    "afg" "usa" "fra" "ita" "esp" "zwe" "deu" "chn"
    "CHN" "FRA" "ITA" "USA" "ITA" "ESP" "ITA" "ZWE"
    end
    Then I would like to reshape this db into an incidence matrix. In the example above, the incidence would be an 8x8 matrix looking as follows:
    [CODE]
    AFG USA FRA ITA ESP ZWE DEU CHN
    AFG . . . . . . . 1
    USA . . 1 . . . . .
    FRA. . . . 1 . . . .
    ITA. . 1 . . . . . .
    ESP. . . . 1 . . . .
    ZWE . . . . 1 . . .
    DEU. . . . 1 . . . .
    CHN. . . . . . 1 . .

    [CODE]


    Thank you

  • #2
    Perhaps this.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3(afg usa fra ita esp zwe deu chn)
    "CHN" "FRA" "ITA" "USA" "ITA" "ESP" "ITA" "ZWE"
    end
    rename * c2*
    generate seq = _n
    reshape long c2, i(seq) j(c1) string
    replace seq = _n
    reshape wide c1, i(seq) j(c2) string
    rename c1* *
    drop seq
    list, clean
    Code:
    . list, clean
    
           CHN   ESP   FRA   ITA   USA   ZWE  
      1.   afg                                
      2.                                 chn  
      3.                     deu              
      4.                     esp              
      5.                     fra              
      6.                           ita        
      7.               usa                    
      8.         zwe
    Last edited by William Lisowski; 07 Sep 2022, 10:40.

    Comment


    • #3
      This might be closer to what you want.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str3(afg usa fra ita esp zwe deu chn)
      "CHN" "FRA" "ITA" "USA" "ITA" "ESP" "ITA" "ZWE"
      end
      rename *, upper
      unab ctry : *
      rename * c2*
      generate seq = _n
      reshape long c2, i(seq) j(c1) string
      list, clean
      replace seq = _n
      reshape wide c1, i(seq) j(c2) string
      rename c1* *
      drop seq
      list, clean
      unab vars : *
      local need : list ctry - vars
      foreach v of local need {
          generate `v' = ""
      }
      order `ctry'
      list, clean
      Code:
      . list, clean
      
             AFG   USA   FRA   ITA   ESP   ZWE   DEU   CHN  
        1.                                             AFG  
        2.                                 CHN              
        3.                     DEU                          
        4.                     ESP                          
        5.                     FRA                          
        6.         ITA                                      
        7.               USA                                
        8.                           ZWE
      Last edited by William Lisowski; 07 Sep 2022, 11:50.

      Comment


      • #4
        Here's another method and layout that Frederico might like. Note also the extensive community-contributed -nwcommands- package for social network analysis: -search nwcommands-. Among many other things, that package has several commands for managing network data in various layouts.
        Code:
        // vars to facilitate reshape
        gen int id = _n
        rename (afg-chn)  ego=   // I like to think with "ego" and "alter"
        //
        // All the edge pairs with a "tie" get a 1.
        reshape long ego, i(id) j(alter) string
        keep ego alter
        //
        // Fill in all the edge pairs that have no tie and give them a 0.
        fillin ego alter
        replace _fillin = !_fillin  
        rename _fillin tie
        //
        //  Make an "adjacency" or "incidence" matrix.
        reshape wide tie, i(ego) j(alter) string
        list
             +-----------------------------------------------------------------------------+
             | ego   tieafg   tiechn   tiedeu   tieesp   tiefra   tieita   tieusa   tiezwe |
             |-----------------------------------------------------------------------------|
          1. | CHN        1        0        0        0        0        0        0        0 |
          2. | ESP        0        0        0        0        0        0        0        1 |
          3. | FRA        0        0        0        0        0        0        1        0 |
          4. | ITA        0        0        1        1        1        0        0        0 |
          5. | USA        0        0        0        0        0        1        0        0 |
             |-----------------------------------------------------------------------------|
          6. | ZWE        0        1        0        0        0        0        0        0 |
             +-----------------------------------------------------------------------------+

        Comment


        • #5
          Hi all and thanks for the replies!!!

          Comment

          Working...
          X