Announcement

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

  • #16
    Click image for larger version

Name:	71032157_2646146878780712_7483938250801807360_n.jpg
Views:	1
Size:	1.90 MB
ID:	1517173
    Dear Clyde Schechter
    I run code "
    capture postutil clear
    tempfile holding
    postfile handle quantile coefficient lb ub using `holding'

    forvalues i=1/9 {
    local ii = `i'/10
    qregpd d_ta def def_sqrt, quantile(`ii') id(id_firm) fix(year) optimize(mcmc) draws(1000) burn(500) arate(0.5)
    matrix M = r(table)
    post handle (`ii') (_b[def]) (M[1, 5]) (M[1,6])
    }
    postclose handle

    use `holding', clear
    graph twoway connected coefficient lb ub quantile" and get the graph. But when want to combine the coefficient from quantile regression from qregpd and the coefficient from OLS like Dwi Utami, I run your edited code
    "capture postutil clear
    tempfile holding
    postfile handle quantile coefficient lb ub using `holding'

    forvalues i=1/9 {
    local ii = `i'/10
    qregpd d_ta def def_sqrt, quantile(`ii') id(id_firm) fix( year) optimize(mcmc) draws(1000) burn(500) arate(0.5)
    matrix M = r(table)
    post handle (`ii') (_b[def]) (M[1, 5]) (M[1,6])
    }
    postclose handle

    regress d_ta def def_sqrt
    gen ols_coeff = _b[def]
    label var lb "lower limits"
    label var ub "upper limits"
    label var coefficient "beta"
    label var ols_coef "ols"

    use `holding', clear
    graph twoway connected coefficient lb ub ols_coeff quantile"


    But I just get the result of regression qregpd and ols. Stata says that " label var lb "lower limits"
    variable lb not found
    r(111);

    .
    . label var ub "upper limits"
    variable ub not found
    r(111);

    .
    . label var coefficient "beta"
    variable coefficient not found
    r(111);

    .
    . label var ols_coef "ols"

    .
    .
    .
    . use `holding', clear

    .
    . graph twoway connected coefficient lb ub ols_coeff quantile
    variable ols_coeff not found
    r(111)"
    And one more thing, each time I run your code, I have to import data again.
    I really do not know why, and I tried it many times, but the results still same,
    So sorry for my stupid question.
    Best regard,

    Comment


    • #17
      You need to understand how the code works. The modifications you are making to it are not appropriate.

      Let's review what postfile and its associated commands do. They enable you to take results of repetitive calculations and store them in a data file separate from the one in memory. (In version 16, you can do this with frames instead.) So the part of the code before -use `holding'- has the original data in memory, but each time you go through the loop, the -post handle- command causes the selected regression outputs to be stored in the separate temporary file named `holding'. The -postclose- command causes that file to be closed and it is now ready to be -use-d. But they are not, at that point, in memory and you cannot do calculations with them.

      Then when you -use `holding', clear-, the original data in memory are dropped and the contents of `holding' are read in, taking their place. Subsequent calculation must refer only to the variables in the `holding' file, not to the original data file.

      This explains all of the difficulties you are encountering. Your code sequence
      Code:
      regress d_ta def def_sqrt
      gen ols_coeff = _b[def]
      label var lb "lower limits"
      label var ub "upper limits"
      label var coefficient "beta"
      label var ols_coef "ols"
      is perfectly legitimate, but because it takes place outside the loop that includes -post handle- and after -postclose handle-, these results are not transferred to the temporary file `holding'. Instead the variable ols_coeff is added to the original data file. And your attempts to label the variables lb, ub, and coeffiicient fail because those variables are in the `holding' file which, at this point, is not yet in memory.

      Then you finally -use `holding', clear-. At that point you wipe out the original data, and the variable ols_coeff, which was added to it, is also wiped out. Now memory contains only the variables from `holding'. So your attempt to graph ols_coeff fails.

      I should also add that even after you fix all these problems, your graph is going to be a very unpleasant surprise to you. The regress command is run only once (and, if you put it inside the loop it would be run on the same exact data each time, so you would get just a single result for ols_coeff). Consequently when you graph it against quantile, you will just get a horizontal line because the value of ols_coeff is just a single number that never changes.

      I don't know what you're actually hoping to do, so I can't suggest other code. But hopefully, if you think about how the code is working you will be able to figure out what to do.

      Comment


      • #18
        Dear Clyde Schechter
        Thanks so much for your reply. I'm actually hoping to run qregpd with 18 years and 14,544 obs. I can run regression but I get a difficult problem for plot the results. For example, I would like plot the results of eq: d_ta = c + def + def_sqrt + ui. And I'm new user Stata (just for 3 days), so it's quite difficult for me.

        Comment


        • #19
          Your question is not very clear. The equation you give in #18 can give rise to many different "results." You could just do OLS regression (though for panel data this is usually not a good idea). You can do fixed-effects or random-effects regression. You can do quantile regressions at various quantiles. And there are still other possibilities. Nor is it clear exactly what "results" you wish to "plot," nor just what kind of plot you want.

          The code below will do panel quantile regression using -xtqreg- at quantiles 0.10, 0.20, ..., 0.90, and then follow that with -xtreg, fe-. It then plots the coefficients of def and def_sqrt on the y-axis with the quantile on the x-axis, and overlays on those plots horizontal lines at heights equal to the coefficients of def and def_sqrt from the -xtreg, fe-. Perhaps this is something like what you want.

          Code:
          capture postutil clear
          tempfile results
          postfile handle int percentile float b_def b_def_sqrt using `results'
          
          forvalues i = 10 (10) 90 {
              xtqreg d_ta def def_sqrt, q(`=`i'/100')
              post handle (`i') (`=_b[def]') (`=_b[def_sqrt]')
          }
          postclose handle
          xtreg d_ta def def_sqrt, fe
          local b_def = _b[def]
          local b_def_sqrt = _b[def_sqrt]
          
          use `results', clear
          graph twoway connect b_def b_def_sqrt percentile, sort ///
              yline(`b_def' `b_def_sqrt')
          You can customize the appearance of the graph with various options available in -graph twoway-.

          May I suggest that this kind of project is a huge leap for somebody who has only been using Stata for 3 days. You really should first do some simple analyses until you have the basics of Stata mastered before taking on problems with this much complexity and programming involved. If this project does not have an urgent deadline, I recommend you put it aside for now. Take the time to fully read the Getting Started [GS] and User's Guide [U] volumes of the PDF manuals that come with your Stata installation. It's a lot to read, and you won't remember it all, but it will take you through the fundamentals of using Stata, so that you will be able to effectively make progress from there with the help of the help files and the rest of the PDF manuals. There are also Stata videos on youtube that you might find helpful. Once you have become comfortable with simpler problems, then you can come back to this project and others of similar complexity. I can tell you that I would not have been able to do this problem on my third day as a Stata user!

          If you are under a short deadline for this project, then keep asking questions here and people will help you as best they can.
          ,

          Comment


          • #20
            Dear Professor Clyde Schechter
            Thank you very much for your help and guidance. I'll try. Best wishes.

            Comment


            • #21
              i am using XTQREG command in quantile regression. I want to plot quantile coefficients with OLS coefficients and confidence interval. please help me.
              i want to make following graphs.

              Attached Files

              Comment


              • #22
                Please read the forum FAQ for excellent advice about how to most effectively request help and provide information. In particular:

                You are asking for help with coding Except for extremely simple situations (and sometimes not even then) nobody can write code for imaginary data. You have to give example data. Be sure to use the -dataex- command to do that.

                If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                When asking for help with code, always show example data. When showing example data, always use -dataex-.

                In addition, while attachments are, in general, discouraged, Word documents are particularly discouraged as they can contain active malware. Some of us are reluctant to download such documents coming from a stranger. Use the insert image button on the Forum editor to include a picture of your graph.

                Comment


                • #23
                  Dear Clyde Schechter ,


                  I followed your code in #11 above to plot the same graph for a mean and quantile regression I have run using a panel data. For the quantile regression I used qreg2
                  by J.A.F. Machado and J.M.C. Santos Silva. and for the average I used xtreg. The code I run is:

                  Code:
                  capture postutil clear
                  tempfile holding
                  postfile handle quantile coefficient lb ub using `holding'
                  
                  forvalues i=1/9 {
                      local ii = `i'/10
                      qreg2 y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) wlsiter(60) quantile(`ii')
                      matrix M = r(table)
                      post handle (`ii') (_b[x5]) (M[1, 5]) (M[1,6])
                  }
                  postclose handle
                  
                  xtreg y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) re
                  gen ols_coeff = _b[x5]
                  
                  label var lb "lower limits"
                  label var ub "upper limits"
                  label var coefficient "beta"
                  label var ols_coef "ols"
                  
                  use `holding', clear
                  graph twoway connected coefficient lb ub ols_coeff quantile



                  But after it has finished running all the regressions successfully, Stata throws out the following error:

                  Code:
                  . gen ols_coeff = _b[x5]
                  
                  .
                  . label var lb "lower limits"
                  variable lb not found
                  r(111);
                  
                  end of do-file
                  
                  r(111);
                  I have the entire output which I have not produced here because I thought it is really long. But I can produce it if you need it to be able to help me. I am not sure, but it appears Stata cannot locate my confidence interval. Are you please able to help me resolve this error?

                  Thank you.

                  Cobby.

                  Comment


                  • #24
                    The -use `holding'- command has to come before the -label var- commands: you have it after. The variables lb, ub, and coefficient only exist in the `holding' data set, so until you -use- it, those variables are not in active memory.

                    There is another problem looming. Your command -gen ols_coeff = _b[x5]- is pointless because the next thing you are going to do is -use `holding', clear- and ols_coeff will thereby get wiped out along with all the original data. If you look carefully at the code in #19 you will see that the coefficient from the OLS regression needs to be stored in a local macro, which can then later be referred to in the -graph- command's -yline()- option in order to create a reference horizontal line at that level.

                    And yet another, your -graph twoway connected- command will also fail because it refers to the non-existent (at that point in time, and if you follow my advice above, never existent) variable ols_coeff.

                    Please read the code in #19 more carefully and follow the approach there, and you will not run into these problems.

                    Comment


                    • #25
                      Dear Clyde Schechter ,

                      Thank you for response. I amended my code based on #19 and got the plot with no errors. However, it appears I still don't get to show the confidence bounds properly. For example, the graph shows the label for the lower limit but it does not appear on the graph. The graph that came up is :


                      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	34.6 KB
ID:	1523292



                      The amended code that produced the graph is:

                      Code:
                      capture postutil clear
                      tempfile results
                      postfile handle int percentile float lb ub x5 using `results'
                      
                      forvalues i = 5 (5) 95 {
                          qreg2 y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) wlsiter(60) q(`=`i'/100')
                          post handle (`i') (`=_b[x5]')
                      }
                      postclose handle
                      xtreg y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) re
                      local x5 = _b[x5]
                      
                      use `results', clear
                      
                      label var lb "lower limits"
                      label var ub "upper limits"
                      
                      graph twoway connect x5 lb ub percentile, sort ///
                          yline(`x5')

                      Moreover, what I wanted to achieve looks like this:

                      Click image for larger version

