Announcement

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

  • Drawing a time series graph of prices with 19 line

    Hi Stata community;

    I do have this data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(ann_2001 ann_2002 ann_2003 ann_2004 ann_2005 ann_2006 ann_2007 ann_2008 ann_2009 ann_2010 ann_2011 ann_2012 ann_2013 ann_2014 ann_2015 ann_2016) int ann_2017 double ann_2018 int ann_2019 long Month
       210 216.31 227.69 320.53 250.52 233.35 308.18 496.89    465 403.24      . 562.88 575.41      .      .  430.7   . 411.24   .  1
    221.91 207.45  219.1 343.16 269.55 226.89 286.64 479.56      .      .      .      .      . 609.94      .      .   .      .   .  2
    222.63  201.7 276.14      .      .      .      . 377.69 470.81      .      .      .      . 532.29      .      .   .      . 351  3
    234.87 205.63 210.85 317.85 243.84 259.93 258.17 494.62      .      .      . 406.43      .      .      .    359 362      .   .  4
    250.77 210.95 205.48 296.82 233.56      . 263.38 458.36      .      .      .      .      .      .      .      .   .      .   .  5
    205.47 213.04 225.97 348.59 276.79 235.66 318.76      . 521.21    380 426.08      . 592.45 580.83      .      .   .      .   .  6
    197.91 215.12 210.87 363.95    300 232.09    262 440.94    480      . 425.72      . 607.54 507.04 388.77 386.45 376      .   .  7
    197.79 203.26    224 382.92 269.12 234.72 273.67 463.42 415.98      . 423.37      .      . 594.87      . 338.35 367  475.5   .  8
     240.5 207.33 220.53 349.04 247.41  232.5 298.06 480.89 364.81      .      .      .      .    612      . 338.26 396    515   .  9
    211.12 210.53 290.59 229.82 243.44 264.03 397.71 410.35      .      .      .  601.2      . 554.55 387.85      .   .      . 356 10
    214.86 221.63 247.38 234.15 251.78    249 374.39 452.75      .      .      . 658.86      .      . 391.03 385.71   .      . 374 11
    215.23 210.05    247 286.43 264.91    240 306.97 496.89      . 407.71      . 681.43      .      . 409.02  409.4   .      .   . 12
    end
    format %tm Month
    label values Month Month
    label def Month 1 "aout", modify
    label def Month 2 "avril", modify
    label def Month 3 "decembre", modify
    label def Month 4 "fevrier", modify
    label def Month 5 "janvier", modify
    label def Month 6 "juillet", modify
    label def Month 7 "juin", modify
    label def Month 8 "mai", modify
    label def Month 9 "mars", modify
    label def Month 10 "novembre", modify
    label def Month 11 "octobre", modify
    label def Month 12 "septembre", modify
    My goal is to draw a time series plot of the prices with the months as my x variable, and the prices as the y variable, so the graph will contain 19 lines, each line represent the price through that year

    I also have defined the "Months" variable as a time variable using the command tsset.

    Ant=y suggestions please?

  • #2
    The months variable is out of order and may arise from an earlier encode.

    Otherwise you would be better off after a
    reshape long.

    Comment


    • #3
      Nick Cox At first, they were in the form of a string variable, I guess I had to encode that variable in order to be able to apply the tsset command and transform it into a time variable. Otherwise, I don't think it's possible to work on them as a string form, if it could be possible, I would be happy to get some help with that
      Thanks

      Comment


      • #4
        Indeed: you do need to encode but encode doesn't understand dates (and the same problem would arise with English names). So, you can go back to the original or you can do this.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input double(ann_2001 ann_2002 ann_2003 ann_2004 ann_2005 ann_2006 ann_2007 ann_2008 ann_2009 ann_2010 ann_2011 ann_2012 ann_2013 ann_2014 ann_2015 ann_2016) int ann_2017 double ann_2018 int ann_2019 long Month
           210 216.31 227.69 320.53 250.52 233.35 308.18 496.89    465 403.24      . 562.88 575.41      .      .  430.7   . 411.24   .  1
        221.91 207.45  219.1 343.16 269.55 226.89 286.64 479.56      .      .      .      .      . 609.94      .      .   .      .   .  2
        222.63  201.7 276.14      .      .      .      . 377.69 470.81      .      .      .      . 532.29      .      .   .      . 351  3
        234.87 205.63 210.85 317.85 243.84 259.93 258.17 494.62      .      .      . 406.43      .      .      .    359 362      .   .  4
        250.77 210.95 205.48 296.82 233.56      . 263.38 458.36      .      .      .      .      .      .      .      .   .      .   .  5
        205.47 213.04 225.97 348.59 276.79 235.66 318.76      . 521.21    380 426.08      . 592.45 580.83      .      .   .      .   .  6
        197.91 215.12 210.87 363.95    300 232.09    262 440.94    480      . 425.72      . 607.54 507.04 388.77 386.45 376      .   .  7
        197.79 203.26    224 382.92 269.12 234.72 273.67 463.42 415.98      . 423.37      .      . 594.87      . 338.35 367  475.5   .  8
         240.5 207.33 220.53 349.04 247.41  232.5 298.06 480.89 364.81      .      .      .      .    612      . 338.26 396    515   .  9
        211.12 210.53 290.59 229.82 243.44 264.03 397.71 410.35      .      .      .  601.2      . 554.55 387.85      .   .      . 356 10
        214.86 221.63 247.38 234.15 251.78    249 374.39 452.75      .      .      . 658.86      .      . 391.03 385.71   .      . 374 11
        215.23 210.05    247 286.43 264.91    240 306.97 496.89      . 407.71      . 681.43      .      . 409.02  409.4   .      .   . 12
        end
        format %tm Month
        label values Month Month
        label def Month 1 "aout", modify
        label def Month 2 "avril", modify
        label def Month 3 "decembre", modify
        label def Month 4 "fevrier", modify
        label def Month 5 "janvier", modify
        label def Month 6 "juillet", modify
        label def Month 7 "juin", modify
        label def Month 8 "mai", modify
        label def Month 9 "mars", modify
        label def Month 10 "novembre", modify
        label def Month 11 "octobre", modify
        label def Month 12 "septembre", modify
        
        * start here 
        decode Month, gen(month)
        label def month 1 janvier 2 fevrier 3 mars 4 avril 5 mai 6 juin 7 juillet 8 aout 9 septembre 10 octobre 11 novembre 12 decembre 
        
        drop Month
        encode month, gen(Month) label(month)
        
        reshape long ann_, i(Month) j(Year)
        
        rename ann_ Quelque 
        
        gen Mdate = ym(Year, Month) 
        format Mdate %tm 
        tsset Mdate 
        
        tsline Quelque, cmissing(n) name(G1, replace)

        Comment


        • #5
          Nick Cox Thanks for the help, it worked, I really appreciate the help Tho, at first, I kinda wanted to have 19 lines on the same graph, each one showing the evolution of the price through a whole year, since there are 19 years of data available. But yet, I don't think that would be good because the graph would be crowded, and also with the missing values for some prices, the comparaison between years is gonna be a bit difficult.
          Thank you

          Comment


          • #6
            19 colours wouldn't work well and the legend will just take up a great deal of space.

            You could try

            Code:
            twoway connected Quelque Month, by(Year, compact note(""))

            or fabplot from the Stata Journal. https://journals.sagepub.com/doi/pdf...6867X211025838
            Last edited by Nick Cox; 14 Mar 2023, 02:52.

            Comment

            Working...
            X