Announcement

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

  • Problem with the graph of xtline when using "i." command

    Hi all,

    I encountered some problems in graphing xtline diagrams. Here is my code:
    Code:
    preserve
    collapse (mean)coverage, by (high planning finance performance year)
    xtset year
    xtline coverage , t(year) i(finance) overlay
    NB: coverage is the y variable. finance is a category variable that includes 4 categories (finance has level 1.2.3.4, which is shown as line finance =1, finance =2...). The diagram will be like:

    I am so confused why the diagram is like this, and only the line of finance level 4 looks normal.

    Thanks a lot!

    Click image for larger version

Name:	Screen Shot 2021-04-03 at 2.28.05 pm.png
Views:	1
Size:	488.5 KB
ID:	1601302

  • #2
    You evidently have in your data set the variables high planning and performance and if the mean of coverage varies with those variables as well as with finance and year -- which seems inevitable in practice -- then what you asked for implies joining up different values for the covariates explicit on the graph.. Hence there are up and down connections as well as left to right connections.

    As you don't want this graph, the question is what do you want to plot? The number of lines possible is the number of distinct combinations of four variables, it seems.

    Comment


    • #3
      xtline coverage, t(year) i(finance) overlay
      is equivalent to

      Code:
      xtset finance year
      xtline coverage, overlay

      The graph is consistent with multiple values of coverage in a particular year (exhibited by the vertical lines connecting points within the same year). I suspect that you are getting a repeated time values error after -xtset finance year-. Bottom line is that xtline is meant for panel data, so you need to somehow create a panel dataset from your variables (unique combinations of finance and year).
      Last edited by Andrew Musau; 03 Apr 2021, 10:30.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        You evidently have in your data set the variables high planning and performance and if the mean of coverage varies with those variables as well as with finance and year -- which seems inevitable in practice -- then what you asked for implies joining up different values for the covariates explicit on the graph.. Hence there are up and down connections as well as left to right connections.

        As you don't want this graph, the question is what do you want to plot? The number of lines possible is the number of distinct combinations of four variables, it seems.
        Hi Nick,

        Thanks a lot for your reply. What I try to plot is how the coverage rate of different levels (level 1.2.3.4) of assurance indicators (finance / performance / planning) changes over the years.

        Comment


        • #5
          Originally posted by Andrew Musau View Post

          is equivalent to

          Code:
          xtset finance year
          xtline coverage, overlay

          The graph is consistent with multiple values of coverage in a particular year (exhibited by the vertical lines connecting points within the same year). I suspect that you are getting a repeated time values error after -xtset finance year-. Bottom line is that xtline is meant for panel data, so you need to somehow create a panel dataset from your variables (unique combinations of finance and year).
          Hi Andrew,

          yes, you are correct, I have repeated time error after xtset. but it seems inevitable in my dataset, see data below (collapse):

          Here, finance 1 are appeared four times, because 4 medical organizations are rated as finance level 1, and their coverage rates in vaccines are exactly varied. So now I don't know how to avoid it.

          year finance performance planning coverage
          2005 1 1 2 96.4767
          2005 1 3 1 96.775
          2005 1 3 2 92.0667
          2005 1 2 2 88.785
          2005 2 3 3 90.73
          2005 2 2 2 86.0205
          2005 2 2 3 92.9025
          2005 2 3 2 83.68
          2005 3 1 1 88.8367
          2005 3 2 3 93.0593
          2005 3 2 2 86.936
          2005 3 3 2 93.2767
          2005 3 3 3 90.0925
          2005 4 3 4 94.38
          2006 1 1 2 96
          2006 1 3 1 97.5
          2006 1 3 2 94.6667
          2006 1 2 2 87.25
          2006 2 3 2 87
          2006 2 3 3 96
          2006 2 2 2 90.1278
          2006 2 2 3 90.25
          2006 3 2 3 95.0533
          2006 3 2 2 88
          2006 3 3 3 92.025
          2006 3 1 1 90.6667
          2006 3 3 2 94.6667
          2006 4 3 4 94
          2007 1 2 2 87.5
          2007 1 3 2 93
          2007 1 3 1 97.5
          2007 1 1 2 96
          2007 2 2 3 92.75
          2007 2 3 3 94
          2007 2 2 2 88.458
          2007 2 3 2 97
          2007 3 3 2 95.3333
          2007 3 2 3 92.5333
          2007 3 1 1 93
          2007 3 2 2 87.4
          2007 3 3 3 92.05
          2007 4 3 4 95
          2008 1 2 2 87.225
          2008 1 3 1 98
          2008 1 3 2 93.3333
          2008 1 1 2 98
          2008 2 3 2 94
          2008 2 3 3 93.3333
          2008 2 2 2 87.9954
          2008 2 2 3 90.75
          2008 3 3 2 94.6667
          2008 3 2 3 93.1333
          2008 3 3 3 92.2562
          2008 3 2 2 85.2
          2008 3 1 1 91
          2008 4 3 4 95
          2009 1 1 2 97.3667
          2009 1 3 1 96.1
          2009 1 3 2 94.7
          2009 1 2 2 88.55
          2009 2 2 2 87.748
          2009 2 3 3 94.1333
          2009 2 3 2 95.9
          2009 2 2 3 91.35
          2009 3 3 3 92.525
          2009 3 1 1 94.7
          2009 3 2 3 93.2733
          2009 3 2 2 85.62
          2009 3 3 2 95.7667
          2009 4 3 4 95.1
          2010 1 3 2 94.1333
          2010 1 2 2 90.75
          2010 1 1 2 98
          2010 1 3 1 97.1
          2010 2 2 2 90.4312
          2010 2 2 3 94.3
          2010 2 3 3 96.0667
          2010 2 3 2 93.6
          2010 3 2 2 86.76
          2010 3 3 2 96.5
          2010 3 3 3 94.2813
          2010 3 2 3 95.2467
          2010 3 1 1 95
          2010 4 3 4 97.1
          2011 1 3 2 95.3333
          2011 1 1 2 98.1667
          2011 1 2 2 92.875
          2011 1 3 1 98.6
          2011 2 2 2 91.2602
          2011 2 3 2 95.2
          2011 2 2 3 94.775
          2011 2 3 3 95.3667
          2011 3 2 2 93.32
          2011 3 2 3 95.8333
          2011 3 3 3 94.7375
          2011 3 3 2 97.1
          2011 3 1 1 94.4333
          2011 4 3 4 98.6
          2012 1 3 1 98.3
          2012 1 1 2 98.3
          2012 1 2 2 94.05
          2012 1 3 2 95
          2012 2 3 3 95.0333
          2012 2 2 2 93.2146
          2012 2 2 3 96.2
          2012 2 3 2 95.9
          2012 3 2 3 96.4267
          2012 3 3 3 95.3375
          2012 3 2 2 92.66
          2012 3 3 2 96.9
          2012 3 1 1 95.3
          2012 4 3 4 98.3
          2013 1 3 1 98.85
          2013 1 1 2 98.4667
          2013 1 2 2 93.3
          2013 1 3 2 97.2
          2013 2 3 3 97.6
          2013 2 2 2 94.1154
          2013 2 2 3 96.15
          2013 2 3 2 96.8
          2013 3 3 3 96.1312
          2013 3 2 3 96.4467
          2013 3 3 2 98.0333
          2013 3 2 2 92.24
          2013 3 1 1 95.3333
          2013 4 3 4 99

          Comment


          • #6
            It depends on what you want to show. If this is average coverage rate by finance level, then

            Code:
            preserve
            collapse coverage, by(finance year)
            xtset finance year
            xtline coverage, overlay
            restore

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              It depends on what you want to show. If this is average coverage rate by finance level, then

              Code:
              preserve
              collapse coverage, by(finance year)
              xtset finance year
              xtline coverage, overlay
              restore
              Hi Andrew,

              It works perfectly!!

              Thanks so much for your help!

              Comment


              • #8
                Let's be clear. The route taken here -- preserve collapse xtset xtline restore -- can be got to work, but it is not essential.

                This code is optimised for the data example. The full dataset evidently covers more years. I put in some work to kill the key (lose the legend) and label directly.


                Code:
                clear 
                input year finance performance planning coverage
                2005 1 1 2 96.4767
                2005 1 3 1 96.775
                2005 1 3 2 92.0667
                2005 1 2 2 88.785
                2005 2 3 3 90.73
                2005 2 2 2 86.0205
                2005 2 2 3 92.9025
                2005 2 3 2 83.68
                2005 3 1 1 88.8367
                2005 3 2 3 93.0593
                2005 3 2 2 86.936
                2005 3 3 2 93.2767
                2005 3 3 3 90.0925
                2005 4 3 4 94.38
                2006 1 1 2 96
                2006 1 3 1 97.5
                2006 1 3 2 94.6667
                2006 1 2 2 87.25
                2006 2 3 2 87
                2006 2 3 3 96
                2006 2 2 2 90.1278
                2006 2 2 3 90.25
                2006 3 2 3 95.0533
                2006 3 2 2 88
                2006 3 3 3 92.025
                2006 3 1 1 90.6667
                2006 3 3 2 94.6667
                2006 4 3 4 94
                2007 1 2 2 87.5
                2007 1 3 2 93
                2007 1 3 1 97.5
                2007 1 1 2 96
                2007 2 2 3 92.75
                2007 2 3 3 94
                2007 2 2 2 88.458
                2007 2 3 2 97
                2007 3 3 2 95.3333
                2007 3 2 3 92.5333
                2007 3 1 1 93
                2007 3 2 2 87.4
                2007 3 3 3 92.05
                2007 4 3 4 95
                2008 1 2 2 87.225
                2008 1 3 1 98
                2008 1 3 2 93.3333
                2008 1 1 2 98
                2008 2 3 2 94
                2008 2 3 3 93.3333
                2008 2 2 2 87.9954
                2008 2 2 3 90.75
                2008 3 3 2 94.6667
                2008 3 2 3 93.1333
                2008 3 3 3 92.2562
                2008 3 2 2 85.2
                2008 3 1 1 91
                2008 4 3 4 95
                2009 1 1 2 97.3667
                2009 1 3 1 96.1
                2009 1 3 2 94.7
                2009 1 2 2 88.55
                2009 2 2 2 87.748
                2009 2 3 3 94.1333
                2009 2 3 2 95.9
                2009 2 2 3 91.35
                2009 3 3 3 92.525
                2009 3 1 1 94.7
                2009 3 2 3 93.2733
                2009 3 2 2 85.62
                2009 3 3 2 95.7667
                2009 4 3 4 95.1
                2010 1 3 2 94.1333
                2010 1 2 2 90.75
                2010 1 1 2 98
                2010 1 3 1 97.1
                2010 2 2 2 90.4312
                2010 2 2 3 94.3
                2010 2 3 3 96.0667
                2010 2 3 2 93.6
                2010 3 2 2 86.76
                2010 3 3 2 96.5
                2010 3 3 3 94.2813
                2010 3 2 3 95.2467
                2010 3 1 1 95
                2010 4 3 4 97.1
                2011 1 3 2 95.3333
                2011 1 1 2 98.1667
                2011 1 2 2 92.875
                2011 1 3 1 98.6
                2011 2 2 2 91.2602
                2011 2 3 2 95.2
                2011 2 2 3 94.775
                2011 2 3 3 95.3667
                2011 3 2 2 93.32
                2011 3 2 3 95.8333
                2011 3 3 3 94.7375
                2011 3 3 2 97.1
                2011 3 1 1 94.4333
                2011 4 3 4 98.6
                2012 1 3 1 98.3
                2012 1 1 2 98.3
                2012 1 2 2 94.05
                2012 1 3 2 95
                2012 2 3 3 95.0333
                2012 2 2 2 93.2146
                2012 2 2 3 96.2
                2012 2 3 2 95.9
                2012 3 2 3 96.4267
                2012 3 3 3 95.3375
                2012 3 2 2 92.66
                2012 3 3 2 96.9
                2012 3 1 1 95.3
                2012 4 3 4 98.3
                2013 1 3 1 98.85
                2013 1 1 2 98.4667
                2013 1 2 2 93.3
                2013 1 3 2 97.2
                2013 2 3 3 97.6
                2013 2 2 2 94.1154
                2013 2 2 3 96.15
                2013 2 3 2 96.8
                2013 3 3 3 96.1312
                2013 3 2 3 96.4467
                2013 3 3 2 98.0333
                2013 3 2 2 92.24
                2013 3 1 1 95.3333
                2013 4 3 4 99
                end 
                
                egen mean = mean(coverage), by(finance year)
                egen tag = tag(finance year)
                separate mean, by(finance) 
                
                * ssc install mycolours  needed 
                mycolours  
                
                forval g = 1/4 { 
                    local extra `extra' || scatter mean`g' year if year == 2013, ms(none) mla(finance) mlabc("`OK`g''") mlabsize(medium)
                }
                
                set scheme s1color 
                line mean? year if tag, sort xla(2005(2)2013) lc("`OK1'" "`OK2'" "`OK3'" "`OK4'") ///
                yla(88(2)98, ang(h)) xtitle("") legend(off) `extra' xsc(r(. 2013.2)) ytitle(Mean coverage by finance type)
                Click image for larger version

Name:	coverage.png
Views:	1
Size:	37.5 KB
ID:	1601441

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  Let's be clear. The route taken here -- preserve collapse xtset xtline restore -- can be got to work, but it is not essential.

                  This code is optimised for the data example. The full dataset evidently covers more years. I put in some work to kill the key (lose the legend) and label directly.


                  Code:
                  clear
                  input year finance performance planning coverage
                  2005 1 1 2 96.4767
                  2005 1 3 1 96.775
                  2005 1 3 2 92.0667
                  2005 1 2 2 88.785
                  2005 2 3 3 90.73
                  2005 2 2 2 86.0205
                  2005 2 2 3 92.9025
                  2005 2 3 2 83.68
                  2005 3 1 1 88.8367
                  2005 3 2 3 93.0593
                  2005 3 2 2 86.936
                  2005 3 3 2 93.2767
                  2005 3 3 3 90.0925
                  2005 4 3 4 94.38
                  2006 1 1 2 96
                  2006 1 3 1 97.5
                  2006 1 3 2 94.6667
                  2006 1 2 2 87.25
                  2006 2 3 2 87
                  2006 2 3 3 96
                  2006 2 2 2 90.1278
                  2006 2 2 3 90.25
                  2006 3 2 3 95.0533
                  2006 3 2 2 88
                  2006 3 3 3 92.025
                  2006 3 1 1 90.6667
                  2006 3 3 2 94.6667
                  2006 4 3 4 94
                  2007 1 2 2 87.5
                  2007 1 3 2 93
                  2007 1 3 1 97.5
                  2007 1 1 2 96
                  2007 2 2 3 92.75
                  2007 2 3 3 94
                  2007 2 2 2 88.458
                  2007 2 3 2 97
                  2007 3 3 2 95.3333
                  2007 3 2 3 92.5333
                  2007 3 1 1 93
                  2007 3 2 2 87.4
                  2007 3 3 3 92.05
                  2007 4 3 4 95
                  2008 1 2 2 87.225
                  2008 1 3 1 98
                  2008 1 3 2 93.3333
                  2008 1 1 2 98
                  2008 2 3 2 94
                  2008 2 3 3 93.3333
                  2008 2 2 2 87.9954
                  2008 2 2 3 90.75
                  2008 3 3 2 94.6667
                  2008 3 2 3 93.1333
                  2008 3 3 3 92.2562
                  2008 3 2 2 85.2
                  2008 3 1 1 91
                  2008 4 3 4 95
                  2009 1 1 2 97.3667
                  2009 1 3 1 96.1
                  2009 1 3 2 94.7
                  2009 1 2 2 88.55
                  2009 2 2 2 87.748
                  2009 2 3 3 94.1333
                  2009 2 3 2 95.9
                  2009 2 2 3 91.35
                  2009 3 3 3 92.525
                  2009 3 1 1 94.7
                  2009 3 2 3 93.2733
                  2009 3 2 2 85.62
                  2009 3 3 2 95.7667
                  2009 4 3 4 95.1
                  2010 1 3 2 94.1333
                  2010 1 2 2 90.75
                  2010 1 1 2 98
                  2010 1 3 1 97.1
                  2010 2 2 2 90.4312
                  2010 2 2 3 94.3
                  2010 2 3 3 96.0667
                  2010 2 3 2 93.6
                  2010 3 2 2 86.76
                  2010 3 3 2 96.5
                  2010 3 3 3 94.2813
                  2010 3 2 3 95.2467
                  2010 3 1 1 95
                  2010 4 3 4 97.1
                  2011 1 3 2 95.3333
                  2011 1 1 2 98.1667
                  2011 1 2 2 92.875
                  2011 1 3 1 98.6
                  2011 2 2 2 91.2602
                  2011 2 3 2 95.2
                  2011 2 2 3 94.775
                  2011 2 3 3 95.3667
                  2011 3 2 2 93.32
                  2011 3 2 3 95.8333
                  2011 3 3 3 94.7375
                  2011 3 3 2 97.1
                  2011 3 1 1 94.4333
                  2011 4 3 4 98.6
                  2012 1 3 1 98.3
                  2012 1 1 2 98.3
                  2012 1 2 2 94.05
                  2012 1 3 2 95
                  2012 2 3 3 95.0333
                  2012 2 2 2 93.2146
                  2012 2 2 3 96.2
                  2012 2 3 2 95.9
                  2012 3 2 3 96.4267
                  2012 3 3 3 95.3375
                  2012 3 2 2 92.66
                  2012 3 3 2 96.9
                  2012 3 1 1 95.3
                  2012 4 3 4 98.3
                  2013 1 3 1 98.85
                  2013 1 1 2 98.4667
                  2013 1 2 2 93.3
                  2013 1 3 2 97.2
                  2013 2 3 3 97.6
                  2013 2 2 2 94.1154
                  2013 2 2 3 96.15
                  2013 2 3 2 96.8
                  2013 3 3 3 96.1312
                  2013 3 2 3 96.4467
                  2013 3 3 2 98.0333
                  2013 3 2 2 92.24
                  2013 3 1 1 95.3333
                  2013 4 3 4 99
                  end
                  
                  egen mean = mean(coverage), by(finance year)
                  egen tag = tag(finance year)
                  separate mean, by(finance)
                  
                  * ssc install mycolours needed
                  mycolours
                  
                  forval g = 1/4 {
                  local extra `extra' || scatter mean`g' year if year == 2013, ms(none) mla(finance) mlabc("`OK`g''") mlabsize(medium)
                  }
                  
                  set scheme s1color
                  line mean? year if tag, sort xla(2005(2)2013) lc("`OK1'" "`OK2'" "`OK3'" "`OK4'") ///
                  yla(88(2)98, ang(h)) xtitle("") legend(off) `extra' xsc(r(. 2013.2)) ytitle(Mean coverage by finance type)
                  [ATTACH=CONFIG]n1601441[/ATTACH]
                  Hi Nick,

                  The diagram is perfectly working! Thanks so much!

                  By the way, I still have another question about the finance type. From the diagram, we see that the trend of type 1 and type 4 are similar. and when I run the command:
                  Code:
                  reg coverage i.year i.planning i.finance i.performance,r
                  the results show:
                  coverage Coef. St.Err. t-value p-value [95% Conf Interval] Sig
                  Planning
                  D planning Baseline
                  C planning -1.088 .545 -2.00 .046 -2.158 -.018 **
                  B planning 1.815 .568 3.19 .001 .7 2.93 ***
                  A planning 1.277 .617 2.07 .039 .067 2.488 **
                  Finance
                  D finance Baseline
                  C finance -2.253 .48 -4.69 .000 -3.195 -1.31 ***
                  B finance -2.326 .515 -4.52 .000 -3.335 -1.316 ***
                  A finance Omitted
                  Performance
                  D performance Baseline
                  C performance -3.117 .517 -6.03 .000 -4.131 -2.103 ***
                  B performance -1.68 .48 -3.50 .000 -2.623 -.738 ***
                  *** p<.01, ** p<.05, * p<.1

                  So why the type 1 finance (A finance) is omitted by collinearity? is it because that the trend of type 1 (D finance) and type 4 (A finance) are similar (they are highly correlated)?

                  Thanks again!

                  Comment


                  • #10
                    I'd ask #9 again in a new thread. The graphics question here is different. The test is whether a post matches the thread title.

                    But there is almost certainly an answer if you look at the data.

                    Code:
                     
                     graph matrix planning finance performance year

                    Comment

                    Working...
                    X