Announcement

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

  • Using the rolling command

    Hello,

    I have an unbalanced panel dataset where panel variable is denoted by firmid and time variable is year. I want to calculate a new variable varsalest defined as the variance of total sales (variable name is sale) in the past 5 years (t-5 to t-1) .

    Going through the previous posts here, I learned to use the rolling command as below-

    xtset firmid year
    rolling varsales=r(Var), window(5) stepsize(5) : summarize sale, detail

    I want to make sure of two things here-
    1) If there are one or more missing values in the 5-year window, then the variance is calculated from the remaining values.
    2) This calculation is done grouped by firm. But the 'by' prefix does not work with rolling command. How can I specify the calculation to be done by each firm?

    Thanks.

  • #2
    (1) is correct.
    (2) -rolling- looks at the -xtset- command and automatically groups by the panel variable.

    I'm not sure your command does what you describe. Because you specified -stepsize(5)-, your command will start at the first year (let's just say it's 1990 to illustrate) and calculate the variance for years 1990-1994, inclusive. Then it will advance the window to 1995 and calculate the variance for years 1995-1999, and then 2000-2004, etc. It will not calculate the variance for years 1991-1995, 1992-1996, 1993-1997, or 1994-1998. I suppose this is consistent with what you said, but I thought I should point out that if you want the results for these intermediate 5 year periods as well, you need to remove the -stepsize()- option from your command.

    All of that said, you might find that you get faster execution with either -tsegen- or -rangestat-. Both of these are user-written commands available from SSC.

    Comment


    • #3
      Thank you Clyde for your response.

      To elaborate on the stepsize() option, I was not exactly sure of the usage even after reading the help file but this is what I wanted - so for the year 1996, the variance should be calculated from last 5 years i.e. 1991-1995 & saved in the new variable varsales. For the year 1997, it should be from 1992-1996 and so on.
      Based on your response, I understand that this is not what stepsize() is doing. So I shall remove it.

      There is one more thing. So my dataset has 30 other variables that I want to preserve as-is. I am only running rolling command to calculate one single variable. But after doing so, all other variables are gone! and I am only left with firmid, start, end, and varsales-just 4 variables.

      I am also going to check out the 2 commands you mentioned because -rolling- is very time consuming. Thank you!

      Comment


      • #4
        Here's an example you can run:

        Code:
        capture ssc inst tsegen 
        
        webuse grunfeld, clear 
        
        tsset 
        tsegen sd = rowsd(L(1/5).invest) 
        list invest sd, sepby(company)
        And naturally you can square the SD to get the variance.

        Comment


        • #5
          Thank you, Nick for the example.

          Comment

          Working...
          X