Announcement

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

  • Rergerssion graph quadratic relationship

    I am writing a regression of the form:
    y=b1+b2x+b3x^2+...+bkxk+e

    I then want to to plot the quadratic relationship between y and x, holding all other variables at their average values. What command should I use for this?

  • #2
    "Rergerssion": edit that title if you can.

    Code:
    help margins

    Comment


    • #3
      I agree with Nick that either margins or the user-written -mcp- command (use search to find and download) is the way to go; note, however, that unless you used factor variable notation you will not get what you want; if you are unsure what I am referring to see
      Code:
      help fvvarlist

      Comment


      • #4
        Note that you need to use factor variable notation to do your squared variables. reg y c.x c.x#c.x [The c. on the first x term may be unnecessary. You cnl also write reg y c.x##c.x. Then margins will take care of both linear and squared effects automatically.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          "Rergerssion": edit that title if you can.

          Code:
          help margins
          Could you give specific codes for me, sir ? I also meet with the problem now, and I am confused after reading help of margins.

          Thank you very much. Looking forward to your reply.

          Wei

          Comment


          • #6
            Specific code for margins from me or anybody else would seem to require that you post your model code.

            Comment


            • #7
              Originally posted by Phil Bromiley View Post
              Note that you need to use factor variable notation to do your squared variables. reg y c.x c.x#c.x [The c. on the first x term may be unnecessary. You cnl also write reg y c.x##c.x. Then margins will take care of both linear and squared effects automatically.
              Sir, based on the same question of plotting the quadratic relationship with holding other variables at sample mean, if my understanding is right, the regression estimation which takes account of factor variable notation should be like:

              regress y c.x1 c.x1#c.x1 x2 x3 .....

              Next, how to use the command of mcp?

              mcp x1 ?

              Is it right? Sir, please give me some suggestions, I am waiting for your reply.

              Wei

              Comment


              • #8
                Originally posted by Nick Cox View Post
                Specific code for margins from me or anybody else would seem to require that you post your model code.
                Dear sir,

                Sorry to post my code.

                Actually, my
                question is the same as the poster,that is plot the quadratic relationship with holding other variables at sample mean, and the model is as follows.

                regress y c.x1 c.x1#c.x1 x2 x3 .....

                where x1 is the independent variable, x2,x3...are control variables.

                Next, how to use the command of mcp or margins? I guess mcp also works, its code should be like this,

                mcp x1 ?

                Although I can obtain the curve, I still wonder if the code is right.

                Please give me some suggestions.

                Thank you, sir.

                Wei

                Comment


                • #9
                  I'm not familiar with -mcp- It's not part of official Stata. But with -margins-, here is what you have to do. First you have to identify interesting values of x1. In most situations that would be a series of values that more or less span the observed range of x1 and are reasonably close to each other. Let's say, just for demonstration, that x1 ranges from 1 to 10 in your data. Then you might do:

                  Code:
                  margins, at(x1 = (1(1)10))
                  marginsplot
                  You can, of course, choose whatever values of x1 you want. Sometimes part of the range of the observed data is not of interest in terms of your research goals. Sometimes you are interested in what happens at out-of-sample values of x1 (though one should always be cautious about that!).

                  Note also that -marginsplot- accepts pretty much all options available in -graph twoway-, and offers several of its own, so you can customize the appearance of the graph to your liking.

                  Comment


                  • #10
                    Hi, Emer, You might want to try this:
                    Code:
                    *import delimited "marginscontplot-ushape.csv", clear
                    
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float(gini pcgdp state trans)
                      59 6.34    6  1.9
                    48.9 6.51  3.1  3.2
                      35 6.55  4.2  1.1
                      57 6.77 13.2  6.9
                      40 6.77    6  1.8
                    36.7  6.9 12.4  4.7
                    57.3 6.92  7.5  5.6
                      49 6.94  1.3  1.8
                      60 6.94  3.3    1
                    51.3 7.13  3.4  6.1
                    50.1 7.22 15.2 12.2
                      54 7.27 11.3  7.3
                    52.5  7.3 18.3  6.8
                    59.1 7.31  9.6  1.1
                    38.3 7.49  2.8  1.6
                      51 7.51  5.1  2.4
                      43 7.57 19.3  7.7
                      57 7.66  7.5  5.9
                      43 7.66 23.3  4.6
                    45.5 7.68 11.8  2.8
                    53.3 7.77    5  6.3
                    59.5  7.8  5.8  3.3
                    38.2 7.81 20.4   12
                    44.5 7.87   11  5.5
                    39.9 7.89 50.8  8.6
                    39.7  7.9 22.2  5.4
                    44.5 7.94 23.7  6.8
                      57 8.03 14.8  3.4
                    47.8  8.1  6.2  4.3
                    42.9 8.18 26.9  7.9
                    56.5 8.24 17.3 13.2
                    51.6 8.24 10.7    2
                    43.8 8.27 13.6  7.3
                      26 8.34 70.4 17.5
                      63 8.35  8.4  2.3
                      46 8.37 16.9 12.2
                    47.6 8.38 15.2  7.6
                    25.7 8.43 95.2 11.7
                    63.3 8.44 11.7  5.5
                    48.2 8.46  9.2 19.1
                    37.9 8.49 78.9 13.1
                      57  8.5 13.2  8.9
                    48.4 8.53  8.4    8
                    50.6 8.58 21.4  5.6
                    44.1 8.64 19.3  5.6
                    35.7 8.65  9.3  2.9
                    42.4 8.66 21.4 10.5
                    23.1 8.69 93.9 19.9
                    38.1  8.7 14.2 17.1
                    39.9 8.77 10.7 16.7
                    34.6 8.86 19.6 25.1
                    19.5 8.91 98.8 21.3
                    31.5 9.02 13.7 18.1
                    35.7 9.04 12.2  8.8
                      41 9.25 10.4 18.3
                    33.3 9.29 27.1 22.1
                    42.8 9.31 18.6  1.2
                      30 9.33 24.7 19.6
                    24.9 9.42 37.9 27.9
                    32.1 9.45   15 31.1
                    31.3 9.47 20.9 24.4
                    27.4 9.47 22.5 30.3
                    28.1 9.48 22.8 19.8
                    30.7 9.52 21.2   31
                      28 9.52 29.4 33.3
                      35 9.52  9.5 17.5
                    26.9 9.53 24.8 27.1
                    20.2 9.55 28.7   22
                    48.5 9.55  7.9  2.9
                    31.6 9.58 29.3 17.1
                    27.8 9.59 22.3 25.7
                    22.9 9.61 36.2 32.2
                      32 9.78 24.1 21.5
                    35.5 9.78 10.4 14.9
                    34.4  9.9 15.8 17.7
                    end
                    
                    reg gini pcgdp c.pcgdp#c.pcgdp state trans, robust
                    margins, dydx(pcgdp) at(pcgdp = (6.4(0.1)9.8))
                    marginsplot, recast(line) recastci(rline) ciopts(lpattern(dash) lcolor(red))
                    Click image for larger version

Name:	u.png
Views:	1
Size:	76.7 KB
ID:	1428913
                    Ho-Chuan (River) Huang
                    Stata 17.0, MP(4)

                    Comment

                    Working...
                    X