Announcement

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

  • Creating polynomial expansion of a variable

    Dear Statalist,

    What I want to do is to create a polynomial expansion of a certain degree of two variables k and i in such a way to approximate an unknown function g(k,i). Is there a function in Stata? Doing that manually is quite time consuming and it is dangerous since you can forget some terms easily.

    Manually it is (for a polynomia of degree 4): gen phi= i+i*k+i*k*k+i*k*k*k + i*i+ +i*i*k*k+i*i*k+i*i*i+i*i*i*k+k*k*k*k+i*i*i*i

    Thank you for your help,

    Gino

  • #2
    Code:
    local degree 4
    
    local polynomial
    
    forvalues d = 1/`degree' {  // GENERATE TERMS OF DEGREE d
        forvalues ni = 0/`d' {
            local nk = `d' - `ni'
            local term = `ni'*"*i" + `nk'*"*k"
            local term = substr("`term'", 2, .)
            local polynomial `polynomial' + `term'
        }
    }
    local polynomial = substr("`polynomial'", 3, .)
    
    display "`polynomial'"
    You can change degree at will just by changing the number specified in the first line.

    Comment


    • #3
      Thank you Clyde!

      Comment

      Working...
      X