Announcement

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

  • Question about the command --localp-- @Nick Cox

    Nick Cox Dear Nick, I have a question about your command --localp--.I want to plot two graphs in one graph. I tried to use addplot(),but this command does
    not support.here is an example.
    Code:
    sysuse auto,clear
    localp price mpg if foreign==1,scheme(s1mono) lineopt(lp(solid)) ///
    addplot(localp price mpg if foreign==0,scheme(s1mono) lineopt(lp(shortdash_dot)))
    localp is not a twoway plot type
    r(198);
    So i have tried another way.
    Code:
    sysuse auto,clear
    localp price mpg if foreign==1,nograph gen(l1)
    localp price mpg if foreign==0,nograph gen(l0)
    twoway line l1 l0 mpg ,sort lp(solid shortdash_dot) ///
           ||scatter price mpg if foreign==1 ,m(O) msize(small) ///
           ||scatter price mpg if foreign==0,m(Oh) msize(small)  ///
           ,legend(order(3 "Foreign Car Price" 4 "Domestic Car Price") rows(2))  ///
           scheme(s1mono)
    This graph is just waht I want.But I think it is not a good way.Do you have a simpler way to get this graph?

    Best
    Raymond
    Attached Files
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

  • #2
    What you want from localp makes perfect sense, but its syntax is not as yet supportive.

    Stata is telling you what the problem is. localp (from SSC, as you are asked to explain: FAQ Advice #12) is a wrapper for lpoly. so like very many community-contributed graph commands, it uses graph twoway but is not thereby a twoway command, so it can't be used in an addplot() call -- or with the graph syntax || or its equivalent using ()

    https://www.statalist.org/forums/forum/general-stata-discussion/general/1290645-localp-downloadable-from-ssc-for-local-polynomial-regression gives the very short story here. In my own use and in teaching I found the defaults of lpoly unhelpful. Compared with what I wanted it often under-smooths and it calculates local means whereas I usually prefer local lines. Also, my students have many worries and concerns and I wouldn't want to add to a long list the burden of spelling Epanechnikov correctly (Apologies to anyone who thinks this is an easy name to spell.)

    Your approach is indeed not ideal, mostly because it is indirect but also because
    localp may choose different bandwidths for different groups considered separately. I would recommend smoothing similarly, or, if you have good reason to the contrary, certainly recommend making that explicit.

    Accepting graph defaults but never explaining them to readers is poor practice, although remarkably common.

    I would do something more like this:

    Code:
    sysuse auto,clear
           
           
    local opts sort bw(5) kernel(biweight) degree(1)
    
    twoway lpoly price mpg if foreign , `opts' lp(solid) lc(red) || lpoly price mpg if !foreign, `opts' lp(dash) lc(blue) ///
    || scatter price mpg if foreign, ms(Oh)  mc(red) || scatter price mpg if !foreign, ms(+) mc(blue) legend(order(1 "Foreign" 2 "Domestic") pos(1) col(1) ring(0))  ///
    ytitle("Price (USD)")  xtitle("Miles per gallon") note(biweight kernel; bandwidth 5; degree 1)

    Comment


    • #3
      Dear Nick,
      Why do you choose bw(5)?In the command lowess,I find that the default bandwidth is between 0 and 1,and I can't set bw to 5.
      How can I choose the correct bandwidth?
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        I am not using lowess but as said lpoly and the bandwidth is defined quite differently in the two commands.

        Comment


        • #5
          I am not using lowess but as said lpoly and the bandwidth is defined quite differently in the two commands.

          Comment


          • #6
            Thank you,Nick.You help me so much.
            Best regards.

            Raymond Zhang
            Stata 17.0,MP

            Comment

            Working...
            X