Announcement

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

  • Why line and tsline produce different results

    Maybe I'm an idiot for asking this, but why do these produce different results?
    Code:
    clear *
    u "http://fmwww.bc.edu/repec/bocode/s/scul_Reunification.dta", clear
    loc int_time = 1990
    cls
    qui xtset
    local lbl: value label `r(panelvar)'
    
    
    loc unit ="West Germany":`lbl'
    
    g treat = cond(`r(panelvar)'==`unit' & `r(timevar)' >=`int_time',1,0)
    
    cls
    
    twoway tsline gdp if code ~=7, connect(L) name(gr1, replace)
    
    twoway line gdp year if code ~=7, connect(L) name(gr2, replace)
    The connect options help file doesn't really specify why these would be different. It seems like they should be the same right?

  • #2
    -tsline- is not appropriate for use with panel data: it is for use with a single time series. The occurrence of multiple observations with the same value of the time variable is causing it to make a confusing plot of points in chronological order, but including all the different values of code. You need to use -xtline-, and to get a single panel with all of the lines on it, use the -overlay- option.

    Comment


    • #3
      Okay thank you!

      Comment


      • #4
        It's presumably deep in the code, but my interpretation is that tsline is just thinking in terms of a time variable and indifferent to panel structurre, whereas line is respectful of your sort order as defined by xtset.

        Comment


        • #5
          I was just asking myself a similar question. In general, it's not clear just what tsline returns when yvar comes form a panel. It would make sense to issue an error, or to require an option specifying how to collapse the multiple time series.
          I'm using StataNow/MP 18.5

          Comment


          • #6
            I disagree with the idea in post #5 of requiring an option to collapse the series. If you want to collapse the series, go ahead, naturally.

            I partially disagree with the main idea too. In general, StataCorp seems to dislike negative documentation, meaning warnings that if you want to do X, then this is really the wrong command for that, and I tend to agree. That is a long slippery slope and there are probably hundreds of help files that could be extended with warnings to look elsewhere if your problem is close to, but not quite what this command is for.

            At the same time it perhaps might help if there were a hit to the help file advising that you might need to look elsewhere if you want a panel data plot. Indeed, the "Also see" points to xtline, as it should.

            The issue is not black and white, as

            1. It's entirely possible to have just one panel in which case tsline behaves as I imagine people would expect. That might be unusual but it would be both legal and possible.

            2. Even more crucially, calling up tsline with a single panel should work fine.

            3. As important as #2 (just above, not post 2 in this thread) using the by() option of tsline gives a sensible result too. (Sensible in principle; if you have a hundred or a thousand panels. the result is not what you should want.)

            The problem is that if you do ask, implicitly or explicitly, for two or more panels at the same time, without specifying by(), then tsline can give puzzling output, and the question then is whether tsline should automatically give you the equivalent of xtline, overlay. I guess many people would prefer if tsline gave them the graph they presumably want, but there could be disagreement on that. For example, an error message with advice might be better than a puzzling result.

            The fact that Jared Greathouse was puzzled by what is going on shows that all this can bite even quite experienced users.

            Comment


            • #7
              My only additional query on this (I swear this approach would've saved me so much time upon submission of my paper!!) is how I might modify the colors of the lines such that they are the same color? In the paper where this dataset comes from, the authors looked at how the Reunification of West Germany with East Germany affected West German GDP. Hence, we may want to show, clearly, the trend line of West Germany compared to its donors.

              Code:
              clear *
              u "http://fmwww.bc.edu/repec/bocode/s/scul_Reunification.dta", clear
              loc int_time = 1990
              cls
              qui xtset
              local lbl: value label `r(panelvar)'
              
              
              loc unit ="West Germany":`lbl'
              
              g treat = cond(`r(panelvar)'==`unit' & `r(timevar)' >=`int_time',1,0)
              
              cls
              
              qui levelsof code, loc(units)
              
              loc a: word count `units'
              
              loc treat = `a'-1
              
              xtline gdp,  ///
                  overlay ///
                  addplot((connected gdp year if code ==`unit', ///
                      connect(L) lcolor(black) mcol(none) lwidth(thick))) ///
                  legend(order(1 "Donors" `treat' "West Germany") ring(0) pos(11)) ///
                  yti("GDP per Capita") ///
                  tline(`int_time')
              I like rainbows as much as the next guy, but what if I wanted all non-West German line to be grayscale 7 or whatever color I please and have them overlaid? Is this on the menu? When I try
              Code:
              xtline gdp, lcol(gs10)  ///
                  overlay ///
                  addplot((connected gdp year if code ==`unit', ///
                      connect(L) lcolor(black) mcol(none) lwidth(thick))) ///
                  legend(order(1 "Donors" `treat' "West Germany") ring(0) pos(11)) ///
                  yti("GDP per Capita") ///
                  tline(`int_time')
              Stata complains, saying lcol is not allowed. It does work if I do this,
              Code:
              xtline gdp, lcol(gs10)  ///
                  addplot((connected gdp year if code ==`unit', ///
                      connect(L) lcolor(black) mcol(none) lwidth(thick))) ///
                  legend(order(1 "Donors" `treat' "West Germany") ring(0) pos(11)) ///
                  yti("GDP per Capita") ///
                  tline(`int_time')
              or when I remove the overlay option from xtline. Might there be a way to have them overlaid and still, yet, retain control over the line colors with xtline?

              Comment


              • #8
                See fabplot from the Stata Journal which creates the kind of graph you want. The allowed -overlay()- options are

                overlaid_options Description
                ------------------------------------------------------------------------------------------------
                Main
                overlay overlay each panel on the same graph
                i(varname_i) use varname_i as the panel ID variable
                t(varname_t) use varname_t as the time variable

                Plots
                plot#opts(cline_options) affect rendition of the # panel line

                Add plots
                addplot(plot) add other plots to the generated graph

                Y axis, Time axis, Titles, Legend, Overall
                twoway_options any options other than by() documented in [G-3] twoway_options
                and the highlighted implies that you will need to specify options for each panel line separately.

                Code:
                clear *
                u "http://fmwww.bc.edu/repec/bocode/s/scul_Reunification.dta", clear
                loc int_time = 1990
                qui xtset
                local lbl: value label `r(panelvar)'
                
                
                loc unit ="West Germany":`lbl'
                
                g treat = cond(`r(panelvar)'==`unit' & `r(timevar)' >=`int_time',1,0)
                
                cls
                
                qui levelsof code, loc(units)
                loc a: word count `units'
                forval i=1/`a'{
                    local lcolors " `lcolors' plot`i'opts(lcol(gs10))"
                }
                loc treat = `a'-1
                **# Bookmark #1
                xtline gdp,  overlay ///
                    `lcolors' ///
                    addplot((connected gdp year if code ==`unit', ///
                        connect(L) lcolor(black) mcol(none) lwidth(thick))) ///
                    legend(order(1 "Donors" `treat' "West Germany") ring(0) pos(11)) ///
                    yti("GDP per Capita") ///
                    tline(`int_time')
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	132.7 KB
ID:	1689131

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  an error message with advice might be better than a puzzling result.
                  Not much disagreement then - the main point is, under certain conditions it is currently not fully clear what the output is, and there's room for disambiguating it. Yes the advice seems more in the spirit of minimizing 'negative documentation' than the required option.
                  I'm using StataNow/MP 18.5

                  Comment

                  Working...
                  X