Announcement

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

  • Plotting fitted line from multivariate linear & quadratic regression

    Dear Statalisters,

    I am wondering whether there is a way to plot the fitted line from multivariate linear & quadratic regressions.
    For example, in simple linear regression, I would do it this way:
    Code:
    sysuse auto, clear
    reg mpg weight
    predict mpg_hat1
    line mpg_hat1 weight, sort
    
    *or maybe simply
    twoway  (lfit mpg weight)
    However, when I try to do this in multivariate linear regression:
    Code:
    sysuse auto, clear
    reg mpg weight length turn
    predict mpg_hat2
    line mpg_hat2 weight, sort
    It gives me a jagged line plot since the predicted values take into account all the covariates included in the model.

    Regarding this, I'd like to ask:
    Is it possible to plot a linear fitted line for the multivariate model (i.e. just take into account the coefficient for the variable weight after adjusting for length and turn )

    I would also like to fit a multivariate quadratic model, but I am guessing the solution would be similar.

    Thanks for your help!

    Regards,
    Albert
    Last edited by Albert Henry; 10 Jul 2017, 12:06.

  • #2
    See http://www.maartenbuis.nl/wp/inter_q...ter_quadr.html
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      If you simply want to plot the linear slope of weight holding the effect of length and turn, use of 'function' may help:

      Code:
      sysuse auto, clear
      
      reg mpg c.weight length turn //Linear model
      
      twoway (function mpg=_b[_cons]+_b[weight]*x, range(1800 4800) xtitle(weight))
      
      //For quadratic slope of weight:
      
      reg mpg c.weight##c.weight length turn //quadratic model
      
      twoway (function mpg=_b[_cons]+_b[weight]*x +_b[weight]*x^2, range(1800 4800) xtitle(weight))
      Roman

      Comment


      • #4
        Roman, your code seems to be holding variables length & turn constant at 0, which is unrealistic. Why not just use margins & marginsplot instead?

        Code:
        sysuse auto, clear
        // Linear fit
        regress mpg c.weight length turn //Linear model
        quietly margins, at(weight=(1800(100)4800))
        marginsplot, recast(line) recastci(rarea)
        // Quadratic fit
        regress mpg c.weight##c.weight length turn //quadratic model
        quietly margins, at(weight=(1800(100)4800))
        marginsplot, recast(line) recastci(rarea)
        Also, in your second example (for the quadratic fit), I think you need to replace _b[weight]*x^2 with _b[c.weight#c.weight]*x^2.

        HTH.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          Bruce I agree that is unrealistic. However, my answer#3 relates to the question asked in the original post#1 referring to the bold part (if I understood it correcly). Thanks for correcting the quadric typo.
          Roman

          Comment


          • #6
            Hi Maarten, Roman, and Bruce,

            Thanks for your suggestions. For now, I am more inclined to use the margins and marginsplot command as suggested by Bruce.

            If I may ask further, is it possible to overlay two or more models created using marginsplot in the same graph?
            For example, in the code provided by Bruce at post #4, is there a way to combine the linear and quadratic fit models in the same graph?

            Regards,
            Albert

            Comment


            • #7
              Hello Albert. Does this give you what you want?

              Code:
              scatter mpg weight || lfit mpg weight || qfit mpg weight
              --
              Bruce Weaver
              Email: [email protected]
              Version: Stata/MP 18.5 (Windows)

              Comment


              • #8
                #6: If you are after overlaying two predictions from two 'margins' post-estimations, use the user-written program 'combomarginsplot'written by Nicholas Winter after margins. Type -ssc install combomarginsplot- to install the programme. Example of Bruce's commands with margins and overlaying the plots with combomarginsplot:


                Code:
                qui regress mpg c.weight length turn //Linear model
                qui margins, at(weight=(1800(100)4800)) noci saving(x1, replace)
                
                qui regress mpg c.weight##c.weight length turn //quadratic model
                margins, at(weight=(1800(100)4800))  noci saving(x2, replace)
                
                qui combomarginsplot x1 x2 , noci plotdim(_filenumber) ///
                     label("Linear" "Qadratic") name(fixed, replace)
                Click image for larger version

Name:	test.png
Views:	1
Size:	166.3 KB
ID:	1401299
                Roman

                Comment


                • #9
                  Hi Bruce and Roman,

                  Thanks for your help!
                  I think Bruce's suggestion in #7 will only plot the univariate model (with only weight as covariate) so I will go with Roman's suggestion in #8 for now

                  Best,
                  Albert

                  Comment

                  Working...
                  X