Name:	qplot.PNG
Views:	1
Size:	28.9 KB
ID:	1523291



                      Thus, I need confidence intervals for the mean regressions as well as for the quantile regressions. How can I achieve this, please?

                      Comment


                      • #26
                        In this case you need to blend the code from #4 and #19. I find it difficult to believe that the code you show in #25 actually produced the graph you show, or even any graph at all. Your -post handle- command only gives 2 expressions to post, but your -postfile handle- command requires 5 expressions. You should have just gotten an error message when you hit the -post handle- and no results at all. Assuming that somehow Stata just filled in the missing eexpressions with missing values, you still shouldn't have gotten the ub curve, becase no ub values ever got posted. What you need should look something like this:

                        Code:
                         
                        capture postutil clear tempfile results postfile handle int percentile float bx5 lb ub using `results' forvalues i = 5 (5) 95 { qreg2 y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) wlsiter(60) q(`=`i'/100')
                        matrix M = r(table) post handle (`i') (M[1, 5]) (M[5,5]) (M[6,5])
                        } postclose handle xtreg y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) re local x5 = _b[x5] use `results', clear label var bx5 "x5 coefficient" label var lb "lower limits" label var ub "upper limits" graph twoway connect bx5 lb ub percentile, sort /// yline(`x5')
                        Note: -qreg2- is not an official Stata command and I am not familiar with it. This code depends crucially on its returning results in r(table) just the way that official Stata estimation commands do.
                        Last edited by Clyde Schechter; 04 Nov 2019, 21:32.

                        Comment


                        • #27
                          Thank very much Clyde Schechter . The qreg2 appears to have the r(table) because I visually inspected the graph and it matches exactly with the points produced in the regression output (tables).


                          Click image for larger version

