Announcement

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

  • Connecting twins in a dataset using twin status and family ID

    I thought this would be rather simple, but I can't figure it out.

    I have a dataset that includes twin pairs. I want to make a twin variable (e.g. variable X is person i's value for Y, and variable X2 is the value of person i's twin for Y) so that I can do MZ and DZ twin correlations. I have family ID and twin status. I tried to do it by doing various versions of by X: replace twinvariable[_N]=_N-1, etc., but it isn't quite doing it. Is there some way to tell Stata, "look in this variable Y-defined group. If there are two cases in this group that have the same value for variable X, insert a value in variable X2 that consists of the value of the other pair case"?

    Many thanks

  • #2
    What about this?

    Code:
    clear
    * Create some data
    input family x
    1 2
    1 3
    2 1
    2 4
    end
    * Generate an identifier for twin within family
    bys family: gen twin=_n
    * Create twin variable
    gen x_twin=.
    * Replace by value of other twin within each family
    bys family: replace x_twin=x[2] if twin==1
    bys family: replace x_twin=x[1] if twin==2
    Jorge Eduardo Pérez Pérez
    www.jorgeperezperez.com

    Comment


    • #3
      This may help too:

      SJ-8-4 dm0043 . Tip 71: The problem of split identity, or how to group dyads
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/08 SJ 8(4):588--591 (no commands)
      tip on how to handle dyadic identifiers

      For example, within groups of 2, 3 - _n is always the index of the other observation. (If _n == 1, then 3 - _n == 2 and vice versa.)

      For that and other tiny tricks, the pdf is free to all:

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0043

      Last edited by Nick Cox; 12 Feb 2015, 08:35.

      Comment


      • #4
        Thanks! The code above worked once I dropped all the non-twin observations (then I re-merged the twin variables back in with the main dataset).

        Comment

        Working...
        X