Announcement

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

  • Getting xline corresponding to a specific day on xtline for quarterly data.

    I'm using the xtline command to generate the chart below. My data is expressed with use of the quarterly intervals. I would like to add a vertical line corresponding to a specific day. So for example, in the chart below I would like the line to correspond to the event that took place on 11 November 2010.
    Click image for larger version

Name:	example.png
Views:	1
Size:	21.2 KB
ID:	339528
    Code
    Code:
    xtline measure, overlay ///
            plot1(lwidth(medthick) lpattern(dash_dot)) ///
            plot2(lwidth(medthick) lpattern(shortdash)) ///
            plot3(lwidth(medthick) lpattern(solid)) ///
            plot4(lwidth(medthick) lpattern(longdash)) ///
            title("Example", size(medium)) ///
            ytitle("Some measure", size(small)) ///
            xtitle("`xaxtitle'", size(small)) ///
            ylabel(, labsize(vsmall)) ///
            xtitle(, size(small)) ///
            xlabel(,  labsize(vsmall) alternate) ///
            xline(20) ///
            name(example, replace)

    It appears that it is still not possible to upload dta files directly to the forum. The attached data is in Excel. Time series:
    Code:
    gen date = quarterly(time, "YQ")
        format %tqCCYY-!Qq date
        xtset group date
    Attached Files
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    11 Nov 2010 is pretty near halfway through 2010q4 so you want a line at or very near 203 on the time axis.

    Code:
    . di yq(2010,4)
    203

    Comment


    • #3
      Nick,

      As illustrated below, it does what I should. Having said that I'm wondering whether I could make it more automatic as I will have to generate a number of charts of different time scales with set of dates marked on each. My ideal solution would be to somehow automatically translate provided data into corresponding figure and pass the values in local macro when applicable.
      Click image for larger version

