Announcement

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

  • Running standard deviation to date

    Hi all,

    First time post so bare with me if I've left out any critical details.
    I'm using STATA version 12.1 on Mac.

    I'm trying to calculate by ID, Season, and Phase the running standard deviation up to any given date.

    Example dummy data below. There is not a training session on every day hence the missing data and I only want to include dates where there was a session in the standard deviation calculation. If there is a change in ID, Season, or Phase I would like the standard deviation calculation to reset and start again.

    Example 1, for ID 1 on the 8/1/16 I would like the SD from 1/1/16 to the 7/1/16. Example 2, for ID 1 on the 17/1/16 I would like the SD from 1/1/16 to the 16/1/16.

    It's a time series dataset and therefore I set it accordingly:

    tsset ID Date
    tsfill

    Date ID Season Phase Distance
    1/1/16 1 2016 1 8000
    2/1/16 1 2016 1 .
    3/1/16 1 2016 1 4000
    4/1/16 1 2016 1 .
    5/1/16 1 2016 1 3000
    6/1/16 1 2016 1 .
    7/1/16 1 2016 1 .
    8/1/16 1 2016 1 2000
    9/1/16 1 2016 1 .
    10/1/16 1 2016 1 .
    11/1/16 1 2016 1 7000
    12/1/16 1 2016 1 .
    13/1/16 1 2016 1 .
    14/1/16 1 2016 1 9000
    15/1/16 1 2016 1 .
    16/1/16 1 2016 1 .
    17/1/16 1 2016 1 8000
    18/1/16 1 2016 1 .
    19/1/16 1 2016 1 4000
    20/1/16 1 2016 2 .
    21/1/16 1 2016 2 3000
    22/1/16 1 2016 2 .
    23/1/16 1 2016 2 .
    24/1/16 1 2016 2 2000
    25/1/16 1 2016 2 .
    26/1/16 1 2016 2 .
    27/1/16 1 2016 2 7000
    28/1/16 1 2016 2 .
    29/1/16 1 2016 2 .
    30/1/16 1 2016 2 7000
    31/1/16 1 2016 2 .
    1/2/16 1 2016 2 8000
    2/2/16 1 2016 2 .
    3/2/16 1 2016 2 .
    4/2/16 1 2016 2 9000
    5/2/16 1 2016 2 .
    1/1/16 2 2016 1 8000
    2/1/16 2 2016 1 .
    3/1/16 2 2016 1 4000
    4/1/16 2 2016 1 .
    5/1/16 2 2016 1 3000
    6/1/16 2 2016 1 .
    7/1/16 2 2016 1 .
    8/1/16 2 2016 1 2000
    9/1/16 2 2016 1 .
    10/1/16 2 2016 1 .
    11/1/16 2 2016 1 7000
    Any help on this would be greatly appreciated!


  • #2
    Please use dataex (SSC) to show examples, as outlined in the FAQ Advice #12. Dates are a particular pain, as is shown by the editing needed to produce an example that will run.

    Otherwise this appears to yield to rangestat (SSC): search the forum for mentions. -1000 is an arbitrarily large first argument.

    Code:
    clear 
    input str8 sDate    ID    Season    Phase    Distance
    "1/1/16"    1    2016    1    8000
    "2/1/16"    1    2016    1    .
    "3/1/16"    1    2016    1    4000
    "4/1/16"    1    2016    1    .
    "5/1/16"    1    2016    1    3000
    "6/1/16"    1    2016    1    .
    "7/1/16"    1    2016    1    .
    "8/1/16"    1    2016    1    2000
    "9/1/16"    1    2016    1    .
    "10/1/16"    1    2016    1    .
    "11/1/16"    1    2016    1    7000
    "12/1/16"    1    2016    1    .
    "13/1/16"    1    2016    1    .
    "14/1/16"    1    2016    1    9000
    "15/1/16"    1    2016    1    .
    "16/1/16"    1    2016    1    .
    "17/1/16"    1    2016    1    8000
    "18/1/16"    1    2016    1    .
    "19/1/16"    1    2016    1    4000
    "20/1/16"    1    2016    2    .
    "21/1/16"    1    2016    2    3000
    "22/1/16"    1    2016    2    .
    "23/1/16"    1    2016    2    .
    "24/1/16"    1    2016    2    2000
    "25/1/16"    1    2016    2    .
    "26/1/16"    1    2016    2    .
    "27/1/16"    1    2016    2    7000
    "28/1/16"    1    2016    2    .
    "29/1/16"    1    2016    2    .
    "30/1/16"    1    2016    2    7000
    "31/1/16"    1    2016    2    .
    "1/2/16"    1    2016    2    8000
    "2/2/16"    1    2016    2    .
    "3/2/16"    1    2016    2    .
    "4/2/16"    1    2016    2    9000
    "5/2/16"    1    2016    2    .
    "1/1/16"    2    2016    1    8000
    "2/1/16"    2    2016    1    .
    "3/1/16"    2    2016    1    4000
    "4/1/16"    2    2016    1    .
    "5/1/16"    2    2016    1    3000
    "6/1/16"    2    2016    1    .
    "7/1/16"    2    2016    1    .
    "8/1/16"    2    2016    1    2000
    "9/1/16"    2    2016    1    .
    "10/1/16"    2    2016    1    .
    "11/1/16"    2    2016    1    7000
    end 
    gen Date = daily(sDate, "DMY", 2050) 
    
    rangestat (sd) Distance, interval(Date -1000 -1) by(ID Season Phase)

    Comment


    • #3
      Nick, you're a great man. Thank you very much!

      Comment

      Working...
      X