Announcement

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

  • Creating a macro with a polynomial series

    Hi everyone,

    The problem I have is the following: suppose x, y and z are data vectors (variables)

    I need to create a macro that contains the following polynomial series:

    (x + y + z) + (x + y + z)^2 + ... + (x + y + z)^4

    Right now, I'm literally generating every power k variable + each interaction by hand... Is there a more efficient way of doing this? I've been trying fp generate, without success, it doesn't seem to work for more than 1 variable (or I didn't understand how to do this from the documentation).

    Thanks in advance

  • #2

    Code:
    gen double whatever = x + y + z 
    
    replace whatever = whatever + whatever^2 + whatever^3 + whatever^4
    You could write a loop, but for your problem as stated, the above is what I would do -- although I have never found anything above a cubic to be the right thing to use.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      gen double whatever = x + y + z
      
      replace whatever = whatever + whatever^2 + whatever^3 + whatever^4
      You could write a loop, but for your problem as stated, the above is what I would do -- although I have never found anything above a cubic to be the right thing to use.
      Probably, I haven't been clear enough: I need macro like:

      Code:
      global whatever x y z x^2 y^2 z^2 xy xz yz
      And so on, until I reach the nth order (I said 4th randomly, could be less or more, I'll try until I'll get the same estimate for the parameter of interest).

      Comment


      • #4
        I see. A better answer depends on knowing how you intend to use this. For example, x y z are said to be variables, but we don't know that xz and so on are also variables.

        Factor variable notation should help.

        Comment


        • #5
          Ok, thanks, I'll try!

          So, x is continuous (it's investment)
          y is continuous (it's capital)
          z is discrete (it's age)

          I simply need to estimate a production function similarly to Olley and Pakes (1996). Hence, the polynomial here is a control function for productivity, which I use in the estimation to explicitly "control" for some productivity process (this, in reality, is not the "true" control function, it's simply an approximation of the inverse of the optimal investment rule).

          I can't find a way to feasibly write down this polynomial in Stata, apart from a sluggish "gen this" "gen that" approach...

          P.S. I don't want to use the dedicated packages to estimate this model: I need to understand if I got everything correctly, first, as I might want to build on this.
          Last edited by Matteo Bulgarelli; 18 May 2022, 06:08.

          Comment

          Working...
          X