Announcement

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

  • #16
    expanding the dataset is one common trick to include a total category, but you can also define a new variable if specifying the groups separately.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(caseid bmipct_y1 year anyuse)
    1 55 1 0
    1 57 2 0
    1 60 3 0
    2 57 1 1
    2 65 2 1
    2 80 3 1
    2 81 5 1
    2 81 6 1
    3 54 1 2
    3 40 3 2
    3 45 5 2
    3 50 7 2
    end
    
    bys year: egen bmipct_ymean= mean(bmipct_y1)
    set scheme s1color
    twoway line bmipct_ymean year, sort|| line bmipct_y1 year if anyuse==0, sort || line bmipct_y1 year if anyuse==1, sort || line bmipct_y1 year if anyuse==2, sort
    gr save gr1, replace
    preserve
    expand 2, g(new)
    replace bmipct_y1= bmipct_ymean if new
    *CODE TOTAL CATEGORY =3
    replace anyuse=3 if new
    twoway line bmipct_y1 year, sort by(anyuse)
    gr save gr2, replace
    gr combine gr1.gph gr2.gph
    restore
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.6 KB
ID:	1629789

    Comment


    • #17
      Thank you so much, this code worked.

      Comment


      • #18
        This is an old thread, but wanted to just comment and say I put together a slim wrapper for the -twoway line- command that allows you to plot aggregate values over time and by group: https://github.com/jdcols01/trendline

        The basic syntax for the command is:
        trendline yvar, time(time_var) [category(category_var) statistic(string)]

        And all other twoway line options work with it as well.

        Comment


        • #19
          Originally posted by Jared Colston View Post
          This is an old thread, but wanted to just comment and say I put together a slim wrapper for the -twoway line- command that allows you to plot aggregate values over time and by group: https://github.com/jdcols01/trendline
          You might want to consider creating a separate thread to explain this in detail, including clear instructions on how to install your command. Providing some examples would make it even more accessible and helpful. A good example of how to do this is https://www.statalist.org/forums/for...ailable-on-ssc.

          Comment


          • #20
            Jared Colston

            ​​​​​​​I naturally agree with Andrew Musau's point that as you have made your command public you might as well follow up with some advertising.

            There is a general issue here that isn't trivially answered. I have often posted contradictory material in the sense that in one place I post a new command and in the other I expand on how a few lines of basic Stata get you where you want to be without needing to install or learn or use such a new command.

            This is territory where something like

            Code:
            bysort group year : egen mean = mean(whatever) 
            separate mean, by(group), veryshortlabel 
            local means `r(varlist)' 
            egen tag = tag(group year) 
            line `means' year if tag, sort
            may get most of what you want. But there can be pain as well as pleasure in working that out and getting it right.

            Comment

            Working...
            X