Announcement

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

  • Adjacency matrix to row-wise dyad format

    Hello,

    I am trying to reformat a dataset that includes information on individuals' contacts know each other. My challenge is that most of the information I have found on this listserve about reshaping adjacency matrices relates to sociocentric data, where all of the individuals and all of their contacts are in one network. Here, I have egocentric data, so each person essentially has their own adjacency matrix. Currently, the data look like this:
    ID Alter Know2 Know2 Know4
    7000 1 No No No
    7000 2 . No No
    7000 3 . . No
    7000 4 . . .
    7001 1 Yes . .
    7001 2 . . .
    ID refers to respondents, alter refers to contacts that they name, and know* refers to whether the alter in that row knows the alter that corresponds to the know variable suffix (row 1, column 3: Does alter 1 know alter 2?). Of note, alter 1 for respondent 7000 is a different person from alter 1 of respondent 7001; those alters are just the contacts listed first by the two respondents.

    I am trying to create row-wise format that would look something like this:
    ID From To Know
    7000 7000_1 7000_2 No
    7000 7000_1 7000_3 No
    7000 7000_1 7000_4 No
    7000 7000_2 7000_3 No
    7000 7000_2 7000_4 No
    7000 7000_3 7000_4 No
    7001 7001_1 7001_2 Yes
    Here, the From and To columns refer to alters. The prefix of the value refers to the respondent and the suffix after the underscore refers to the alter number from the first table (7000_1 is respondent 7000's first alter).

    The second table is in "long" format, but so is the first, so I can't quite figure out what I'd have to do to reshape this.

    Any help would be tremendously appreciated.
    Thanks you!
    Robbie Dembo

  • #2
    Your desired result is a doubly long format. So reshape long is still the way to go
    Below is followed by some string manipulation to get your desired output

    Code:
    ren alter from
    reshape long know, i(id from) j(to)
    tostring id from to, replace
    replace from=id+"_"+from
    replace to=id+"_"+to

    Comment


    • #3
      Hi Jorrit,

      Thank you so much for your response. When I run the second line of the code, I am getting an error:

      reshape long know, i(id from) j(to)
      variable to contains all missing values

      I am wondering if you have any sense of what I may be doing wrong? I am happy to attach a small dataset example if it would help.

      Many thanks, again.
      Robbie

      Comment


      • #4
        Did you figure this out yet?
        If not, please do share a data example. Use dataex to create one. This helps give the forum readers more info on your data. See more on how and why in the FAQ: https://www.statalist.org/forums/help#stata

        Comment


        • #5
          Jorrit - thanks again for your help. For whatever reason, adding "string" in the reshape code seemed to do it:


          Code:
           
           ren alter from reshape long know, i(id from) j(to, string) tostring id from to, replace replace from=id+"_"+from replace to=id+"_"+to
          One other question I had is -- all of the identifiers in the "to" column now have an underscore suffix, e.g. 7000_10_, 7000_11_, etc. Is there a way to get rid of that last underscore for all of the values of the "to" variable? I have read Nick Cox's comments on the string commands (like this) but these seem to relate to changing variable names and not values.

          Thanks again,
          Robbie

          Comment


          • #6
            Code:
            replace to= substr(to,1,length(to)-1) if substr(to,-1,1)=="_"
            Again though, please do post examples of your dataset with dataex for every question. This makes it easier to answer any question, even a short one like this, where it is really clear what you want. Those examples are always useful and never superfluous.

            Comment


            • #7
              Thank you Jorrit, I will do that in the future.

              All best,
              Robbie

              Comment

              Working...
              X