Announcement

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

  • Drawing a tsset time series with two y variables that don't have the same scale

    Hello Stata people;

    I have this type of data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str13 date int(primedutransportetderefroidissem prixminimumlaproductionmillimeli)
    "November 2007"  40  450
    "January 2008"   40  500
    "November 2008"  40  550
    "January 2010"   40  580
    "October 2012"   60  700
    "October 2014"   70  733
    "January 2015"   70  736
    "April 2017"     70  766
    "July 2018"      90  890
    "April 2019"    105  945
    "July 2020"      95 1040
    "April 2021"    100 1140
    "November 2022" 200 1340
    end

    What I wanna do is draw a tsset time serie that shows the evolution of the two prices in the time, according to the available dates.
    Already, the date variable is not well defined and I don't know how to do so in a way that I could apply the tsset comman (if already I should apply it)
    And also, is there an option in codes that could help me draw another y axis for the variable "primedutransportetderefroidissem" since it doesn't have the same scale as the other variable?

    Thanks for the help.

  • #2
    Code:
    gen mdate = monthly(date, "MY")
    assert missing(mdate) == missing(date)
    format mdate %tmMon_YY
    label var mdate "Month"
    
    tsset mdate
    
    graph twoway line primedutransportetderefroidissem mdate, yaxis(1) ///
        || line prixminimumlaproductionmillimeli mdate, yaxis(2)

    Comment


    • #3
      Although I guess that the units of measurement are different, this graph is (a start on) a serious alternative candidate:

      Code:
      gen mdate = monthly(date, "MY")
      format mdate %tm
      line prime prix mdate, ysc(log) yla(50 100 200 500 1000, ang(h)) legend(col(1))
      It's a matter of taste partly but I advise much shorter variable names and letting longer variable labels carry more information.

      Comment


      • #4
        Here is another take:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str13 date int(primedutransportetderefroidissem prixminimumlaproductionmillimeli)
        "November 2007"  40  450
        "January 2008"   40  500
        "November 2008"  40  550
        "January 2010"   40  580
        "October 2012"   60  700
        "October 2014"   70  733
        "January 2015"   70  736
        "April 2017"     70  766
        "July 2018"      90  890
        "April 2019"    105  945
        "July 2020"      95 1040
        "April 2021"    100 1140
        "November 2022" 200 1340
        end
        
        gen mdate = monthly(date, "MY")
        * format mdate %tm
        set scheme s1color 
        
        * Stata Journal https://journals.sagepub.com/doi/pdf/10.1177/1536867X221141058
        mylabels 2007/2022, myscale(ym(@, 7) - 0.5) local(labels) format(%02.0f)
        myticks 2007/2023, myscale(ym(@, 1) - 0.5) local(ticks)
        
        * dividing line at geometric mean 
        local where = sqrt(200 * 450)
        
        twoway connected prime prix mdate, lc(red blue) mc(red blue) ysc(log) yla(50 100 150 200  400(200)1400, ang(h)) legend(off) xla(`labels', noticks labsize(small)) xtic(`ticks', tlength(*3)) yli(`where', lc(gs12)) text(1200 565 "prix text here", color(blue) place(e)) text(50 740 "prime text here", color(red) place(w)) ysc(r(. 2000)) xtitle("") aspect(0.7)
        Click image for larger version

Name:	prixprime.png
Views:	1
Size:	24.9 KB
ID:	1705747


        It is fortunate but not completely fortuitous that log scale works quite well here. The different units of measurement are in our favour.

        As the data are irregularly spaced it seems helpful to show exactly where the observations fall in time.

        We can avoid the awkward legend in favour of direct labelling.

        I am not completely happy with the year labelling but the principle is that text labels go in the middle at each interval (without ticks) and ticks go at the ends of each interval but are longer. I thought of showing just 07 to 22 but that needs more decoding by the reader.

        More on that at https://journals.sagepub.com/doi/pdf...867X0800700410 -- except that the commands used above avoid the need for any loops.

        Comment


        • #5
          Note that in #4 I commented out a format statement. For reasons I don't understand, formatting the monthly date variable as a monthly date (surprise!) interferes with the trickery of mylabels and myticks.

          Comment


          • #6
            Clyde Schechter Thanks for the help It worked, just I had a little problem with the irregular spacing of the data, also, those dates are important in the data because they show the month in which the decision of changing the price was made.
            Nick Cox The units of measurment are the same, it is basically a price expressed in a monetary unit, it's about the scale and the step of each Y variable that are different from each other, that's why I was hoping to have 2 Y axes. As for the irregularity of the time variable spacing, thos time variables are crucial, and I wanna show them in the graph, so I guess your first code suggestion is more helpful.

            Thanks for both of you

            Comment


            • #7
              Clyde Schechter Is there an option in the code that could help show just the dates in the data on the graph ? Because those dates are crucial for the analysis, just don't bother with the spacing between them, but I wanna show the value of the price for each of those given dates

              Comment


              • #8
                Well, it depends on what you mean by "show." The following is the best I could do to include all of those dates on the horizontal axis and get them almost readable.

                Code:
                levelsof mdate, local(dates)
                
                graph twoway line primedutransportetderefroidissem mdate, yaxis(1) ///
                    || line prixminimumlaproductionmillimeli mdate, yaxis(2) ///
                    xlabel(`dates', format(%tmMonYY) angle(90) labsize(vsmall))
                The problem is that some of them are very close together, so you have to orient them vertically and also reduce the size so that they don't just smash completely on top of each other. Personally, I would hate this graph, but perhaps it will serve your purposes. I don't think there is any way to do it and still leave them all truly separated and also large enough to read easily.

                Comment


                • #9
                  Clyde Schechter Thanks Yeah, that is what I mean by "show", I mean tha Iwanna have the dates of the data on the graph.

                  Comment

                  Working...
                  X