Announcement

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

  • tsfilter on multiple variables

    Dear Statalisters,

    I'm trying to run an HP filter on a few time series and I keep getting an error when Stata tries to generate the new variables. I've tried different specifications of the command but nothing seems to work.

    So, suppose I have yearly observations on a number of companies and 3 (existing) time series variables, var1 var2 var3. I've declared my dataset as being a panel, using xtset.
    I want to run an HP filter on all 3 series, and save both the cyclical and the trend component as new variables. Also, I need to change the smoothing parameter to 100, say.

    What's the correct syntax for this? I tried different specifications and I keep getting a "variable xxxx not found" error, where xxxx should be the name of the first NEW variable that Stata itself should generate when filtering the existing series - that is, the first element of "newvarlist" in the documentation: http://www.stata.com/manuals13/tstsfilterhp.pdf

    Thank you very much for your help





  • #2
    The syntax is quite simple. Unless you show exact code and output it's difficult to assess your problem. A working example with two variables:

    Code:
    *clear all
    set more off
    
    webuse gdp2
    
    * make sure data is -tsset-
    tsset
    
    * create a second fake variable
    gen var2 = gdp_ln
    
    * filter (creates cyclical and trend components)
    tsfilter hp gdp_hp var2_hp = gdp_ln var2, smooth(100) trend(gdp_tr var2_tr)
    See help tsfilter hp.

    Please use your full real name in the forum. You are asked to do this in the Advice Guide, within the FAQ (and when you register).
    Last edited by Roberto Ferrer; 14 May 2014, 19:05.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Hi and thanks for your prompt reply.
      Your example works fine. When I try to adapt it to my dataset, though, I get an error message. See here below for the relevant part of the code
      (again, c_code is a company ID and yyear is the observation year. var1 and var2 are the balance sheet data I'm interested in)

      Apologies, but I don't feel comfortable using my full name. The FAQ specify it's not compulsory, so I take mine as a "valid" choice. Hope no forum user takes offense, I just don't want my name to show up in search engine results.



      Code:
      xtset c_code yyear, yearly panel variable: c_code (unbalanced) time variable: yyear, 1986 to 2011, but with a gap delta: 1 year tsfilter hp gdp_hp var2_hp = var1 var2, smooth(100) trend(gdp_tr var2_tr) note: no observations for panel 1 note: no observations for panel 2 note: no observations for panel 3 note: no observations for panel 4 variable gdp_tr not found r(111);

      The problem doesn't seem to be related to the trend() part either:


      Code:
      . tsfilter hp gdp_hp var2_hp = var1 var2, smooth(100) // trend(gdp_tr var2_tr)
      note: no observations for panel 1
      note: no observations for panel 2
      note: no observations for panel 3
      note: no observations for panel 4
      variable gdp_hp not found
                 st_varindex():  3500  invalid Stata variable name
          _TSFILTER_hpFilter():     -  function returned error
                       <istmt>:     -  function returned error
      r(3500);

      Comment


      • #4
        No offence or offense taken, but you think having a little difficulty with Stata syntax will harm your public image? That's extraordinary. There is a little freedom on both sides here, the freedom not to comply, and the freedom to try to persuade you otherwise.

        In terms of your question:

        gdp_hp var2_hp should be new variable names
        var1 var2
        should be existing variable names
        gdp_tr var2_tr should be new variable names

        The existing variables should contain non-missing values.

        It is really difficult to see that different error messages should arise with the same syntax but put on one line or two.

        I'd check that your Stata is fully up-to-date as regards both executable and ado-files. I suspect something bizarre here such as a corrupted Stata. You may need to contact Stata tech support if no better solution is offered.



        Last edited by Nick Cox; 15 May 2014, 02:22.

        Comment


        • #5
          Thanks, Nick. I'm using Stata from a university server, so I would be inclined to think it is fully up to date. I might ask the IT people, though.

          Some variables do indeed have quite a few missing values - no variable is completely "empty", though, as far as I know. I'm guessing the missing values are what's creating problems, though, it's the most likely explanation.

          Also note that I'm not getting different error messages by splitting the code onto two lines (///), but by inserting or cutting out an option (//). Or at least that's what I wanted to do. Am I wrong?

          Finally, I totally agree with you when you say:

          There is a little freedom on both sides here, the freedom not to comply, and the freedom to try to persuade you otherwise.
          I have no problems with people asking me to change my policy. I hope you can understand people might have different sensitivities when it comes to dealing with their digital footprint or "public image"

          Thanks again!

          Comment


          • #6
            LPP85,

            Can you post a complete example (using toy data) reproducing those errors?
            You should:

            1. Read the FAQ carefully.

            2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

            3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

            4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

            Comment


            • #7
              There was an update to -tsfilter- on 24 April. You should have gotten an error that the data is not balanced.
              I hacked a reproduce dataset. Below is the message you should have received.


              . tsset
              panel variable: c_code (unbalanced)
              time variable: yyear, 1986 to 2011, but with a gap
              delta: 1 year

              .
              . tsfilter hp gdp_hp var2_hp = var1 var2, smooth(100) trend(gdp_tr var2_tr)

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

              -Rich

              Comment


              • #8
                Also note that I'm not getting different error messages by splitting the code onto two lines (///), but by inserting or cutting out an option (//). Or at least that's what I wanted to do. Am I wrong?

                No; you're right on that detail.

                Comment

                Working...
                X