Announcement

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

  • How to do the second differencing in Stata?

    Consider the following long-form panel data example:
    Code:
    mata:
    pid    = (1::100) # J(3, 1, 1)
    period = J(100, 1, 1::3)
    Y      = rnormal(300, 1, 0, 1)
    X      = rnormal(300, 1, 0, 1)
    end
    getmata pid period Y X
    xtset pid period
    Here, I want to regress Y[t] - Y[t-2] on X[t] - X[t-2] (I think, the meaning of Y[t] is clear)
    That is, I need to conduct the second difference.

    At the first time, I tried to use the D. operator like below:
    Code:
    reg D2.(Y X)
    But, you know, it is not what I want, because D2.Y is D.(D.Y) = Y[t] - 2Y[t-1] + Y[t-2].
    So, is there any way to easily conduct the second (and higher order) difference in Stata?

  • #2
    Code:
    gen d2y = y - l2.y
    gen d2x = x - l2.x
    reg d2y d2x
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis Thank you for your comment. Of course, we can manually generate the second differenced variables. But, in fact, what I want to do is to run a regression without generating new variables like below:
      Code:
      reg G.Y G.X
      , where G. is the imaginary second differencing operator. Can we do this in Stata?

      Comment


      • #4
        Why not try it?

        Comment


        • #5
          Nick Cox In my real problem, I have to generate too many variables
          Code:
          gen d2Y = Y - l2.Y
          gen d3Y = Y - l3.Y
          ...
          gen d10Y = Y - l10.Y
          
          gen d2X = X - l2.X
          gen d3X = X - l3.X
          ...
          gen d10X = X - l10.X
          So, I want to just run the regression more compactly (specifically, I will run the regression with Mata's _stata command, so I want more compact one)
          Code:
          sureg (G2.Y G2.X) (G3.Y G3.X) ... (G10.Y G10.X)
          where G. is the imaginary differencing operator.

          Of course, Maarten Buis 's solution is perfect, but I am wondering whether there is a more compact way.

          Comment


          • #6
            You changed the question from regress to sureg, but no matter: The information you need is there in the help for sureg

            depvar and the varlists may contain time-series operators; see tsvarlist.
            But that doesn't extend to expressions involving two or more variables and/or two or more operators.

            Comment


            • #7
              Nick Cox Right, by simplifying the question I changed the question. I did not know the tsvarlist (may be time-series variable list). I will check the help file. Thank you so much.

              Comment

              Working...
              X