Announcement

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

  • tsfilter hp gaps in sample

    I have a balanced panel (panel of 216 countries, time is months from Jan 1970 to Dec 2017). A sample of my data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 CountryName float(country_id date year month gdp_ln)
    "Afghanistan" 4 120 1970  1 21.282246
    "Afghanistan" 4 121 1970  2 21.282246
    "Afghanistan" 4 122 1970  3 21.282246
    "Afghanistan" 4 123 1970  4 21.282246
    "Afghanistan" 4 124 1970  5 21.282246
    "Afghanistan" 4 125 1970  6 21.282246
    "Afghanistan" 4 126 1970  7 21.282246
    "Afghanistan" 4 127 1970  8 21.282246
    "Afghanistan" 4 128 1970  9 21.282246
    "Afghanistan" 4 129 1970 10 21.282246
    "Afghanistan" 4 130 1970 11 21.282246
    "Afghanistan" 4 131 1970 12 21.282246
    "Afghanistan" 4 132 1971  1  21.32819
    "Afghanistan" 4 133 1971  2  21.32819
    "Afghanistan" 4 134 1971  3  21.32819
    "Afghanistan" 4 135 1971  4  21.32819
    "Afghanistan" 4 136 1971  5  21.32819
    "Afghanistan" 4 137 1971  6  21.32819
    "Afghanistan" 4 138 1971  7  21.32819
    "Afghanistan" 4 139 1971  8  21.32819
    end
    format %tm date
    label values country_id c_all
    label def c_all 4 "Afghanistan", modify
    The exercise is quite simple. I am trying to decompose my series on GDP into trend and cyclical component using an HP filter. However, when I try to use the HP filter I get:

    Code:
    tsfilter hp gdp_hp = gdp_ln
    
    Number of gaps in sample:  1   (gap count includes panel changes)
    sample may not contain gaps
    r(498);
    There seems to be no problem with the panel structure. Furthermore, tsfilter hp should be able to handle panel structures and not just time series. Some description of my panel structure:

    Code:
     
     xtset country_id date
           panel variable:  country_id (strongly balanced)
            time variable:  date, 1970m1 to 2018m12
                    delta:  1 month
    
    xtdescribe
    
    country_id:  4, 8, ..., 999                                  n =        216
        date:  1970m1, 1970m2, ..., 2018m12                      T =        588
               Delta(date) = 1 month
               Span(date)  = 588 periods
               (country_id*date uniquely identifies each observation)
    
    Distribution of T_i:   min      5%     25%       50%       75%     95%     max
                           588     588     588       588       588     588     588
    
         Freq.  Percent    Cum. |  Pattern*
     ---------------------------+---------------------------------------------------------------------------------------------------
    > ---------------------
          216    100.00  100.00 |  5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
    > 555555555555555555553
     ---------------------------+---------------------------------------------------------------------------------------------------
    > ---------------------
          216    100.00         |  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    > XXXXXXXXXXXXXXXXXXXXX
     -------------------------------------------------------------------------------------------------------------------------------
    > ---------------------
     *Each column represents 5 periods.
    Some further points: my time variable is in %tm format, generated using the monthly() function. Some countries have missing values of gdp_ln, but no country has ALL missing values.
    I am stumped as to what the gap in sample refers to.

  • #2
    I can't reproduce this behaviror.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 CountryName float(country_id date year month gdp_ln)
    "Afghanistan" 4 120 1970  1 21.282246
    "Afghanistan" 4 121 1970  2 21.282246
    "Afghanistan" 4 122 1970  3 21.282246
    "Afghanistan" 4 123 1970  4 21.282246
    "Afghanistan" 4 124 1970  5 21.282246
    "Afghanistan" 4 125 1970  6 21.282246
    "Afghanistan" 4 126 1970  7 21.282246
    "Afghanistan" 4 127 1970  8 21.282246
    "Afghanistan" 4 128 1970  9 21.282246
    "Afghanistan" 4 129 1970 10 21.282246
    "Afghanistan" 4 130 1970 11 21.282246
    "Afghanistan" 4 131 1970 12 21.282246
    "Afghanistan" 4 132 1971  1  21.32819
    "Afghanistan" 4 133 1971  2  21.32819
    "Afghanistan" 4 134 1971  3  21.32819
    "Afghanistan" 4 135 1971  4  21.32819
    "Afghanistan" 4 136 1971  5  21.32819
    "Afghanistan" 4 137 1971  6  21.32819
    "Afghanistan" 4 138 1971  7  21.32819
    "Afghanistan" 4 139 1971  8  21.32819
    end
    format %tm date
    label values country_id c_all
    label def c_all 4 "Afghanistan", modify
    
    xtset country_id date
    tsfilter hp gdp_hp = gdp_ln
    
    list
         +---------------------------------------------------------------------------+
         | CountryName    country_id      date   year   month     gdp_ln      gdp_hp |
         |---------------------------------------------------------------------------|
      1. | Afghanistan   Afghanistan    1970m1   1970       1   21.28225     .013101 |
      2. | Afghanistan   Afghanistan    1970m2   1970       2   21.28225    .0097919 |
      3. | Afghanistan   Afghanistan    1970m3   1970       3   21.28225    .0064827 |
      4. | Afghanistan   Afghanistan    1970m4   1970       4   21.28225    .0031733 |
      5. | Afghanistan   Afghanistan    1970m5   1970       5   21.28225   -.0001367 |
         |---------------------------------------------------------------------------|
      6. | Afghanistan   Afghanistan    1970m6   1970       6   21.28225   -.0034475 |
      7. | Afghanistan   Afghanistan    1970m7   1970       7   21.28225   -.0067592 |
      8. | Afghanistan   Afghanistan    1970m8   1970       8   21.28225   -.0100722 |
      9. | Afghanistan   Afghanistan    1970m9   1970       9   21.28225   -.0133866 |
     10. | Afghanistan   Afghanistan   1970m10   1970      10   21.28225   -.0167024 |
         |---------------------------------------------------------------------------|
     11. | Afghanistan   Afghanistan   1970m11   1970      11   21.28225   -.0200198 |
     12. | Afghanistan   Afghanistan   1970m12   1970      12   21.28225   -.0233385 |
     13. | Afghanistan   Afghanistan    1971m1   1971       1   21.32819     .019286 |
     14. | Afghanistan   Afghanistan    1971m2   1971       2   21.32819    .0159657 |
     15. | Afghanistan   Afghanistan    1971m3   1971       3   21.32819    .0126451 |
         |---------------------------------------------------------------------------|
     16. | Afghanistan   Afghanistan    1971m4   1971       4   21.32819    .0093245 |
     17. | Afghanistan   Afghanistan    1971m5   1971       5   21.32819    .0060039 |
     18. | Afghanistan   Afghanistan    1971m6   1971       6   21.32819    .0026834 |
     19. | Afghanistan   Afghanistan    1971m7   1971       7   21.32819   -.0006371 |
     20. | Afghanistan   Afghanistan    1971m8   1971       8   21.32819   -.0039575 |
         +---------------------------------------------------------------------------+
    
    . 
    .
    Last edited by Justin Niakamal; 26 Nov 2020, 08:07.

    Comment


    • #3
      Hello Justin Blasongame , thank you for your reply.

      Could it be that because it seems to work on this small sample of 20 observations, and furthermore this sample is not a real panel because it only has Afghanistan, whereas my data has 216 countries, that the problem is with the panel identifier (country_id)?

      Or, alternatively, is it more likely that some other country in my full data has too many missings and somehow it reads this as an error "gaps in sample"?

      Comment


      • #4
        I couldn't reproduce this either. What version of Stata are you using? (You are assumed to be using the latest version and if not you're asked to say which version you're using.)


        I think I recall that if you go back far enough hpfilter didn't support panel data. I don't see the error message in #1 as part of the code in Stata 16, but I might be looking in the wrong place.

        Comment


        • #5
          Hello Nick Cox. I am using Stata 16.1. Also, I am not using hpfilter but tsfilter hp, which supports panel data.

          I have found the problem. Based on the fact that both of you were able to run it using this small subsample, I figured it had to do with missing data (the sample I posted has no values missing for gdp_ln). As I said before, my full data has some missing values, and when I imputed this problem disappeared. I saw in the user guide that missing values at the beginning and end of the series are excluded. I do not know how sensitive the command is to missing values in general. Anyways, when I replaced all the missing values it worked with the full panel data set.

          Comment

          Working...
          X