Announcement

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

  • Count consecutive observation as cumulative sum

    Hi,

    I have the following dataset:
    Code:
     
    USREC myear
    1 2001m4
    1 2001m5
    1 2001m6
    1 2001m7
    1 2001m8
    1 2001m9
    1 2001m10
    1 2001m11
    0 2001m12
    0 2002m1
    0 2002m2
    0 2002m3
    0 2002m4
    0 2002m5
    1 2002m6
    1 2002m7
    1 2002m8
    1 2002m9
    0 2002m10
    0 2002m11
    1 2002m12
    1 2003m1
    1 2003m2
    0 2003m3
    0 2003m4
    I would like to count the consecutive "USREC"=="1", reinitialize the counter again when "0", not count the "0"s and start the counter over again when a new series a "1" starts. Manually, it looks as follow:

    Code:
     
    USREC myear rec_month
    1 2001m4 1
    1 2001m5 2
    1 2001m6 3
    1 2001m7 4
    1 2001m8 5
    1 2001m9 6
    1 2001m10 7
    1 2001m11 8
    0 2001m12 0
    0 2002m1 0
    0 2002m2 0
    0 2002m3 0
    0 2002m4 0
    0 2002m5 0
    1 2002m6 1
    1 2002m7 2
    1 2002m8 3
    1 2002m9 4
    0 2002m10 0
    0 2002m11 0
    1 2002m12 1
    1 2003m1 2
    1 2003m2 3
    0 2003m3 0
    0 2003m4 0
    I'm not sure how to program that. I have tried using "_n", unsuccessfully. Can someone help? Thanks!




  • #2
    Have a look at Nick Cox's tsspell on SSC. I think it has the functionality you seek.

    Comment


    • #3
      With tsspell (SSC) that variable comes for free as _seq when you go (e.g.)

      Code:
      tsset myear
      tsspell, pcond(USREC)
      If you are interested in spells, however called, that program may have other uses.

      There are also various ways to get it directly. Here's one:

      Code:
      gen wanted = USREC in 1
      replace wanted = cond(USREC, wanted[_n-1] + 1, 0) in 2/L
      See also

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0029 (free to all)

      for a detailed discussion of principles here.

      In the cond() code, a moment's thought will show that USREC will do instead of USREC == 1 as evaluating USREC == 1 returns 1 or 0 precisely when evaluating USREC does. (If there are missings on USREC, you need something different, but it has the flavour of something always known.)
      Last edited by Nick Cox; 06 Oct 2015, 02:47.

      Comment


      • #4
        Thanks a lot Nick &Stephen!

        Comment


        • #5
          I have a problem with my data
          variables:
          tmax:Maximum temperature
          date: date of event

          And I wanted to identify number of events and duration of each event.
          Each event is identified when 2 following conditions are satisfied:
          (1) tmax is equal 36.4 or more
          (2) number of consecutive days of each event is 3 days or more
          start date of the research is 1Jan2005


          I used codes as below:
          gen time=td(1jan2005)+date-1
          tsspell time, cond(tmax>=36.4)
          list _spell _seq date if tmax>=36.4


          And I got results like this:

          +---------------------------+
          | _spell _seq date |
          |---------------------------|
          75. | 1 1 16mar2005 |
          78. | 2 1 19mar2005 |
          79. | 2 2 20mar2005 |
          83. | 3 1 24mar2005 |
          93. | 4 1 03apr2005 |
          |---------------------------|
          94. | 4 2 04apr2005 |
          95. | 4 3 05apr2005 |
          102. | 5 1 12apr2005 |
          104. | 6 1 14apr2005 |
          109. | 7 1 19apr2005 |
          |---------------------------|
          110. | 7 2 20apr2005 |
          111. | 7 3 21apr2005 |


          The problem here is Stata only determines each spell based on threshold of tmax, and not include number of consecutive days at least 3 days.

          How can I make it right?
          How can I generate a variable 'dur' as a number of consecutive days for each event.
          How can I generate a variable as number of events weekly, monthly?

          Thank you in advance!

          Comment


          • #6
            Stata just does what you tell it. The help for tsspell, which you use here but do not cite (SSC), does discuss two-stage identification of spells and gives a worked example. In your case the code might be

            Code:
            tsspell time, cond(tmax>=36.4)
            egen dur = max(_seq), by(_spell) 
            drop _seq _spell _end 
            tsspell time, cond(tmax >= 36.4 & dur >= 3)
            expect that any structure of different places calls for modifications.

            The rest is counting using egen if I understand correctly. My own bias is that weeks and months are usually artificial time units in climatology.

            Comment


            • #7
              Thank you very much Nick Cox.
              But when i use command egen nohw = count(_spell) if _spell!=0
              It makes wrong because it count each _spell many times.
              for example: for _spell #1 and duration of _spell #1 is 3 days, it determines _spell #1 as 3 different spells.
              Can you help me!

              Comment


              • #8
                Don't do that then. The number of spells is the same as the number of observations with _seq == 1 or _end == 1.

                Or it is the maximum value of _spell.

                Comment


                • #9
                  Ok. I figure it out. Thank you so much Nick Cox! Have a nice day ^^

                  Comment


                  • #10
                    How do I create a variable as maximum value of temperature for each spell. Thank you in advance Nick!

                    Comment


                    • #11
                      Finally, I got it, thanks!

                      Comment


                      • #12
                        I have a data of daily maximum temperature (tmax), and cases by month. How do i create a variable (mean_tmax_m) as "Mean Maximum Temperature (by month)" in order to link it with cases. Thanks for your help!

                        Comment


                        • #13
                          These new questions are unrelated to the present thread. Please start a new thread for your next different question. Otherwise

                          Code:
                          help egen

                          Comment

                          Working...
                          X