Announcement

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

  • Graph to show trends over time

    I'm sorry for what is probably a very simple question.

    I would like to graphically represent the trends in three different operations over time. My preference is for a line graph with year on the x-axis and frequency on the y-axis where each line represents a different operation. This feels as if it ought to be simple (it is in Excel!)

    I have tried "twoway (line procedure year)", which just produces a graph with hundreds of overlapping lines.

    I then discovered tsline but have been unable to successfully define my variables as a time series: "tsset id year, y" gives the error "repeated time values within panel". I know that some patients might be listed as undergoing the same operation in the same year (most likely they had one side operated and then the other...) and hadn't anticipated this as being an obstacle for drawing the graph.

    Is there an easier way to achieve this?

    David


    Click image for larger version

Name:	Screen Shot 2017-07-31 at 11.25.46.png
Views:	1
Size:	8.8 KB
ID:	1404466

  • #2
    Code:
    twoway (line var1 year) (line var2 year) (line var3 year)
    Please review section 12 in the FAQ for advice on sharing data excerpts and code.

    Comment


    • #3
      Code:
      line var1 var2 var3 year
      would also work for numeric variables. Whether it's what you want is unclear. Your code attempt featured a single response -procedure- but your data example showed three different responses. Also, the example did not show any of the complications you mention, so you might need more work before your graph can be drawn.
      Last edited by Nick Cox; 31 Jul 2017, 06:20.

      Comment


      • #4
        Thank you both - that was a useful lesson in describing the underlying structure of the data. Please now find a sample dataset below using dataex. I actually don't have the frequency counts suggested by the mock table that was in my opening post - I suppose that's what I envisage needing in order to create the graph. What I do have is a list of patients who've undergone an operation (1, 2, 3, 4, or 5) in a given year. I would like to plot the change in choice of operation over time.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte primarypatientprocedurecode float year
        1 2012
        4 2011
        1 2013
        3 2010
        1 2008
        4 2008
        4 2008
        5 2004
        1 2004
        1 2010
        1 2010
        4 2008
        3 2013
        2 2010
        4 2007
        2 2013
        1 2014
        3 2014
        3 2015
        2 2016
        end
        format %ty year

        Comment


        • #5
          Lots of ways to make progress. Here's one:

          Code:
          preserve
          contract primary year
          separate _freq, by(primary) veryshortlabel
          tsset primary year
          tsline _freq?, yla(0/3) legend(pos(3) col(1))
          restore

          Comment


          • #6
            Here is another option, inspired by Nick's code.
            Code:
            contract primarypatientprocedurecode year
            tsset primarypatientprocedurecode year
            tsline _freq, by(primarypatientprocedurecode) yscale(r(0))

            Comment


            • #7
              Hi there,

              Merry Christmas and Happy New Year

              I am new to STATA and I have population based prevalence rate (with 95% CI) for a disease for each year for 10 years (2005-2014) , what syntax in Stata I can use to assess whether there is a trend or not? Is it possible to make a graph for this trend with 95% CI?

              Best regards

              Comment

              Working...
              X