Name:	example2.png
Views:	1
Size:	21.9 KB
ID:	339693
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        Just to add to my previous posts, I'm looking at tline option, which I would like to place on a specific day. The problem is that my data uses quaterly intervals and I would like for the line to correspond to a specific day. I keep on getting the error below when trying to plot the line.
        Code:
        (...)
        tline(15sep2008)
        (...)
        invalid argument "15sep2008" must be a number or valid argument for q() function
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          You haven't given us a reproducible example. This is from the manual

          Code:
          sysuse tsline2
          tsset day
          tsline calories, tline(28nov2002 25dec2002)
          and works in 13.1. Did you tsset or xtset the data?

          As in your first question you need to supply an equivalent quarterly date. I would not expect tsline() to convert an argument for me.

          Comment


          • #6
            Nick,

            Thanks, my thinking is that even if the dates are quaterly they correspond to the exact figures on the x axis, as in your example. So if di yq(2010,4), corresponds to 203 there should be a way to derive the the figure from more precise date that would indicate the position of the line at the right distance from tick marks corresponding to 2008Q4 and 2008Q3, I just don't know how to do it. As a more general matter, I would be happy to for the tsline (or xtline in my case) to convert the argument, as it is rather common request that the one may be willing to mark days in a chart with monthly or quarterly time series. To answer your question, my data is xtset.
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              Also my apologies, for not providing reproducible examples. I included the code below that should show what I'm trying to achieve as I'm doing a number of transformations on the data providing reproducible example would be quite onerous.
              Code:
              * Define time series data.
              capture : xtset dec_simd12 date
              collapse (mean) cs_jsa_pertot_, by(dec_simd12 date)
              xtset dec_simd12 date
              
              // Draw the graph
              xtline cs_jsa_pertot_ if (dec_simd12 == 1) | (dec_simd12 == 10), overlay ///
                  plot1(lwidth(thick) lpattern(solid)) ///
                  plot2(lwidth(thick) lpattern(dash)) ///
                  title("Title", size(medium)) ///
                  subtitle("{it:text}", size(small)) ///
                  caption("{it:Text}", size(vsmall)) ///
                  note("note", ///
                  size(vsmall)) ///
                  legend(cols(1) size(vsmall) position(10) ring(0) rowgap(.5) ///
                      region(fcolor(gs15) lwidth(vvthin) lcolor(black)) ///
                           title("{bf: Geographies}", size(vsmall) justification(left) ///
                              position(11) margin(tiny)) symxsize(8)) ///
                  ytitle("text", size(small)) ///
                  xtitle("Quarter", size(small)) ///
                  ylabel(, labsize(vsmall) tlcolor(black)) ///
                  xtitle(, size(small)) ///
                  xlabel(#15, labsize(vsmall)  alternate tlcolor(black)) ///
                  scheme(burd) ///
                  tline(15sep2008) ///
                  name(newchart, replace)
              Kind regards,
              Konrad
              Version: Stata/IC 13.1

              Comment


              • #8
                It's easy enough to give reproducible examples -- and examples free of other stuff that's not material to the problem. A dataset with quarterly data everyone can use is turksales.dta. As reported, tline() can't make sense of daily dates for such data, so you must convert. Try this code to see the principle:

                Code:
                 
                webuse turksales
                tsset
                line sales t, tline(1996q4)
                line sales t, tline(5nov1996)
                line sales t, tline(`=qofd(daily("5nov1996", "DMY"))')

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  As reported, tline() can't make sense of daily dates for such data, so you must convert.
                  I understand that this is why, my initial attempt was to draw the line with use of the xtline not tline. Let's say that in a hypothetical chart with two data points each for one quarter located at opposite ends of the axis I want to add two xlines one for early and another one for late September. Considering that the range of x-axis would be relatively norrow, or better short, visually the lines should aper as in some distance from each other. My question is how to calculate that distance. di yq(2010,4) solves this partially but I cannot run it on a daily date:

                  Code:
                  . di mdy(9,15,2008)
                  17790
                  as naturally the obtained figure will be beyond the scale.
                  Kind regards,
                  Konrad
                  Version: Stata/IC 13.1

                  Comment


                  • #10
                    Just to add, this is what I want to produce but I want to calculate the 150 and 150.3 to something that would correspond to where the day would be if I would have a data set with delta one day.

                    Code:
                    clear
                    webuse turksales
                    tsset
                    line sales t, xline(150 150.3) ///
                    text(100 148 "day") ///
                    text(100 154 "week after")
                    Click image for larger version

Name:	Graph.png
Views:	1
Size:	58.1 KB
ID:	370963
                    Kind regards,
                    Konrad
                    Version: Stata/IC 13.1

                    Comment


                    • #11
                      Daily dates to equivalent quarterly dates are a pain because of leap years. I think you have going to have to write your own conversion code for what you want.

                      Comment


                      • #12
                        Thanks, it crosses my mind that there may be a workable solution of using your do suggestion to get the quarter before and ending quarter, divide the difference by number of days and count the number of days from start quarter. This would indicate approximate location on x axis.
                        Kind regards,
                        Konrad
                        Version: Stata/IC 13.1

                        Comment


                        • #13
                          I am sure that it's codeable; I was just being lazy as the problem is not personally amusing.

                          But there is another way to do it.

                          Convert your quarterly data to daily data using rules such as quarter 1 = 15 Feb, 2 = 15 May, 3 = 15 Aug, 4 = 15 Nov. Then plot your day lines as you wish. I think you need a rule that each quarter is plotted at its middle for graphs to make most sense.

                          Consider this code:

                          Code:
                          webuse turksales
                          rename t qdate
                          gen ddate = mdy(2 + 3 * mod(qdate, 4), 15, yofd(dofq(qdate)))
                          format ddate %td
                          edit
                          There could be an easier way to do it. You need to tsset afresh.

                          Comment


                          • #14
                            Originally posted by Nick Cox View Post
                            I was just being lazy as the problem is not personally amusing.
                            I'm sad to read that. I even wanted to suggest a program name, maybe daytox? The command syntax could like that:
                            Code:
                            daytox date, [start date, end date ]
                            It would calculate x depending on start and and end date or maximum range of the tsset data if not provided
                            Kind regards,
                            Konrad
                            Version: Stata/IC 13.1

                            Comment


                            • #15
                              I did suggest a solution. Here is a simpler one.

                              Code:
                              webuse turksales , clear
                              rename t qdate
                              gen ddate = mdy(2 + 3 * mod(qdate, 4), 15, yofd(dofq(qdate)))
                              gen ddate2 = dofq(qdate) + 45
                              gen diff = ddate - ddate2
                              format ddate* %td
                              edit
                              Here is a summary of how I see your problem now.

                              1. Wanting to add a line defined by a daily date to a graph for quarterly data with tline() won't work because tline() expects ... a quarterly date.

                              2. So map the quarterly date to equivalent daily dates. Using dofq() replaces each date by the daily date of its start, e.g. 1 January for quarter 1. For graphical purposes you'd be better with a date in its middle. Adding 45 days seems precise enough.
                              Last edited by Nick Cox; 30 Oct 2014, 03:35.

                              Comment

                              Working...
                              X