Announcement

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

  • -margins- after piecewise linear regression using mkspline

    Dear Statalisters:

    I have been googling around for an answer to this, and most of the answers are older and suggest essentially hacking -margins- to get margins after using a spline in a regression. I'm hoping that someone at Stata (or elsewhere) has come up with a solution for it in the last 10 years that I can use.

    Unfortunately, I'm still on Stata 16.1, but hopefully there is something in there that can help.

    I'm looking to fit a piecewise linear spline into a regression model. For example, if y=income, and x1=age, and x2 is a nominal variable for race/ethnicity, I would normally do something like this:

    Code:
    regress income i.race##c.age
    margins, at(age=22(10)62) over(race)
    There are a couple of different ways to specify that, but this is the solution that I've found that works best for these specific models since I can then change the over option to compare one group vs all others, for example. It gives me what I need.

    So if I were to use splines, I would think the correct code would be something like:

    Code:
    mkspline agec 7=age
    regress income i.race##c.agec1 i.race##c.agec2 i.race##c.agec3 i.race##c.agec4 i.race##c.agec5 i.race##c.agec6 i.race##c.agec7
    margins, at(age=22(10)62) over(race)
    Except that because age is no longer in the model, and it doesn't realize that age is related to agec1-agec7, it spits back an error message.

    I saw somewhere a suggestion that I could do this:

    Code:
    mkspline agec 7=age
    regress income i.race##c.agec1 i.race##c.agec2 i.race##c.agec3 i.race##c.agec4 i.race##c.agec5 i.race##c.agec6 i.race##c.agec7
    margins, over(race age)
    I am pretty sure this would work, but it would go on and calculate out age for 40-something levels which I don't really need. And if I have age down to the monthly level (e.g., 22.08, 22.16, 22.25, 22.35....) then I would think it would calculate it out for 400+ levels.

    Ideally, what I would do is something like:

    Code:
    mkspline agec 7=age
    regress income i.race##c.agec1 i.race##c.agec2 i.race##c.agec3 i.race##c.agec4 i.race##c.agec5 i.race##c.agec6 i.race##c.agec7
    margins, over(race age(22(10)62)
    Which I am pretty sure would spit back an error code, but is essentially what I want.

    If you have any advice on how to code this properly, I would greatly appreciate hearing it. Thanks for your time.

    Best,
    Jonathan
    Last edited by Jonathan Horowitz; 08 Nov 2023, 17:14.

  • #2
    Look into f_able from ssc
    https://www.stata.com/meeting/us20/s...ios-Avila1.pdf

    Comment


    • #3
      Thanks--I will look into this and report back if I have more questions.

      Comment


      • #4
        Just something else
        https://friosavila.github.io/stata_do/stata_do6.html

        Comment

        Working...
        X