Announcement

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

  • How to plot the below graphs after quantile regression?

    Please could anyone tell how to plots the same graphs after quantile regression? I have tried to plots with "grqreg" command but i got different graphs. Please help me thanks.

    Click image for larger version

Name:	Untitled.jpg
Views:	3
Size:	129.2 KB
ID:	1483206

  • #2

    Code:
    sysuse auto, clear
    sqreg price weight length foreign, quantile(.1 .2 .3 .4 .5 .6 .7 .8 .9) reps(100)
    
    preserve
    gen q = _n*10 in 1/9
    
    foreach var of varlist weight length foreign {
        gen _b_`var'  = .
        gen _lb_`var' = .
        gen _ub_`var' = .
    
        local i = 1
        foreach q of numlist 10(10)90 {
            replace _b_`var' = _b[q`q':`var'] in `i'
            replace _lb_`var' = _b[q`q':`var'] - _se[q`q':`var']*invnormal(.975) in `i'
            replace _ub_`var' = _b[q`q':`var'] + _se[q`q':`var']*invnormal(.975) in `i++'
        }
    }
    keep q _b_* _lb_* _ub_*
    keep in 1/9
    reshape long _b_ _lb_ _ub_, i(q) j(var) string
    set scheme s1color
    twoway rarea _lb_ _ub_ q, astyle(ci) yline(0) acolor(%90) || ///
       line _b_ q,                                               ///
       by(var, yrescale xrescale note("") legend(at(4) pos(0)))  ///
       legend(order(2 "effect"                                   ///      
                    1 "95% confidence" "interval")               ///
              cols(1))                                           ///
       ytitle(effect on percentile of price)                       ///
       ylab(,angle(0) format(%7.0gc))                            ///    
       xlab(10(10)90) xtitle(percentile of price)
    restore
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	86.6 KB
ID:	1483214
    Last edited by Maarten Buis; 12 Feb 2019, 02:12.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks but i found the followings errors.

      . twoway rarea _lb_ _ub_ q, astyle(ci) yline(0) acolor(%90) || ///
      / / / is not a twoway plot type
      r(198);

      .
      . line _b_ q, ///
      option / not allowed
      r(198);

      .
      . by(var, yrescale xrescale note("") legend(at(4) pos(0))) ///
      : required
      r(100);

      .
      . legend(order(2 "effect" ///
      command legend is unrecognized
      r(199);

      .
      . 1 "95% confidence" "interval") ///
      1 is not a valid command name
      r(199);

      .
      . cols(1)) ///
      command cols is unrecognized
      r(199);

      .
      . ytitle(effect on percentile of price) ///
      command ytitle is unrecognized
      r(199);

      .
      . ylab(,angle(0) format(%7.0gc)) ///
      command ylab is unrecognized
      r(199);

      .
      . xlab(10(10)90) xtitle(percentile of price)
      command xlab is unrecognized
      r(199);

      Comment


      • #4
        Originally posted by Zeeshan Fareed View Post
        Thanks but i found the followings errors.

        . twoway rarea _lb_ _ub_ q, astyle(ci) yline(0) acolor(%90) || ///
        / / / is not a twoway plot type
        r(198);

        .
        . line _b_ q, ///
        option / not allowed
        r(198);

        .
        . by(var, yrescale xrescale note("") legend(at(4) pos(0))) ///
        : required
        r(100);

        .
        . legend(order(2 "effect" ///
        command legend is unrecognized
        r(199);

        .
        . 1 "95% confidence" "interval") ///
        1 is not a valid command name
        r(199);

        .
        . cols(1)) ///
        command cols is unrecognized
        r(199);

        .
        . ytitle(effect on percentile of price) ///
        command ytitle is unrecognized
        r(199);

        .
        . ylab(,angle(0) format(%7.0gc)) ///
        command ylab is unrecognized
        r(199);

        .
        . xlab(10(10)90) xtitle(percentile of price)
        command xlab is unrecognized
        r(199);
        Zeeshan,

        You have to copy Maarten's code into a do file, then execute the whole block. It doesn't work if you copy and paste line by line.

        /// is a line break. That tells Stata that the code continues on the next line in a do file, but it's only recognized in do files.
        Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

        When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

        Comment


        • #5
          Thanks all, i have done.

          Comment


          • #6
            @Maarten Buis I have these quantiles q(0.05 0.10 .20 0.30 0.40 .50 0.60 .70 0.80 0.90 0.95).
            What will be the revised codes for drawing quantile coefficient graph?
            Specially for your given following codes;

            preserve gen q = _n*10 in 1/9 foreach var of varlist weight length foreign { gen _b_`var' = . gen _lb_`var' = . gen _ub_`var' = . local i = 1 foreach q of numlist 10(10)90 { replace _b_`var' = _b[q`q':`var'] in `i' replace _lb_`var' = _b[q`q':`var'] - _se[q`q':`var']*invnormal(.975) in `i' replace _ub_`var' = _b[q`q':`var'] + _se[q`q':`var']*invnormal(.975) in `i++' } } keep q _b_* _lb_* _ub_* keep in 1/9 reshape long _b_ _lb_ _ub_, i(q) j(var) string set scheme s1color twoway rarea _lb_ _ub_ q, astyle(ci) yline(0) acolor(%90) || /// line _b_ q, /// by(var, yrescale xrescale note("") legend(at(4) pos(0))) /// legend(order(2 "effect" /// 1 "95% confidence" "interval") /// cols(1)) /// ytitle(effect on percentile of price) /// ylab(,angle(0) format(%7.0gc)) /// xlab(10(10)90) xtitle(percentile of price) restore


            Comment


            • #7
              Originally posted by Maarten Buis View Post
              Code:
              sysuse auto, clear
              sqreg price weight length foreign, quantile(.1 .2 .3 .4 .5 .6 .7 .8 .9) reps(100)
              
              preserve
              gen q = _n*10 in 1/9
              
              foreach var of varlist weight length foreign {
              gen _b_`var' = .
              gen _lb_`var' = .
              gen _ub_`var' = .
              
              local i = 1
              foreach q of numlist 10(10)90 {
              replace _b_`var' = _b[q`q':`var'] in `i'
              replace _lb_`var' = _b[q`q':`var'] - _se[q`q':`var']*invnormal(.975) in `i'
              replace _ub_`var' = _b[q`q':`var'] + _se[q`q':`var']*invnormal(.975) in `i++'
              }
              }
              keep q _b_* _lb_* _ub_*
              keep in 1/9
              reshape long _b_ _lb_ _ub_, i(q) j(var) string
              set scheme s1color
              twoway rarea _lb_ _ub_ q, astyle(ci) yline(0) acolor(%90) || ///
              line _b_ q, ///
              by(var, yrescale xrescale note("") legend(at(4) pos(0))) ///
              legend(order(2 "effect" ///
              1 "95% confidence" "interval") ///
              cols(1)) ///
              ytitle(effect on percentile of price) ///
              ylab(,angle(0) format(%7.0gc)) ///
              xlab(10(10)90) xtitle(percentile of price)
              restore
              [ATTACH=CONFIG]n1483214[/ATTACH]

              Hello sir. I wish to use the code that you have shared, but my concerns are RAREA level, because I use several variables.
              his gives me the following result: "rarea requires 3 variables: _lb_ _ub_ _jb_ q"

              Comment


              • #8
                You did not tell us what your code is, so it is to know what is going on with your code. I guess you thought that adding a variable requires you to add a variable to rarea. This is incorrect. Instead you change the line sqreg price weight ... to whatever variables you need and the line foreach var of varlist weight length foreign to whatever variables you need. After that it should work. If it does not, tell us your exact code and give us an extract from your data.
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment


                • #9
                  Originally posted by Maarten Buis View Post
                  You did not tell us what your code is, so it is to know what is going on with your code. I guess you thought that adding a variable requires you to add a variable to rarea. This is incorrect. Instead you change the line sqreg price weight ... to whatever variables you need and the line foreach var of varlist weight length foreign to whatever variables you need. After that it should work. If it does not, tell us your exact code and give us an extract from your data.
                  Hi,
                  Can I have contact for you to send my database?
                  sqreg s4_09 no_educ s1_05 s1_03 genre AV Eq_agri s1_09_1, quantile (.1 .2 .3 .4 .5 .6 .7 .8 .9) reps(100)

                  Comment


                  • #10
                    A couple of comments:
                    1. When I asked for your code, I did not just ask for your model. You wanted help with creating a graph, so we need to see all the code that you used to try to create that graph. this sounds a bit picky, but the problem you have is one of coding, and the problem there is often in the tiniest of details. So to figure out what the problem is, we really need to know all the details.
                    2. On Statalist we give (whenever possible) access to data by copying extracts from our data using dataex. If that is not possible (e.g. for legal or privacy reasons) then we create artificial data with essentially the same structure as your data. This way any answer I give you not only benefits you, but also other persons who have a similar problem and find this post.

                    More general points that are not related to your problem:
                    1. I would use informative variable names. Renaming variables like s4_09 to something human readable will in the long run prevent errors and allow you to work quicker.
                    2. I used a 100 bootstrap replications to get results quickly, not because I thought that was a sensible choice. In most case I would say that it is not enough.

                    ---------------------------------
                    Maarten L. Buis
                    University of Konstanz
                    Department of history and sociology
                    box 40
                    78457 Konstanz
                    Germany
                    http://www.maartenbuis.nl
                    ---------------------------------

                    Comment


                    • #11
                      Hi there,

                      I have a follow-up question on the commands in #2: They work for me, but only if I use the sqreg command, not with xtqreg. Code runs until
                      replace _b_`var' = _b[q`q':`var'] in `i' then error message "equation q10 not found" appears.

                      Is it possible to also depict the xtqreg results in the graphical way above?

                      Best,
                      Carlo

                      Comment


                      • #12
                        Hi Carlo
                        did you try qregplot (from ssc). It should be easy to make this kind of graphs with that command
                        HTH
                        F

                        Comment


                        • #13
                          Hi Fernando,

                          That works, thanks a lot for this hint! Helps a lot.

                          Best
                          Carlo

                          Comment

                          Working...
                          X