Announcement

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

  • How can I make a line graph for data from a certain date range?

    Hello! I'm working with daily time series data with the date originally in the format "01/29/19" (string) which I changed to 29jan2019.

    I want to use the tsline command to plot my 2 series- insys_growth and spy_growth for the date ranges 02mar2015 to 31may2017
    I used the command
    Code:
    tsline insys_close spy_close if 02mar2015<=edate<=30may2017
    but it keeps giving me an error 30may2017 invalid name (even though the date is totally there in my data).
    I also tried using the inrange command
    Code:
    tsline spy_growth insys_growth if inrange(date, 02mar2015, 31may2017)
    but I get the error "31may2017 invalid name r(198);"

    A preview of my data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 date float(edate insys_growth spy_growth)
    "1/30/09" 17927   .1988509 4.4167905
    "2/2/09"  17930 .016529638 4.4137673
    "2/3/09"  17931 .016529638 4.4277167
    "2/4/09"  17932   .1988509 4.4228086
    "2/5/09"  17933   .3530013 4.4375796
    "2/6/09"  17934   .3530013  4.465678
    "2/9/09"  17937   .3530013 4.4670568
    "2/10/09" 17938   .1988509  4.420165
    "2/11/09" 17939   .3530013 4.4260435
    "2/12/09" 17940   .3530013  4.426761
    "2/13/09" 17941    .604316  4.415945
    "2/16/09" 17944   .4865331         .
    "2/17/09" 17945 .016529638 4.3722286
    "2/18/09" 17946   .4865331 4.3698277
    "2/19/09" 17947   .1988509  4.359014
    "2/20/09" 17948   .1988509  4.349245
    "2/23/09" 17951   .1988509 4.3128104
    "2/24/09" 17952   .1988509   4.35002
    "2/25/09" 17953   .1988509  4.342116
    "2/26/09" 17954   .1988509  4.325721
    "2/27/09" 17955 .016529638 4.3031187
    "3/2/09"  17958  -.2066147   4.25703
    "3/3/09"  17959   .1988509 4.2494946
    "3/4/09"  17960 .016529638  4.272909
    end
    format %td edate
    How can I plot the tsline graph only for a specific date range?
    (Not sure why it's showing the date as 17927 and such, when it's actually in the 30may2017 format.)
    I also want to round up the insys_growth and spy_growth decimals and format them to have to digits after the decimal point. For instance, 4.272909 would become 4.27. How can I do that?

    Thank you!!
    Shruti

  • #2
    A key detail of dataex is to show numeric dates as ... numeric dates. If it showed 20may2017 and so forth people answering would have to do the surgery to convert such displayed dates to numeric. You want to make life more difficult for us? (Insert emoticon of choice.) We are unpaid and only sometimes appreciated. Naturally not.

    That aside, your attempted tsline statements won't work for your example data, but the principle is otherwise easy. inrange() knows nothing about dates, so you need tin() instead.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 date float(edate insys_growth spy_growth)
    "1/30/09" 17927   .1988509 4.4167905
    "2/2/09"  17930 .016529638 4.4137673
    "2/3/09"  17931 .016529638 4.4277167
    "2/4/09"  17932   .1988509 4.4228086
    "2/5/09"  17933   .3530013 4.4375796
    "2/6/09"  17934   .3530013  4.465678
    "2/9/09"  17937   .3530013 4.4670568
    "2/10/09" 17938   .1988509  4.420165
    "2/11/09" 17939   .3530013 4.4260435
    "2/12/09" 17940   .3530013  4.426761
    "2/13/09" 17941    .604316  4.415945
    "2/16/09" 17944   .4865331         .
    "2/17/09" 17945 .016529638 4.3722286
    "2/18/09" 17946   .4865331 4.3698277
    "2/19/09" 17947   .1988509  4.359014
    "2/20/09" 17948   .1988509  4.349245
    "2/23/09" 17951   .1988509 4.3128104
    "2/24/09" 17952   .1988509   4.35002
    "2/25/09" 17953   .1988509  4.342116
    "2/26/09" 17954   .1988509  4.325721
    "2/27/09" 17955 .016529638 4.3031187
    "3/2/09"  17958  -.2066147   4.25703
    "3/3/09"  17959   .1988509 4.2494946
    "3/4/09"  17960 .016529638  4.272909
    end
    format %td edate
    tsset edate
    tsline spy_growth insys_growth if tin(1feb2009, 27feb2009)
    gives the idea.

    Why round the data? At most, use a %3.2f display format.

    Comment


    • #3
      Hi Nick,
      Thank you so much for your help. I'm sorry I don't know how dataex works very well- still very new to Stata. Didn't mean to make it difficult. Really appreciate your help!

      Comment

      Working...
      X