Announcement

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

  • Graph Slope of a Quadratic Regression

    Hi everyone,

    Just curious to know if there a command in Stata which will plot for me the slope of the fitted line below?

    Thanks.
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	15.5 KB
ID:	1471376



    These are the commands I used to get to the figure above:

    gen hhsize2 = hhsize * hhsize
    reg pscore hhsize hhsize2
    predict yhat
    twoway (scatter yhat hhsize)
    Last edited by danishussalam; 21 Nov 2018, 00:43.

  • #2
    The slope of a quadratic a + b x + c x^2 is just a straight line b + 2 c x and can be plotted after a regression with (in your case)

    Code:
    twoway function _b[hhsize] + 2 * _b[hhsize2] * x, range(hhsize)
    Somehow I doubt that is what you really want, but I can't formulate any other guess on what the slope of a quadratic might be.

    Not the same question but the data in your graph are not especially convincing on there being a turning point rather than a flattening

    Comment


    • #3
      Hi Nick Cox

      Thanks, for the reply.

      Actually, this "the data in your graph are not especially convincing on there being a turning point rather than a flattening" is exactly what I want to investigate.

      From the graph I attached above, it's obvious that there is a negative relationship (corr -54%) between pscore and hhsize. But most importantly, the rate at which pscore decreases, with each additional household, becomes smaller after say 7 households. i.e. it decreases at an increasing rate and then becomes flat - Is there a way I can visualize this?

      Thanks.

      I see that relationship also when I tabulate household size by mean of score:


      table hhsize, c(mean pscore)


      Household
      Size mean(pscore)

      1 56.95327
      2 47.55906
      3 36.85659
      4 32.55806
      5 28.12925
      6 27.71605
      7 24.76667
      8 25.60656
      9 23.74576
      10 21.98058
      11 21
      12 21.41936
      13 21.58333
      14 18.54545
      15 17.55556
      Last edited by danishussalam; 21 Nov 2018, 01:58.

      Comment


      • #4
        Code:
        help lpoly 
        
        search localp

        Comment


        • #5
          Code:
          sysuse auto, clear
          
          // use more reasonable units for weight and price
          gen weight_t = 0.00045359237 * weight
          label var weight_t "weight (tonne)"
          
          gen price_t = price /1000
          label var price_t "Price (1000s dollar)"
          
          // estimate model
          reg price_t c.weight_t##c.weight_t i.foreign
          
          // plot the slope
          margins, dydx(weight_t) at(weight_t=(.75(.05)2.15) foreign=0)
          marginsplot, recastci(rarea) ciopt(astyle(ci) acolor(%75)) ///
             plotopts(msymbol(i)) xlab(1(.5)2) yline(0) scheme(s1color) ylab(,angle(0))
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Note that the thing that makes Maarten's solution the best approach is using factor variable notation for the square. This makes margins work much more easily. You can also plot predicted rather than dydx values (which is what I suspect you want since you have plotted what look like actual values along with the prediction equation).

            Comment


            • #7
              Dear Maarten Buis

              Could you suggest how to create marginsplot in case one cannot use factor variable notation for the squared term?

              For my current analysis, I apply the xthtaylor command which I always have to generate the squared term of independent variable by hand?

              And the syntax "marginsplot" does not work in this case.

              Thank you very much,
              Rattiya

              Comment


              • #8
                Hi Rittiya
                you could use my command "f_able" (from ssc). The paper that explains it is here https://journals.sagepub.com/doi/pdf...6867X211000005

                and an example:

                Code:
                ssc install f_able
                webuse psidextract
                ** Alternatively, you can create the variable using fgen exp2 = exp^2
                frep exp2=exp^2
                xthtaylor lwage wks south smsa ms exp exp2 occ ind union fem blk ed, endog(exp exp2 occ ind union ed) constant(fem blk ed)
                margins, dydx(exp)
                margins, dydx(exp) at(exp=(1(5)50))
                HTH
                F

                Comment


                • #9
                  Hi FernandoRios ,

                  many thanks for your suggestions. I have tried your command but still not working which I may not apply correctly. Please find the steps below:

                  I am examining the relationship between share of informal employment (dependent var) and deforestation rate (independent var):


                  **** squared term of deforestationFAO

                  fgen sq_xdeforestationFAO = _xdeforestationFAO^2
                  frep sq_xdeforestationFAO = _xdeforestationFAO^2

                  *** Then I ran the xthtaylor
                  global DetModel4 InflationC GDPGrowth ineq_edu _xdeforestationFAO sq_xdeforestationFAO

                  xthtaylor sh_Informal $DetModel4 _dC129 _xRatFreeAsso Africa Americas $yearD, endog(GDPGrowth shcerForArea) vce(cluster _xccode)

                  *** The result is that the deforestation is postively signficant and its squared term is negatively significant

                  *** The I calculated the turning point as: nlcom -_b[_xdeforestationFAO]/(2*_b[sq_xdeforestationFAO]) = 1.56



                  margins, at(_xdeforestationFAO=(0(0.5)5))
                  marginsplot


                  *** I understood that when I apply the marginsplot, I would get the concave plot.., instead it produces the linear line

                  Did I miss any steps here?


                  Best,
                  Rattiya
                  Last edited by Rattiya Lippe; 19 Jan 2023, 11:06.

                  Comment


                  • #10
                    Yes,
                    First, you dont need to "fgen" and "frep" (i did it in the example because there was an exp2 already )

                    second, something i forgot to add. This is the complete example

                    Code:
                    ssc install f_able
                    webuse psidextract
                    ** Alternatively, you can create the variable using fgen exp2 = exp^2
                    frep exp2=exp^2
                    xthtaylor lwage wks south smsa ms exp exp2 occ ind union fem blk ed, endog(exp exp2 occ ind union ed) constant(fem blk ed)
                    f_able exp2, auto
                    margins, dydx(exp)
                    margins, dydx(exp) at(exp=(1(5)50))
                    or in your case, after xthtaylor
                    Code:
                    f_able sq_xdeforestationFAO, auto
                    That line "tells" margins what to look for

                    Comment


                    • #11
                      FernandoRios : You are amazing !.. Now it works

                      Many thanks, Rattiya

                      Comment

                      Working...
                      X