Announcement

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

  • Plotting regression results in scatter plot

    Hello,

    I have a regression of the form:

    Code:
    reg yvar xvar zvar i.year
    where year is a categorical variable from 1990 to 2020. I want to store the estimates for each year and plot them in a scatter plot with a fitted regression line. Something along the lines of saving the estimates as "yearcoefficients" and the plotting
    Code:
    scatter year yearcoefficients || lfit year yearcoefficients
    Is this possible at all?

    Thank you very much!

    Joan





  • #2
    Once the year is treated as a categorical variable its relationship with the dependent is no longer linear. Fitting a linear line can be misleading. A more accurate way would be to just connect the predicted means in a non-linear manner:
    Code:
    reg yvar xvar zvar i.year
    margins year
    marginsplot
    The -margins- command can also use a -saving()- option to export the results into another data set (see -heal margins saving-), with that you can add the years back and force a linear line through it. However, that line would not be related to the regression model.

    Comment


    • #3
      Thank you very much Ken, and if I use margins plot how can I fit a line (not related to the regression model) through the observations? I tried lfit / qfit but it did not work out.

      Comment


      • #4
        Not that I know of. As stated in #2, you can add a -saving(WHATEVER)- option after the margins (e.g. margins year, saving(WHATEVER)) into a data set called WHATEVER. The margins will be in there, then you can create a year variable like -gen year = _n + 1989- and make the plot.

        Again, I still think it's a bad idea, please think twice before showing it. There is also a test for trend across ordered indicator variables, use -help nptrend- to learn more.

        Comment


        • #5
          Yes I understand what you mean, perhaps my approach is completely wrong here. Basically I want to plot the coefficients without confidence intervals and with a fitted quadratic line to show the evolution across time, is it possible to do that with marginsplot?

          Thank you again,

          Joan

          Comment


          • #6
            You would have to have a quadratic term for time in your model, but to approximate what you see out of qfit, you'd need to switch back to continuous time. Ken said that treating time as continuous could be problematic, but if you model time as a polynomial in the regression, then you can approximate what you see in the raw data:
            Code:
            reg yvar xvar zvar c.year##c.year
            margins, at(year=(1990(1)2020))
            marginsplot
            That should give you a bendy line in the marginsplot.

            Comment


            • #7
              Basically I want to plot the coefficients without confidence intervals and with a fitted quadratic line to show the evolution across time, is it possible to do that with marginsplot?
              No, not if year was treated as a categorical variable; and that is for a good reason. The suggestion in #5 is a lot better for the purpose.

              Just in case #4 was not clear, here is the workaround I was talking about:
              Code:
              sysuse nlsw88, clear
              
              reg wage hours i.age
              margins age, saving(outfile, replace)
              
              use outfile, clear
              gen age = _n + 33
              list age _margin
              twoway (scatter _margin age)(qfit _margin age)

              Comment


              • #8
                This works perfectly thank you so much!

                Comment

                Working...
                X