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

  • HP filter in panel data


    I am working with panel data and I'd like to apply the Hodrick Prescott filter on a variable (v2x_polyarchy) for each country.
    I have tried the following:

    encode country_text_id, gen(countries)
    xtset countries year
    tsfilter hp cycle_dem = v2x_polyarchy, smooth(400) trend(hptrend_dem)

    But it runs only for a couple of countries and this problem emerges:

    Number of gaps in sample: 1 (gap count includes panel changes)
    sample may not contain gaps

    I have also tried to run a loop and that for each country the filter will be applied generating a different variable with each cycle.

    encode country_text_id, gen(countries)
    xtset countries year

    foreach i in countries {
    tsfilter hp cycle_dem_`i' = v2x_polyarchy, smooth(400) trend(hptrend_dem_`i')

    But now the problem remains the same and only one variable is created (with the name cycle_dem_countries) instead of one for each country's cycle.

    Could anyone please tell me what am I doing wrong?

    Thnk you all
    Last edited by Angel Muniz; 06 Dec 2019, 11:15.

  • #2
    You're imagining, I guess, that the loop

    foreach i in countries
    somehow ensures that Stata loops over the distinct values of the variable countries. But that's not the way that the loop works. Your loop is a loop over a single item, the text "countries", and so boils down to a single statement

    tsfilter hp cycle_dem_countries = v2x_polyarchy, smooth(400) trend(hp_trend_dem_countries)

    which is legal, but doesn't do anything different.

    Otherwise, the problem is as reported. If gaps are fatal, gaps are fatal. You should consider interpolation, or move on.


    • #3
      If that is not how the loop works, how should I write it?

      I'd like the hp filter to run for each country and generate a new variable (cycle_dem_i) with the i of each country

      thank you for the answer


      • #4
        The command automatically treats panels separately. A loop won’t solve the problem of gaps, which needs a different solution, namely interpolation first.