Announcement

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

  • Unexpected vertical lines after tw connected

    Dear all,

    I'm using Stata 17.0 for Windows.

    My dataset are long, with a unique Y value over X assessments:

    clear
    input double(pid moca) byte wave
    1 16 6
    1 4 12
    7 3 24
    8 21 0
    8 3 18
    8 22 24
    9 7 0
    15 16 18
    16 26 24
    17 15 0
    26 9 18
    32 20 12
    34 18 12
    36 9 0
    37 17 0
    38 3 24
    39 1 0
    40 20 12
    40 18 24
    41 23 0
    44 14 6
    44 12 12
    48 21 6
    49 16 6
    49 21 18
    51 13 0
    52 12 24
    53 26 0
    56 22 12
    56 24 18
    61 6 6
    65 10 18
    70 10 6
    71 14 6
    75 6 0
    75 6 6
    78 8 12
    83 14 0
    88 17 18
    91 10 12
    92 7 24
    93 14 0
    98 22 6
    104 4 12
    107 7 6
    108 25 0
    110 13 6
    116 9 0
    119 6 6
    119 6 24
    120 28 0
    129 28 24
    130 17 0
    130 .a 18
    131 18 0
    133 9 24
    134 11 0
    134 9 12
    140 5 6
    140 3 12
    140 2 18
    143 .r 6
    143 .r 12
    145 18 6
    147 .a 12
    154 14 12
    154 14 24
    155 23 0
    155 19 18
    155 21 24
    156 18 0
    157 5 6
    157 0 12
    157 0 18
    161 19 6
    161 18 24
    162 15 0
    162 14 12
    168 21 6
    171 .r 6
    172 6 18
    172 5 24
    173 22 0
    175 21 6
    176 13 18
    178 12 18
    178 8 24
    179 12 0
    179 2 24
    180 8 0
    184 4 6
    184 .r 18
    187 15 24
    188 26 0
    194 1 6
    195 15 12
    197 25 24
    198 29 0
    203 5 6
    203 1 18
    end


    I'm attempting to create a connected line graph based on twoway scatter, where each line describes an individual's moca over wave.

    Here are my commands:
    . sort pid wave
    . tw connected moca wave, msymbol(i)


    My graph output has unexpected vertical lines. Unexpected because each person only has one moca per wave and there are no duplicates. Somehow the connected command must be linking across pid.

    Click image for larger version

Name:	example.png
Views:	1
Size:	309.6 KB
ID:	1651348

    Or have I mis-specified the command?


  • #2
    Code:
    xtset pid wave
    xtline moca, overlay leg(off) scheme(s1mono)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	61.5 KB
ID:	1651357

    Comment


    • #3
      connect(L) can help here.

      Comment


      • #4
        Thank you very much both, great solution.
        For my own understanding, what's the underlying reason tw connect didn't work, even after sort?

        Comment


        • #5
          The twoway connect code in #1 knows nothing about the panel structure. and will willingly connect data points for different panels.

          Even with balanced panels and no missing values, you can get spurious connections, as seen in

          Code:
          . webuse grunfeld, clear
          
          . twoway line invest year
          With incomplete panels you can still get spurious connections unless your code prohibits it. Suppose panel 42 has values ending in 2008 and panel 43 has values starting in 2009....

          Ensuring that different panels are represented by different variables is one of various ways to avoid spurious connections.

          Comment


          • #6
            Fantastic answer, thank you. God bless Statalist and all who contribute!

            Comment

            Working...
            X