Name:	Capture2.PNG
Views:	1
Size:	43.2 KB
ID:	1523308






                          I have two quick issues:

                          (1). Is there still a way to impose the confidence interval for the mean estimate (from the xtreg)
                          as two horizontal lines
                          separately from that for the quantiles, as in the sample plot in #25?

                          (2). Or rather instead of (1), how do I shade the confidence interval as in #10? I tried doing that with rarea by changing the last line of the code as :
                          Code:
                          graph twoway rarea bx5 lb ub percentile, sort ///
                              yline(`x5')
                          But I got error:
                          Code:
                          rarea requires 3 variables: bx5 lb ub percentile
                          r(103);
                          
                          end of do-file
                          
                          r(103);

                          Comment


                          • #28
                            Code:
                            capture postutil clear
                            tempfile results
                            postfile handle int percentile float bx5 lb ub using `results'
                            
                            forvalues i = 5 (5) 95 {
                                qreg2 y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) wlsiter(60) q(`=`i'/100')
                            
                            matrix M = r(table)
                            post handle (`i') (M[1, 5]) (M[5,5]) (M[6,5])}
                            postclose handle
                            xtreg y x1 x2 x3 x4 x5 x6 x7 y2 - y5 if wanted1==5, cluster(unit) re
                            local x5 = _b[x5]
                            matrix M = r(table)
                            local lb = M[5,5]
                            local ub = M[6,5]
                            
                            use `results', clear
                            
                            label var bx5 "x5 coefficient"
                            label var lb "lower limits"
                            label var ub "upper limits"
                            
                            graph twoway connect bx5 lb ub percentile, sort ///
                                yline(`x5' `lb' `ub')

                            Comment


                            • #29
                              Dear Clyde Schechter ,

                              Sorry to bother you about this again. The last code produced the error below:

                              Code:
                              post:  4 expressions expected and more than 4 found
                              r(198);
                              
                              end of do-file
                              
                              r(198);

                              Comment


                              • #30
                                Looking closely at the -post- command, I see there is a } character at the end of the line, which should not be there. Sorry for the typo. I think if you remove that, you will be fine.

                                Comment

                                Working...
                                X