Announcement

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

  • How to use hp filter on different groups

    Hi,

    I have GDP data for each state from the last 10 years, and I want to use hp filter on each state's time series, suppose "state" is the variable for state name, and "year" is the time, is there a concise way to use "tsfilter hp" command to detrend each state's time series?

    Thanks!
    Last edited by John Zhao; 23 Mar 2019, 20:39.

  • #2
    From what you've stated, it sounds like you have your data in a long format. You can either reshape your data or create a tempfile and loop through each state.

    For example (something along these lines, I can't verify w/o seeing your data)

    Code:
    tempfile temp
    
    egen id = group(state)
    local end = id[_N]
    save `temp'
    forvalues x = 1/`end' {
         use `temp' , clear
         keep if id == `x'
         tsset year
         tsfilter hp state_hp = state_gdp
         tempfile _`x'
         save `_`x''
    }
    clear
    forvalues x = 1/`end' {
          append using `_`x''
    }
    Last edited by Justin Niakamal; 23 Mar 2019, 21:59.

    Comment


    • #3
      Disclaimer: I don't use and know little about -tsfilter-, so what I write here is predicated on the correctness of what Justin Blasongame wrote in #2. Assuming that is right, the code can be considerably simplified:

      Code:
      capture program drop one_state
      program define one_state
          tsset year
          tsfilter hp state_hp = stage_gdp
          exit
      end
      
      runby one_state, by(state)
      -runby- is written by Robert Picard and me, and is available from SSC.

      Comment


      • #4
        Yes, you are right, it's long format, and I found that by using
        Code:
        encode stabr, gen(state)
        tsset state year
        tsfilter hp stateGDP_hp=stateGDP
        the hp filter works autimatically by state and year, please correct me if I'm wrong.



        Click image for larger version

Name:	Screen Shot 2019-03-24 at 11.34.14 AM.png
Views:	1
Size:	181.0 KB
ID:	1489808

        Comment

        Working...
        X