Announcement

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

  • Polynomial Regression Plots

    I ran the following regression (sample data below):

    Code:
    xtreg lngdp i.iso3 tmean tmeansq pmean pmeansq lnpop
    The results are the following (country fixed effects absorbed).

    Code:
           tmean |   .0082621   .0023944     3.45   0.001     .0035691    .0129551
         tmeansq |  -.0004511   .0000862    -5.23   0.000    -.0006201   -.0002821
           pmean |    .081774   .0108711     7.52   0.000     .0604671     .103081
         pmeansq |  -.0093381   .0011375    -8.21   0.000    -.0115676   -.0071086
           lnpop |   .7900886   .0018756   421.25   0.000     .7864125    .7937646
           _cons |   2.043321   .0420113    48.64   0.000      1.96098    2.125662
    I would like to plot functions tmean and pmean along with their squared terms. Something like the following:

    Code:
    twoway function y = _b[_cons] +_b[tmean]*x + _b[tmeansq]*x^2, range(-10 40)
    Code:
    twoway function y = _b[_cons] +_b[pmean]*x + _b[pmeansq]*x^2, range(0 15.04)
    Is it possible to add confidence intervals for these plots? Thanks a lot.


    Data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long iso3 double(tmean pmean lngdp tmeansq pmeansq lnpop) float n
    30                 .                  . -.10238007456064224                  .                 .   -3.59053098549622 1
    30                 .                  . -2.3195972442626953                  .                 .  -6.004328534538948 2
    30 4.558476775956307 2.4556989669799805 .012236329726874828  20.77971076965332 6.030457496643066  -2.725406532072661 3
     1 4.060157103825159 1.6384220123291016 -.48375624418258667  16.48487663269043 2.684426784515381 -3.2221001674784175 4
    30 3.710102459016416  1.439255952835083  -6.297446250915527 13.764860153198242 2.071457624435425 -11.045147226737937 5
    end
    label values iso3 iso3
    label def iso3 1 ".", modify
    label def iso3 30 "CHL", modify
    Thank you very much!

  • #2
    You're doing this the hard way. Re-run your regression using factor-variable notation and let Stata do all the hard work for you.

    Code:
    xtreg lngdp ib1.iso3 c.tmean##c.tmean c.pmean##c.pmean lnpop
    margins, at(tmean = (-10(5)40) iso3 = 1 pmean = 0 lnpop = 0)
    marginsplot
    margins, at(pmean = (0(1)15) iso3 = 1 tmean = 0 lnpop = 0)
    marginsplot
    Note that -marginsplot- accepts most of the options available in -graph twoway- commands so you can customize the appearance of the graph to your taste.

    I also wonder if your graphs might not be more useful if, instead of plotting these bare quadratic functions (which is what setting iso3 = 1, pmean = 0 and lnpop = 0 gets you) you plotted the expected values of lngdp as functions of tmean and pmean adjusted to the observed distribution of iso3 and (respectively) pmean, and tmean, or perhaps at average values of the last two. Those would be simple modifications of the -margins- commands.



    Comment


    • #3
      Dear Professor Schechter,

      Thank you so much for your suggestions. The code works really well!

      I agree with your suggestion of plotting lngdp as average values of tmean and pmean, (iso3 are countries). Average values - I understand, could you kindly provide me some guidance for the "the observed distribution" part? Thanks again!

      Comment


      • #4
        Well, the plots with all other variables set to their observed values would be:

        Code:
        xtreg lngdp ib1.iso3 c.tmean##c.tmean c.pmean##c.pmean lnpop
        margins, at(tmean = (-10(5)40))
        marginsplot
        margins, at(pmean = (0(1)15))
        marginsplot
        When you give a -margins- command, any variable whose values are not covered in an -at()- or -atmeans- option, are left at their observed values for calculating the expected values or marginal effects.

        Comment


        • #5
          Professor Schechter,

          I'm not sure why the my reply was not posted. I apologise.

          I understand now. Thank you so much!

          Comment

          Working...
          X