Announcement

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

  • Twoway kernel density plots with confidence intervals.

    Goal: Plot kernel densities for two samples with confidence intervals.

    Following the documentation of 'kdens' at http://fmwww.bc.edu/repec/bocode/k/kdens.html I am able to plot kernel densities for two subsamples in the following two ways (using Stata 14 MP on macOS Mojave):

    Code:
    use http://www.stata-press.com/data/r7/trocolen.dta
    generate byte g = uniform()<.5
    * first alternative:
    twoway kdens length if g==1 || kdens length if g==0
    * second alternative:
    kdens length if g==1, addplot(kdens length if g==0)
    I am also able to use 'kdens' to plot one kernel density with its confidence interval:

    Code:
    kdens length if g==0 , ci
    Unfortunately, the combination of these approaches is unsuccessful.

    Code:
    . twoway kdens length if g==1, ci || kdens length if g==0, ci
    option ci not allowed
    r(198);
    
    . kdens length if g==1, ci addplot(kdens length if g==0, ci)
    (n2() set to 316)
    (bandwidth = 51.439651)
    option ci not allowed
    r(198);
    The second approach can be modified for partial success. The following plots both kernels and the confidence interval only for the primary 'kdens':

    Code:
    kdens length if g==1, ci addplot(kdens length if g==0)
    I believe that a solution is possible by analogy to this discussion of a similar problem using 'lpoly' on Statalist in 2008:
    https://www.stata.com/statalist/arch.../msg00675.html
    Last edited by Arthur Morris; 02 Nov 2018, 02:11.

  • #2
    The community-contributed command kdens calls up graph twoway but is not a twoway type, so it can't be used as such, including as a command called up by addplot().

    As I understand it you need to call up kdens with ci and generate() options and then directly plot the variables so produced.

    Comment


    • #3
      Sorry, #2 is wrong in part, as exceptionally kdens is also written as twoway kdens. Something like this may help, however:

      Code:
      use http://www.stata-press.com/data/r7/trocolen.dta, clear 
      generate byte g = uniform()<.5
      
      kdens length if g==1, gen(d1 x1) ci(ci1_1 ci1_2) 
      kdens length if g==0, gen(d0 x0) ci(ci0_1 ci0_2) 
      
      twoway rarea ci0_1 ci0_2 x0 , color(orange%20) || ///
      rarea ci1_1 ci1_2 x1 , color(blue%20) || ///
      line d1 x1, lc(orange) || line d0 x0, lc(blue) /// 
      legend(order(3 "group 0" 4 "group 1") pos(11) ring(0) col(1))

      Comment


      • #4
        Thank you, Nick. This approach allows me to plot kernel densities for two samples with confidence intervals.

        Comment

        Working...
        X