Announcement

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

  • Graphing regression with 3 variables

    Im using a regression model with 3 variables (a numerical dependent variable, a numerical independent variable and a categoriacal independent variable).

    lets say: regress X Y i.Z

    Is there a way to include the 3 variables in one graph ?

    What i want is basically a linear prediction graph that, instead of 1 line graphing the relationship between variables X and Y, includes 1 line for each category of Z.


    Im attaching a drawing of what i need. (The drawing has a scatter plot but i dont necessarily need it.

    Thanks in advanced.

    Click image for larger version

Name:	Regression.png
Views:	3
Size:	12.6 KB
ID:	1523258
    Attached Files

  • #2
    try this:
    Code:
    sysuse auto, clear
    regress price mpg i.fore
    predict yhatprice
    twoway (lfit price yhatprice if for==0, sort) (lfit price yhatprice if for==1,sort) (scatter price yhatprice if for==0, mcolor(red)) (scatter price yhatpriceif for==1, mcolor(green)), legend(off)
    you can, of course, have your own legend for whatever you want, but I turned off the default

    Comment


    • #3
      Thanks, but i think that graph is not exactly what i want.

      That shows the linear fit for regress price mpg if fore==0 and also the linear fit when fore==1. It combines two graphs.

      In my case both lines should be parallel. This should show the difference in price when a car is foreign or domestic (1767$) when controlling for mpg.



      Comment


      • #4
        The following is inspired by Rich's example, but plots the data as in the original post, and uses twoway function to plot the fitted lines.

        Code:
        . * data setup
        . sysuse auto
        (1978 Automobile Data)
        
        . gen y = gear
        
        . gen x = mpg
        
        . gen z = foreign
        
        . 
        . * fit model
        . regress y x i.z
        
              Source |       SS           df       MS      Number of obs   =        74
        -------------+----------------------------------   F(2, 71)        =     61.69
               Model |  9.64714906         2  4.82357453   Prob > F        =    0.0000
            Residual |  5.55129871        71  .078187306   R-squared       =    0.6347
        -------------+----------------------------------   Adj R-squared   =    0.6245
               Total |  15.1984478        73  .208197915   Root MSE        =    .27962
        
        ------------------------------------------------------------------------------
                   y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                   x |   .0315523   .0061528     5.13   0.000     .0192839    .0438208
                 1.z |   .5446826   .0773537     7.04   0.000     .3904436    .6989216
               _cons |   2.180953   .1280064    17.04   0.000     1.925715     2.43619
        ------------------------------------------------------------------------------
        
        . 
        . * plot predicted values and raw data
        . twoway  (function y = x*_b[x]+_b[_cons], lcolor(red) range(x)) ///
        >         (function y = x*_b[x]+_b[_cons]+_b[1.z], lcolor(blue) range(x)) ///
        >         (scatter y x, mcolor(green)) ///
        >         , legend(off) ytitle(Y) xtitle(X)
        Click image for larger version

Name:	graph.png
Views:	1
Size:	24.0 KB
ID:	1523269

        Comment


        • #5
          Thanks so much! I think this is exactly what i need.

          I was also looking into doing it with margins (https://www.youtube.com/watch?v=7maMbX_65b0 minute: 4).

          Comment

          Working...
          X