Announcement

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

  • Plotting predictions for each period along with confidence intervals?

    Dear colleagues,

    given data on 12 months, I regressed my outcome on dummies for periods 2-12, plus controls c1-c5, to see where the outcome changes significantly relative to the baseline month:
    Code:
    reg outcome t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 c1 c2 c3 c4 c5
    .

    Now I can plot the 11 coefficients together with confidence intervals:
    Code:
    coefplot, keep(t*) vertical levels(95)
    But I'm wondering how I can best plot the corresponding predictions? I know I could manually add to the coeffcient the constant, plus each of coefficients on c1-5 times the respective mean value. Or I could use predict, then average for each period, then plot those averages. But in neither case would be sure how to get the correct confidence intervals.

    Is there a pre-programmed command for this, as option of -coefplot- or otherwise?

    Thanks! PM








  • #2
    First advice would be: "stop using manually computed dummies." It's nearly never necessary. Try refit the model with the original time variable (let's call that mos):
    Code:
    reg outcome i.mos c1 c2 c3 c4 c5
    Then, you can tap into -margins- and -marginsplot-:
    Code:
    margins mos
    marginsplot
    It'd by default set all the other variables not mentioned here at their means. There are options to fine tune them, check -help margins- for use cases. Here is a quick demo using auto:

    Code:
    sysuse auto, clear
    
    reg mpg i.rep78 weight headroom
    margins rep78
    marginsplot

    Comment


    • #3
      Hi Ken,

      thanks so much, that seems to work so far!!

      Just one follow-up: Ideally I'd also like to include in the regression interactions of the time dummies with a continous treatment variable T to see whether exposure to treatment T has a different effect in different periods. So I tried

      Code:
      reg outcome c.T##i.mos c1 c2 c3 c4 c5
      margins c.T##i.mos
      marginsplot
      That did not work, presumably because this notation does not allow for continuous variables. Is there another way of getting there?

      Thanks! PM

      Comment


      • #4
        Originally posted by Peter Meier View Post
        Hi Ken,

        thanks so much, that seems to work so far!!

        Just one follow-up: Ideally I'd also like to include in the regression interactions of the time dummies with a continous treatment variable T to see whether exposure to treatment T has a different effect in different periods. So I tried

        Code:
        reg outcome c.T##i.mos c1 c2 c3 c4 c5
        margins c.T##i.mos
        marginsplot
        That did not work, presumably because this notation does not allow for continuous variables. Is there another way of getting there?

        Thanks! PM
        Continuous variable cannot be a main argument in margins, which is for factors (categorical variables) only. If you need to see how predicted y changes with mos with T, you'll need to give some concrete T examples:

        Code:
        reg outcome c.T##i.mos c1 c2 c3 c4 c5
        margins mos, at(T=(30 35 40 45 50))
        marginsplot

        Comment


        • #5
          Thanks so much Ken, this works great now! PM

          Comment

          Working...
          X