Announcement

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

  • attempt at simple xtline plot gives syntax error

    Hi everybody,

    I am using Stata15 and want to show oil price development in the time-period I am looking at since its part of my independant variable.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id_2 long yrmo int target_sidea double oil_price_t0 float OPS_t
    . 200312   .  27.85            .
    . 200912   .  75.48            .
    . 200304   .  23.43            .
    . 200907   .  64.96            .
    . 200305   .  24.25            .
    . 200905   .  57.39            .
    . 200401   .  28.68            .
    . 200906   .  69.21            .
    . 200307   .  26.62            .
    . 200303   .  27.42            .
    . 200311   .   27.5            .
    . 200309   .  25.26            .
    . 200302   .  30.19            .
    . 200908   .  71.32            .
    . 200911   .  77.62            .
    . 200308   .  27.61            .
    . 200310   .  27.13            .
    . 200306   .  25.49            .
    . 200910   .  73.27            .
    . 200903   .  45.57            .
    . 200904   .  50.18            .
    . 200301   .  28.05            .
    . 200909   .   67.9            .
    1 200806  27 127.58    .07012443
    1 200709  57  73.25    .08620504
    1 200405  35  34.47    .10416858
    1 200803  28  96.77    .07297191
    1 200511  76  51.31   -.04478218
    1 200605 221   64.9   .013183548
    1 200606 128  65.08   .002769635
    1 200802  37  89.96   .031504914
    1 200510 105  53.66   -.05228052
    1 200609 184  59.77   -.14040913
    1 200901  18  44.96    .09244503
    1 200807  21 131.22    .02813167
    1 200406  52   33.4  -.031533517
    1 200702 185  55.68    .06818504
    1 200503 118  45.57     .1073863
    1 200608 149  68.78  -.003773055
    1 200407  54  34.48     .0318235
    1 200610 245   56.5   -.05626323
    1 200509  96  56.54 -.0014139086
    1 200404  43  31.06     .0195065
    1 200801  42  87.17    .01642415
    1 200811  34  51.38   -.28933507
    1 200410  87  37.57    .05751856
    1 200701 216  52.01   -.12032206
    1 200703 139  59.05    .05876352
    1 200412 107  34.26  -.017648337
    1 200808  36 113.21   -.14763081
    1 200501 162  37.81    .09859517
    1 200812  39  40.99   -.22592087
    1 200602 145  57.57   -.01277203
    1 200707 114  69.45     .0547474
    1 200409  12  35.47   -.07650152
    1 200505 179     45   -.04561048
    1 200508 112  56.62    .06890459
    1 200611 136  56.81   .005471728
    1 200704 186  63.83    .07783877
    1 200804  30 103.46    .06684807
    1 200607 172  69.04    .05906874
    1 200403  32  30.46     .0703774
    1 200604 180  64.05    .10544727
    1 200603 138  57.64  .0012151777
    1 200506 122  50.97    .12457474
    1 200512  29  53.12   .034667812
    1 200902  18  43.13   -.04155437
    1 200402  14  28.39  -.010163056
    1 200507 117  52.85   .036220465
    1 200411 231  34.87     -.074579
    1 200504 123   47.1   .033023402
    1 200706 108  65.75    .01857447
    1 200809  30  95.96   -.16531305
    1 200810  31  68.62    -.3353474
    1 200711  52  86.73     .1171779
    1 200601 163  58.31    .09322012
    1 200805  32 118.94    .13943411
    1 200712  57  85.75  -.011363798
    1 200612 124  58.66   .032045674
    1 200502 101  40.93    .07928964
    1 200408  78  38.29    .10480934
    1 200705 123  64.54   .011061858
    1 200710  45  77.14     .0517437
    1 200708  77   67.2   -.03293378
    2 200601 293  58.31    .09322012
    2 200604 208  64.05    .10544727
    2 200509 185  56.54 -.0014139086
    2 200512  75  53.12   .034667812
    2 200703 339  59.05    .05876352
    2 200610 378   56.5   -.05626323
    2 200902  16  43.13   -.04155437
    2 200808  29 113.21   -.14763081
    2 200510 295  53.66   -.05228052
    2 200708 199   67.2   -.03293378
    2 200607 278  69.04    .05906874
    2 200612 350  58.66   .032045674
    2 200704 360  63.83    .07783877
    2 200806  48 127.58    .07012443
    2 200609 302  59.77   -.14040913
    2 200506 149  50.97    .12457474
    end
    format %tm yrmo


    The data is correctly xtset:
    Code:
    xtset id_2 yrmo, monthly
    And I want (for starters) a simple graph:
    Code:
    xtline oil_price_t0, overlay
    After all I have read this should work, but it doesn't and Stata gives me a 'invalid syntax'.

    I'd appreciate any hint.
    Best
    Marvin
    Last edited by Marvin Reusch; 29 Dec 2021, 08:42.

  • #2
    You do not have a proper Stata year-month variable.

    Code:
    format yrmo %5.0f
    gen ym= ym(int(yrmo/100), yrmo-(int(yrmo/100)*100))
    format ym %tm
    xtset id_2 ym

    Comment


    • #3
      To Andrew's advice let me add the following.

      Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

      All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Let me add that the output of xtset was a hint that you weren't getting the date correct
      Code:
      . xtset id_2 yrmo, monthly
      
      Panel variable: id_2 (unbalanced)
       Time variable: yrmo, 2.0e+05 to 2.0e+05, but with gaps
               Delta: 1 month
      while doing as Andrew demonstrated yields
      Code:
      . xtset id_2 ym
      
      Panel variable: id_2 (unbalanced)
       Time variable: ym, 2004m2 to 2009m2, but with gaps
               Delta: 1 month

      Comment


      • #4
        Thanks to both of you!
        That works perfectly for the graph.

        I'm a bit worried about the main part of my project now though. If I use Andrews setup and adjust (i.ym instead of i.yrmo) my regression to
        Code:
        eststo reg1: quietly ppmlhdfe   target_sidea    OPS_t1    l(1).target_sidea ,  irr absorb( i.ym i.month##i.id_2 ) vce(cluster id_2)
        , then Stata yields the error 'not sorted'.

        Comment


        • #5
          If it works with "yrmo" as your time variable, it should work with "ym". Provide a MWE as in #1 that replicates the error.

          Comment


          • #6
            Have you tried including
            Code:
            sort id_2 ym

            Comment


            • #7
              Again, thanks a lot! I would even have reported wrong results.
              For future questions: What is MWE?

              Comment


              • #8
                https://en.wikipedia.org/wiki/Minimal_working_example

                Comment


                • #9
                  Some further questions regarding the graph:

                  How can I display the dates as eg Jun 2004 instead of 2004m6?
                  I tried:
                  Code:
                  xtline oil_price_t0, overlay legend(off) note(Description: Crude oil - Dubai medium - Fateh API 32° - spot price - FOB Dubai) ///
                          xtitle("") tlabel(2004m6(6)2009m6) xlabel(, angle(45)) ///
                          ytitle(US-Dollar per Barrel) plotregion(margin(0)), ///
                          format("%tmMon_CCYY")
                  It plots nicely without the format. When I include the format option Stata says its an 'invalid format'. I can't figure out the right specification.

                  Further I want to include oil price shocks in average oil producing disrict (similar as in the picture below). Could you give some guidance? I honestly don't even know where to start looking. I'd be grateful for any ideas.

                  Click image for larger version

Name:	oil_price_dynamics.png
Views:	1
Size:	96.0 KB
ID:	1642943

                  Comment


                  • #10
                    With your data example from #1 this works

                    Code:
                    gen mdate = ym(floor(yrmo/100), mod(yrmo, 100))
                    
                    xtset id_2 mdate 
                    
                    xtline oil_price_t0, overlay legend(off) note(Description: Crude oil - Dubai medium - Fateh API 32° - spot price - FOB Dubai) ///
                            xtitle("") tlabel(2004m6(6)2009m6) xlabel(, angle(45)) ///
                            ytitle(US-Dollar per Barrel) plotregion(margin(0)) xla(, format("%tmMon_CCYY"))
                    The problems with your syntax in #9 include

                    1. The comma before the format() option is illegal in your terms if you think that format() is an option here.

                    2. format() is in fact a suboption of xlabel().

                    I'd recommend consideration of a different solution here as documented in https://www.stata-journal.com/articl...article=gr0030 That way you avoid the always awkward ang(45).

                    The new graph you show in #9 is too far from your data as so far explained for me to have any useful suggestion.

                    Comment


                    • #11
                      Thanks Nick!

                      For everyone with similar problems, the key seems to be the correct specification of datetime (help datetime)and panel data declaration (help xtset).

                      Comment


                      • #12
                        For anyone ending up here with a similar problem, this comes very close to what I want:

                        Code:
                        twoway bar OPS_total mdate,  color(white) lcolor(black) yaxis(2) ylabel(-40(10)40 ,axis(2))  ytitle("Oil Price Shocks (Log-Diff.)", axis(2)) yscale(alt) ///
                                    || tsline oil_price_t0, color(red) ytitle("Oil Price (US-Dollar per Barrel)") ylabel(-50(25)150) ytick(-50(25)150) yscale(alt axis(2)) ///
                                    || , xtick("516(12)599") xla("522(12)599", noticks format(%tmCCYY)) xtitle("") title(Figure 1: Oil Price Dynamics and Shocks) ///
                                    legend(rows(2) order (2 "Crude oil price, Dubai medium, Fateh API 32°" 1 "Oil Price Shock in the average oil producing district"))
                        The only thing left to do would probably be to set the tsline for the right timeframe, maybe with something like
                        Code:
                        if tin(528, 587)
                        . That does not work for me though and there is no more time. Good luck struggling soul in the future.

                        Comment

                        Working...
                        X