Announcement

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

  • Generate NBER recession dummy

    Dear All,

    Suppose that I have time series from 1857m1 to 2017m12 as follows.
    Code:
    clear
    set obs 1932
    gen t = m(1857m1)+_n-1
    format t %tm
    gen recession = 0
    I'd like to replace recession with 1 as long as the months belong to recession defined by NBER, i.e., between nber1 and nber2 below.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(nber1 nber2)
    -1231 -1213
    -1191 -1183
    -1137 -1105
    -1087 -1069
    -1035  -970
     -934  -896
     -874  -861
     -834  -824
     -804  -787
     -769  -751
     -727  -709
     -688  -665
     -632  -619
     -600  -576
     -564  -541
     -497  -490
     -480  -462
     -440  -426
     -399  -386
     -366  -322
     -272  -259
     -179  -171
     -134  -123
      -78   -68
      -29   -21
        3    13
      119   130
      166   182
      240   246
      258   274
      366   374
      494   502
      575   593
    end
    format %tm nber1
    format %tm nber2
    Any suggestions?
    Last edited by River Huang; 13 Jul 2017, 02:13.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    River:
    assuming you do not have missing values, I would try:
    Code:
    replace recession=1 if t> nber1 & t<= nber2
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      River:
      assuming you do not have missing values, I would try:
      Code:
      replace recession=1 if t> nber1 & t<= nber2
      Carlo, thanks for your reply. The problem is that they are in two different files.

      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        River:
        perhaps what follows seems trivial, but: can't you -merge- them somehow?
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Not so intuitive to me, but I am trying.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Thanks to your suggestion, I think I figure it out as follows:
            Code:
            // 1
            clear 
            set obs 1932
            gen t = m(1857m1)+_n-1
            format t %tm
            
            gen recession = 0
            save "nber-1.dta", replace
            
            // 2
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input long(nber1 nber2)
            -1231 -1213
            -1191 -1183
            -1137 -1105
            -1087 -1069
            -1035  -970
             -934  -896
             -874  -861
             -834  -824
             -804  -787
             -769  -751
             -727  -709
             -688  -665
             -632  -619
             -600  -576
             -564  -541
             -497  -490
             -480  -462
             -440  -426
             -399  -386
             -366  -322
             -272  -259
             -179  -171
             -134  -123
              -78   -68
              -29   -21
                3    13
              119   130
              166   182
              240   246
              258   274
              366   374
              494   502
              575   593
            end
            format %tm nber1
            format %tm nber2
            
            expand nber2 - nber1 + 1
            gen t = nber1
            bys t: replace t = t[_n-1] + 1 if _n > 1
            format t %tm 
            gen recession = 1
            save "nber-2.dta", replace
            
            // merge 1 and 2
            use "nber-1.dta", clear
            merge 1:1 t using "nber-2.dta", replace update
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment


            • #7
              I'll let merge meisters solve this one way. (In fact, while I was writing this, River figured it out.)

              This is just to point out that you can have different datasets in the same file so long as you respect lack of alignment. (Carlo's solution in #2 won't work because the data are not aligned.)

              There are fewer recessions than months, so we can put the second dataset in with the first and then loop over pairs of points.

              Code:
              clear
              set obs 1932
              gen t = m(1857m1)+_n-1
              format t %tm
              gen recession = 0
              
              input long(nber1 nber2)
              -1231 -1213
              -1191 -1183
              -1137 -1105
              -1087 -1069
              -1035  -970
               -934  -896
               -874  -861
               -834  -824
               -804  -787
               -769  -751
               -727  -709
               -688  -665
               -632  -619
               -600  -576
               -564  -541
               -497  -490
               -480  -462
               -440  -426
               -399  -386
               -366  -322
               -272  -259
               -179  -171
               -134  -123
                -78   -68
                -29   -21
                  3    13
                119   130
                166   182
                240   246
                258   274
                366   374
                494   502
                575   593
              end
              
              count if nber1 < .
              
              quietly forval i = 1/`r(N)' {
                  replace recession = 1 if inlist(t, nber1[`i'], nber2[`i'])
              }
              
              set scheme s1color
              gen slice = _n > _N/2
              scatter recession t, yla(0 1, ang(h)) ms(o) by(slice, note("") xrescale col(1)) ///
              subtitle("", fcolor(none))
              Click image for larger version

Name:	recession.png
Views:	1
Size:	20.8 KB
ID:	1401774





              The recipe for slicing is perhaps easier to use than the old sliceplot written some years ago.

              SJ-6-3 gr0025 . . . . . . . . . . . . Speaking Stata: Graphs for all seasons
              (help cycleplot, sliceplot if installed) . . . . . . . . . N. J. Cox
              Q3/06 SJ 6(3):397--419
              illustrates producing graphs showing time-series seasonality

              The generalisation to slice = ceil(k *_n/_N) to get k slices is immediate. Notice that we don't need to label the slices or the fact of slicing so both the note and the subtitle get zapped.

              Comment


              • #8
                I think that Nick meant to use inrange() instead of inlist() to identify recession months.

                Here's yet another way to get there, using rangejoin (from SSC):

                Code:
                clear
                set obs 1932
                gen t = m(1857m1)+_n-1
                format t %tm
                tempfile main
                save "`main'"
                
                clear
                input long(nber1 nber2)
                -1231 -1213
                -1191 -1183
                -1137 -1105
                -1087 -1069
                -1035  -970
                 -934  -896
                 -874  -861
                 -834  -824
                 -804  -787
                 -769  -751
                 -727  -709
                 -688  -665
                 -632  -619
                 -600  -576
                 -564  -541
                 -497  -490
                 -480  -462
                 -440  -426
                 -399  -386
                 -366  -322
                 -272  -259
                 -179  -171
                 -134  -123
                  -78   -68
                  -29   -21
                    3    13
                  119   130
                  166   182
                  240   246
                  258   274
                  366   374
                  494   502
                  575   593
                end
                format nber* %tm
                
                rangejoin t nber1 nber2 using "`main'"
                keep t
                
                merge 1:1 t using "`main'", keep(match using)
                gen recession = _merge == 3
                tsset t

                Comment


                • #9
                  inrange() is correct. Good catch.

                  Comment


                  • #10
                    Nick, thanks for your interesting suggestions.

                    Ho-Chuan (River) Huang
                    Stata 19.0, MP(4)

                    Comment


                    • #11
                      Robert, Thanks very much. It is not so intuitive to me but I will try to figure it out.

                      Ho-Chuan (River) Huang
                      Stata 19.0, MP(4)

                      Comment


                      • #12
                        Hi,

                        I also need a dummy variable. But in my case I just have years instead of months in my panel data file.

                        Is there a way to merge those "Recession" dates with years (1999-2016)? Respectively, match for each year if there was a recession. The problem would be, that some recessions are from May 2003 until Feb 2005 (just an example). In this case in 2003 and 2004 was a recession and in 2005 (since the recession only lasted 2 month in this year) was no recession.


                        I really appreciate your help

                        Juergen

                        Comment


                        • #13
                          Since you have only 8 years (I assume you are merging with a panel data), and you have to identify the recession `year' by yourself, why don't you try something like
                          Code:
                          gen recession = 0
                          replace recession = 1 if year == 2011 | year == 2012
                          Ho-Chuan (River) Huang
                          Stata 19.0, MP(4)

                          Comment

                          Working...
                          X