Announcement

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

  • Add polynomial control variable to model

    Hello, consider a simple regression model:

    Code:
    regress test_score i.gender age
    However, upon inspection of my data, the relationship between test_score and age is not linear. How can I tell Stata to treat the age covariate as polynomial? In R, for example (which I am more familiar with), this would be done simply by doing: test_score ~ gender + I(age^2). But I am unsure of the equivalent in Stata. Thank you.

  • #2
    Code:
    regress test_score i.gender c.age##c.age
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      This is probably what you're looking for:

      Code:
       regress test_score i.gender c.age c.age#c.age
      Edit: Or maybe not. Maarten's code gets you what you want I believe. My code adds both age and age^2 in the regression.
      Last edited by Daniel Shin; 17 Feb 2023, 14:40.

      Comment


      • #4
        Ah okay, this is simpler than I thought. Thanks both--is the main difference between the two that ## will execute both the linear and polynomial forms of age? And does this translate to a quadratic (cubed) or squared term?
        Last edited by Todd Motiwalla; 17 Feb 2023, 14:39.

        Comment


        • #5
          As far as I know, quadratic equations use squares, not cubes... The inclusion of both terms allow more flexibility to fit your data. You can assess what terms are meaningful from the model.

          Comment


          • #6
            the codes in #'s 2 and 3 will give you the exact same results; see
            Code:
            h fvvarlist

            Comment


            • #7
              One # will give you just the interaction (product of variables). Two # will give you the interaction and main effects. The trick is that an interaction of a variable with itself is that variable squared. If you want to model a cubic relationship you would enter c.age##c.age##c.age
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment

              Working...
              X