Announcement

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

  • Plot confidence interval for median

    I'd appreciate if anyone could help me with this one. I need to plot median and percentile confidence intervals (2.5 and 97.5) instead of mean and CIs (I basically need an alternative of ciplot)

    ciplot continuous_variable, by( time_period)

    Here is the dataset.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id float(time_period continuous_variable)
     1 0 18
     1 1 13
     1 2  5
     2 0 18
     2 1 57
     2 2 43
     3 0  0
     3 1 38
     3 2  0
     4 1 63
     4 2  5
     7 0 26
     7 1 11
     7 2  8
     7 3  6
     8 0 67
     8 1 39
     8 2 65
    12 0  6
    12 1 22
    12 2 22
    15 0  5
    15 1 15
    16 0 21
    16 1 98
    16 2 56
    17 0 15
    17 1 12
    17 2 38
    17 3 10
    18 0 41
    18 1 42
    19 0 14
    19 1 13
    21 0  3
    21 1  9
    21 2  9
    end
    label values time_period a
    label def a 0 "first", modify
    label def a 1 "second", modify
    label def a 2 "third", modify
    label def a 3 "fourth", modify
    Thank you
    Last edited by gani davlatyov; 31 Mar 2019, 23:32.

  • #2
    ciplot is from SSC, as you are asked to explain (FAQ Advice #12).

    Its help already gives you most of an answer:

    (note added August 2011)

    For fuller flexibility, consider using statsby first and then standard graphics commands (Cox 2010).

    Cox, N.J. 2010. Speaking Stata: The statsby strategy. Stata Journal 10: 143-151.
    I've added a plot of the raw data, with whiskers 2.5% and 97.5% percentiles added to a box plot. For that, stripplot must be installed from SSC.

    Code:
    set scheme s1color 
    
    * ssc install stripplot 
    stripplot cont, over(time) box(barw(0.1)) pctile(2.5) boffset(-0.2) vertical yla(, ang(h)) width(2) ms(Sh) stack height(0.1) name(G1, replace) 
    
    statsby median=r(c_1) upper=r(ub_1) lower=r(lb_1), by(time) saving(medianci, replace) : centile cont 
    
    use medianci , clear 
    scatter median time, pstyle(p1) || rcap upper lower time , xla(0/3, valuelabel) pstyle(p1) name(G2, replace) ///
    legend(off) ytitle(continuous_variable) note(medians and 95% confidence intervals) yla(, ang(h))

    Click image for larger version

Name:	median_ci_1.png
Views:	1
Size:	20.7 KB
ID:	1491097

    Click image for larger version

Name:	median_ci_2.png
Views:	1
Size:	19.9 KB
ID:	1491098

    Comment


    • #3
      Thank you Dr. Cox. This is really informative.

      Comment

      Working...
      X