Announcement

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

  • Quadratic regression: adding CI to lpoly graph

    Hello all,

    I am running a non-linear regression that looks like this:

    vcemway reg ln_y x x^2 i.year FE1 FE 2 FE3, cluster(region year)

    I then use the regression coefficients to predict E(Y | X) as follows:

    gen beta_S = _b[x]
    gen beta_S_sq = _b[x^2]
    gen beta_cons = _b[_cons]

    gen FE1_mem = _b[hh_members]
    gen FE2_KasP =_b[region1]
    gen FE3_KusP =_b[region2]

    gen share_gen = exp(beta_cons + rand_S*beta_S + rand_S*rand_S*beta_S_sq + FE1_mem + FE2_KasP + FE3_KusP)

    Here, rand_S is a variable containing random values of my random variable.

    *Output graph*
    graph twoway (lpoly share_gen rand_S)

    My question is, how can I compute the upper and lower bounds of the confidence intervals to include them in the graph? I am looking to create new variables and plotting them using lpoly.

    Any other suggestions would also be welcome! Thank you very much in advance!




  • #2
    If you just want the CI overlaid on the plot, there is a corresponding twoway graph called -lpolyci- that does just that.

    Comment


    • #3
      Thank you for your answer! I'm not sure how I can use lpolyci with my code as simply replacing lpoly with lpolyci doesn't work. Would it be possible to have more details please? Thanks.

      Comment


      • #4
        Are you certain you typed it correctly? This (silly example) works for me.

        Code:
        sysuse auto
        graph twoway lpolyci price mpg

        Comment


        • #5
          Thank you. Sorry, perhaps I wasn't clear with my question. The problem is that I am running the lpoly command on a new variable that I generate from the regression coefficients:

          gen share_gen = exp(beta_cons + rand_S*beta_S + rand_S*rand_S*beta_S_sq + FE1_mem + FE2_KasP + FE3_KusP)

          That's why lpolyci won't work as I need to add the confidence intervals manually. As this is a quadratic regression, I am not sure how to do that.

          Many thanks in advance!

          Comment


          • #6
            It would still work. Generate your -share_gen- and then with -rand_S-, you can make the graph using

            Code:
            graph twoway (lpolyci share_gen rand_S)
            For what you are trying to do, generating new variables from regression coefficients, it is probably easier to learn to use -margins- in the long run, but this should get you the graph you want.

            Comment


            • #7
              Thank you so much for your answers and patience! Unfortunately, this still doesn't work (I ran the exact same code) and I still get a graph without CI... I'm not sure what I'm doing wrong.

              Thanks a lot again.

              Comment


              • #8
                Originally posted by Aicha SA View Post
                still doesn't work
                I can't diagnose what is wrong without seeing the exact code you typed and having a reproducible data example. To make a reproducible data example, type -dataex- to generate it, and when you paste the code and data here, be sure to use the CODE delimiters.

                What I can see is that the following two lines of code are not legal syntax, because you cannot specify a quadratic in the way that you have.

                Code:
                vcemway reg ln_y x x^2 i.year FE1 FE 2 FE3, cluster(region year)
                gen beta_S_sq = _b[x^2]
                At a minimum, you should fix these two lines to match below. See -help factor variables- for more information on how this works. The upside is that Stata will now recognize that x and x^2 are correlated.
                Code:
                vcemway reg ln_y c.x##c.x i.year FE1 FE 2 FE3, cluster(region year)
                gen beta_S_sq = _b[c.x#c.x]

                Comment

                Working...
                X