Announcement

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

  • One-sided HP filter

    Dear Statalisters,

    I post the code for One-sided HP filter which is particularly easy to implement using the stock tsfilter command.
    The main idea is that HP filter is the smoother which employs all information t=1..T, i.e. f(y | T) while one-sided HP filter is filtering up to time t, i.e. f(y | t). I replicate the matlab code by Alexander Meyer-Gohde and see there is an identical result.

    We just need to loop from time t=3 to T since we would loss the first two obs due to its lags. Note that, Stock and Watson (1999) defined the one-sided HP filter as follows: y(t) = tau(t) + epsilon(t); (1 - L)^2*tau(t) = eta(t).

    There is an equivalent Kalman version of one-sided HP filter in Stata elsewhere in the forum, but it seems that the snippet code below is easy to read and apply. To use it just replace "series..." by your actual series name.

    Code:
    capture drop series_1scycle
    local start  3
    local end   _N
    forvalues time = `start'/`end' {
            qui tsfilter series_1scycle = series_original in 1/`time', smooth(`lambda')
            qui replace series_1scycle = series_original in `time'
    }
    generate series_1strend = series_original - series_1scycle
    PS: I am not sure the code can produce the exact result of the one-sided HP filter as suggested in Stock and Watson (1999). But you can test with matlab code (serial version) by Alexander Meyer-Gohde and compare two results.

  • #2
    I had a student trying to do this with panel data and I wrote a kludge. I'm pretty sure there is a better way but I wanted to put this out there incase anyone in the future is looking to do this


    Code:
    *one sided filter
        sum id
        local max_id= `r(max)'
        
        *loop through countries
        forvalues i=1/`max_id'{
            disp in red "id = `i'"
            
            preserve
                *keep individual country
                keep if id==`i'
                qui describe
                *if country has observations
                if `r(N)'>0{
                    
                    *code from stata list
                    local start  8
                    local end  = _N
                    *add lambdas here
                    local lambdas 400000 1600
                    
                    foreach lambda in `lambdas'{
                                
                        forvalues time = `start'/`end' {
                            capture tsfilter hp sp_index_1scycle = sp_index in 1/`time', smooth(`lambda')
                            capture replace sp_index_1scycle = sp_index in `time'
                        }
                        generate sp_index_1strend_`lambda' = sp_index - sp_index_1scycle        
                    }
                    keep id date sp_index_1strend*
                    if i!=1 append using data/temp_tstresnd.dta
                    save data/temp_tstresnd.dta, replace
                }
            
            restore
        
        }
    Owner of StataTutor.com

    Comment


    • #3
      Code:
       
       if i!=1 append using data/temp_tstresnd.dta
      should be

      Code:
       
       if `i' != 1 append using data/temp_tstresnd.dta
      If this code worked before, there was a fortunate accident with a variable named (or abbreviating to)
      Code:
       i
      in the original dataset. Or you had the code right in the original, but it's reported differently here.

      Comment


      • #4
        Thank you. I’m not sure how that happened.
        Owner of StataTutor.com

        Comment


        • #5
          Trivial, but rather than

          Code:
          sum id
          local max_id = `r(max)'      
          
          forvalues i = 1/`max_id' {

          this is a simpler way to start

          Code:
          sum id, meanonly    
          
          forvalues i = 1/`r(max)' {

          Comment

          Working...
          X