Announcement

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

  • xtline - average line for group of panels

    Hi All,

    I am currently graphing some panel data. I would like to produce a graph with two lines, one corresponding to values for selected time series in the panel variable and the other line corresponding to the average values derived from the remaining values in the data set and/or average derived from all the panels. Broadly speaking, I am looking to produce chart similar to the attached primitive excel chart. I will be grateful for any help and/or suggestions.

    Attached Files
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    I've never used xtline, but skimming the help for it, why can you not generate a binary variable for the target data and all other data, and then use the overlay option? If you follow the example in the help file, you would get the following error, but you can collapse to means by the new variable, then xtset, and then xtline without problems:

    Code:
    . generate group = person > 1
    
    . xtset group day
    repeated time values within panel
    r(451);
    
    . collapse (mean) calories, by(group day)
    
    . xtset group day
           panel variable:  group (strongly balanced)
            time variable:  day, 01jan2002 to 31dec2002
                    delta:  1 day
    
    . xtline calories, overlay

    Attached Files

    Comment


    • #3

      Here is one way for a single time series:

      Code:
      webuse grunfeld,clear
      preserve
      tempfile tmp
      collapse mean = invest if com !=1, by(year) 
      save `tmp'
      restore
      merge m:1 year using `tmp' 
      line invest year if com == 1 || line mean year
      Scott

      Comment


      • #4
        Thanks very much for showing interest, very useful answers. Which approach would be easiest from the efficiency point of view? I have a data set of the following format:
        Geography IndicatorA2001 IndicatorA2002 IndicatorA2003 IndicatorA2004 IndicatorA2005 IndicatorB2001 IndicatorB2002 IndicatorB2003 ...
        Selected geography 1 2 3 4 5 6 7 8 9
        Remaining geography 2 10 11 12 13 14 14 14 14
        Remaining geography 3 56 65 6 5 6 3 6 4 ..
        Remaining geography 4 56 56 85 8 89 32 56 546 ...
        Ideally, I will be looking to create time series graphs for each set of the indicators. I would like to graph selected geography as a separate line for each of the graphs. Presently, I derived time series data set for one indicator, doing this for each separate indicator will be rather painful exercise.
        Last edited by Konrad Zdeb; 30 Apr 2014, 09:56. Reason: Typos.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          I think this is equivalent to Scott's code

          Code:
           
          webuse grunfeld,clear
          separate invest, by(com == 1) 
          collapse invest0 invest1, by(year) 
          label var invest0 "mean of others"
          label var invest1 "Company 1" 
          line invest? year

          Comment


          • #6
            You can make dummy variables for geography levels in one command with:

            tabulate geography, generate()

            Comment


            • #7
              Colleagues,

              Thank you very much for your help, greatly appreciated.
              Kind regards,
              Konrad
              Version: Stata/IC 13.1

              Comment


              • #8
                Thanks Nick. I had forgotten or never learned that -separate- allows expressions in the by() statement.
                Scott

                Comment


                • #9
                  Indeed. It may even have been the very first idea that separate should support separation on true-or-false expressions. I suspect I use it more for separating on a categorical variable, but we programmed both cases right at the beginning.

                  Comment

                  Working...
                  X