Announcement

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

  • How to graph panel data by region

    Dear Statalist Users,

    I am using DHS data (multiple rounds of surveys that have been administered in multiple countries and regions) and want to analyse the change of some variables over time. I have used
    Code:
    tsset Country_1 SurveyRound_1
    to define the panel and the time variable.

    I now want to show the change of the DVExperience over the different survey rounds. Once, for all countries included, but then also for specific countries or regions.

    Using
    Code:
    tsline DVExperience
    did more or less deliver the desired results, however, in between the value for each survey round there are vertical lines. To try to sidestep that, I have created a mean variable for the DVEperience for each survey round and then plotted it using
    Code:
    egen mean_DVExperience = mean(DVExperience), by(SurveyRound_1)
    graph two-way scatter meanDVExperience SurveyRound_1
    while this basically achieved what I wanted, it is still missing a connecting line between the dots. When trying to add a connecting line, all dots are being connected with each other instead of only from left to right to show the development over time. How can I achieve this?

    Thank you very much in advance!

  • #2
    tsline is indeed a bad choice for a panel set-up; it is really designed for single time series.

    Here is a sequence of commands you can run to see some possibilities. Some of the details are utterly specific to the data used, but there is much general technique in there too. I use an accessible dataset because you don't give us a data example (FAQ Advice #12).

    Code:
    webuse grunfeld, clear
    tsset company year 
    
    * a bad idea 
    tsline invest
    
    * a better idea 
    xtline invest, overlay
    xtline invest, overlay ysc(log)
    xtline invest, overlay ysc(log) yla(1000 300 100 30 10 3 1)
    
    * direct labelling, not a legend 
    * if stc2 as a colour doesn't work, you are not using Stata 18: see FAQ Advice for telling us about using outdated version
    * just use any colour you prefer  
    line invest year, c(L) ysc(log) yla(1000 300 100 30 10 3 1)  || scatter invest year if year == 1954, ms(none) mla(company)  || scatter invest year if year == 1935, ms(none) mla(company) mlabpos(9) mlabcolor(stc2) legend(off) xsc(r(1934.5 .))

    Comment


    • #3
      Thank you very much! I apologize for not following the FAQ.

      xtline indeed does what I want to achieve. However, I also want to display the development over time for different regions. Is this also possible? I tried the by() option, but that did not work.

      Here is an example of my data
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double PSE_Hus_Violence long(Country_1 SurveyRound_1) float Region
         . 13 1 0
         .  . . 0
         . 57 1 1
         . 52 1 1
         . 15 1 1
         . 20 1 1
         . 28 1 1
         .  8 1 1
      51.4 15 2 1
      24.8 20 2 1
      28.8 31 2 1
         . 28 2 1
         . 57 2 1
         . 52 2 1
         .  8 2 1
      43.7 57 3 1
      44.9 15 3 1
         . 30 3 1
      29.8 20 3 1
      30.2 31 3 1
         . 32 3 1
         .  8 3 1
      34.6 20 4 1
      42.2 57 4 1
      29.3 31 4 1
      35.8 32 4 1
         . 15 4 1
        34 31 5 1
      27.9 28 5 1
         . 15 5 1
         . 21 1 2
         . 68 1 2
         . 74 1 2
         . 35 1 2
         . 47 2 2
         . 21 2 2
         . 35 2 2
         . 68 2 2
         . 21 3 2
         . 68 3 2
      30.2 35 3 2
      31.7 35 4 2
         . 68 4 2
      30.3 21 4 2
         . 74 4 2
      25.9 35 5 2
         . 33 2 3
         .  6 2 3
      39.7 33 3 3
         . 43 3 3
         .  6 3 3
         .  6 4 3
        31 33 5 3
         .  6 5 3
      17.8 43 5 3
      31.8 33 6 3
         . 36 1 4
         . 51 1 4
         . 38 1 4
         . 72 1 4
         . 51 2 4
         . 69 2 4
         . 36 2 4
         . 51 3 4
      24.4 64 4 4
      28.1 38 4 4
      31.5 51 4 4
      30.8 64 5 4
      55.5  1 5 4
      26.3 51 5 4
      27.3 51 6 4
         . 73 1 5
         . 34 1 5
         . 58 1 5
         . 73 2 5
         . 34 2 5
      24.8 11 2 5
         . 58 2 5
      22.2 11 3 5
         . 34 3 5
         . 55 3 5
         . 73 3 5
        29 58 3 5
      36.1 66 4 5
         . 34 4 5
         . 11 4 5
      25.9 58 4 5
         . 55 4 5
      40.1 66 5 5
      28.7 11 5 5
         . 34 5 5
      20.9 49 5 5
      24.4 58 5 5
      33.5 55 5 5
      20.7 11 6 5
      17.5 58 6 5
         . 67 1 6
         .  7 1 6
         . 53 1 6
         . 27 1 6
      end
      label values Country_1 Country_1
      label def Country_1 1 "Afghanistan", modify
      label def Country_1 6 "Bangladesh", modify
      label def Country_1 7 "Benin", modify
      label def Country_1 8 "Bolivia", modify
      label def Country_1 11 "Cambodia", modify
      label def Country_1 13 "Central African Republic", modify
      label def Country_1 15 "Colombia", modify
      label def Country_1 20 "Dominican Republic", modify
      label def Country_1 21 "Egypt", modify
      label def Country_1 27 "Ghana", modify
      label def Country_1 28 "Guatemala", modify
      label def Country_1 30 "Guyana", modify
      label def Country_1 31 "Haiti", modify
      label def Country_1 32 "Honduras", modify
      label def Country_1 33 "India", modify
      label def Country_1 34 "Indonesia", modify
      label def Country_1 35 "Jordan", modify
      label def Country_1 36 "Kazakhstan", modify
      label def Country_1 38 "Kyrgyz Republic", modify
      label def Country_1 43 "Maldives", modify
      label def Country_1 47 "Morocco", modify
      label def Country_1 49 "Myanmar", modify
      label def Country_1 51 "Nepal", modify
      label def Country_1 52 "Nicaragua", modify
      label def Country_1 53 "Niger", modify
      label def Country_1 55 "Pakistan", modify
      label def Country_1 57 "Peru", modify
      label def Country_1 58 "Philippines", modify
      label def Country_1 64 "Tajikistan", modify
      label def Country_1 66 "Timor-Leste", modify
      label def Country_1 67 "Togo", modify
      label def Country_1 68 "Turkey", modify
      label def Country_1 69 "Turkmenistan", modify
      label def Country_1 72 "Uzbekistan", modify
      label def Country_1 73 "Vietnam", modify
      label def Country_1 74 "Yemen", modify
      label values SurveyRound_1 SurveyRound_1
      label def SurveyRound_1 1 "DHS-III", modify
      label def SurveyRound_1 2 "DHS-IV", modify
      label def SurveyRound_1 3 "DHS-V", modify
      label def SurveyRound_1 4 "DHS-VI", modify
      label def SurveyRound_1 5 "DHS-VII", modify
      label def SurveyRound_1 6 "DHS-VIII", modify
      label values Region Region
      label def Region 1 "LAC", modify
      label def Region 2 "MENA", modify
      label def Region 3 "SA", modify
      label def Region 4 "CA", modify
      label def Region 5 "SEA", modify
      label def Region 6 "SSA", modify
      I am using Stata 15

      Thank you very much!

      Comment


      • #4
        You should show the command you tried and say in what sense it "did not work".

        FAQ 12.1:

        Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

        Comment


        • #5
          I see my mistake now. Thanks.

          What I want to achieve is:

          1. to show the development over time across all countries together. Right now
          Code:
          xtline DVExperience, overlay
          shows the development over time for each country individually. What I want instead is one line showing the global average, just as
          Code:
          tsline DVExperience
          would do.

          2. to show the development across time for the individual regions. Using the help file for xtline I tried the by option but got the following error message
          Code:
          xtline DVExperience, by(Region)
          option by() not allowed
          r(191)
          Basically, I was able to achieve all I want with tsline the only problem here are the vertical lines.

          I hope I now gave all the necessary information

          The example of the data above is still valid. I am usting Stata 15

          Comment


          • #6
            There is a lot missing here, including a variable DVExperience, but I can make some guesses.

            If I understand correctly your call to xtline is doomed as you've declared the panels to be countries, so xtline is committed either to showing them altogether or one by one. A series of plots by region is harder, but you may get closer with

            Code:
            separate PSE_Hus_Violence, by(Country_1) veryshortlabel 
            local results `r(varlist)' 
            line `results' SurveyRound, by(Region, legend(off))

            Your global average is perhaps

            Code:
            egen mean = mean(PSE_Hus_Violence), by(Country_1)
            and you can add that to the plot

            Code:
            line `results' SurveyRound, by(Region, legend(off)) || line mean SurveyRound, lw(thick)
            where you use something distinctive -- lw(thick) isn't necessarily the best idea -- so that the mean is indeed distinctive.

            Comment

            Working...
            X