Announcement

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

  • Hard Axis Range

    I have a graph with multiple confidence intervals. And some confidence intervals are so big that the figure is scaled so that the small confidence appear only as dots. Is it possible to use a hard x range and just cut out the ends of the big confidence intervals? I tried xscale, but it doesn't help.

  • #2
    Maybe this helps.

    https://www.statalist.org/forums/for...ence-intervals

    Comment


    • #3
      Andrew Musau I am not sure if I can apply what you did in the other example. This is the code I use.


      Code:
      gen       label=.  
      replace label=0  if study=="A"
      replace label=1  if study=="B"
      replace label=2  if study=="C"
      replace label=3  if study=="D"
      
      *CI
      twoway rcap upper lower label if label<25, horizontal||scatter label point if label<25, ///
      title("Estimated Effect of $1000 Increase in Income on", size(medium) span) sub("Predicted Earnings", size(medium) span)  ///
       ytitle("") xtitle("Percent Change", size(small)) xline(0, lp(dash)) msymbol(circle) legend(off)  ///
       graphregion(color(white)) msize(small) xsize(8.5) xscale(range(-1 2)) ysize(20) yscale(range(0 20)) ///
      ylabel( ///
      0  `""Study A""' ///
      1  `""Study B""' ///
      2  `""Study C""' ///
      3  `""Study D""' ///
      , labsize(vsmall) angle(0) notick) yscale(reverse) note("Notes: Figure showing...", size(vsmall) span)
      
      graph export example.pdf, replace
      I would like the x scale to be -1.0 to 2.0 and all confidence intervals that go beyond -1.0 and 2.0 should be cutoff at -1.0 or 2.0. Could you please help?

      Comment


      • #4
        There is so far no data example we can use but the problem is clear. Neither xscale() nor yscale() will cause graph to ignore data. StataCorp has a minor aversion to documenting what specific syntax doesn't do but -- apart from emphasising that range() is for extending an axis -- this is also explicit in the help for axis scale options:


        range() never narrows the scale of an axis or causes data to be omitted from the plot.
        The implication, unfortunately or not, is that you need to take control and truncate intervals you don't want to be shown.

        This example isn't general or bullet-proof but it shows some technique.

        Code:
        sysuse auto, clear
        
        set scheme s1color
        
        capture program drop myciplot
        
        capture program myciplot
            version 15
            syntax varlist(numeric max=1), by(varname) [ MAXimum(numlist max=1) MINimum(numlist max=1) * ]
            if "`maximum'`minimum'" == "" {
                di as err "should specify one or both of minimum() or maximum()"
                exit 498
            }
            
            preserve
            
            statsby, by(`by') clear : ci means `varlist'
            
            if "`minimum'" != "" {
                tempvar lb2
                gen `lb2' = max(lb, `minimum')
            }
            else local lb2 lb
            
            if "`maximum'" != "" {
                tempvar ub2
                gen `ub2' = min(ub, `maximum')
            }
            else local ub2 ub
            
            twoway rspike `lb2' `ub2' `by', lc(black) ///
            || rcap `lb2' `lb2' `by' if `lb2' == lb, lc(black) ///
            || rcap `ub2' `ub2' `by' if `ub2' == ub, lc(black) ///
            || scatter mean `by', legend(off) `options' ///
            || scatter `lb2' `by' if `lb2' != lb, ms(V) mc(black) ///
            || scatter `ub2'  `by' if `ub2' != ub, ms(a) msize(*2) mc(black)
        end
        
        myciplot mpg, by(rep78) min(0) max(50) ytitle("`: var label mpg'") yla(, ang(h)) xsc(r(0.8 5.2)) subtitle(95% confidence intervals for means)
        Click image for larger version

Name:	truncated_ci_plot.png
Views:	1
Size:	19.2 KB
ID:	1567083


        Comment


        • #5
          Along the same lines as Nick Cox, but not as outstanding.

          Code:
          sysuse auto, clear
          qui regress mpg i.rep78 weight
          qui margins, at(rep78=(1 2 3 4 5))  saving(myfile, replace)
          marginsplot, scheme(s1color) saving(gr1, replace)
          use myfile, clear
          *SET MIN AT 17 AND MAX 24
          gen _ci_lb2= cond(_ci_lb <17, 17, _ci_lb)
          gen _ci_ub2= cond(_ci_ub >24, 24, _ci_ub)
          twoway (rcap _ci_lb2 _ci_ub2 _at1, sort pstyle(ci) color(green) ylab(18(2)24)) ///
          (connected _margin _at1, lpattern(solid) lcolor(green) mcolor(green) ///
          scheme(s1color)) (scatteri 24 0.95 24 5.25, recast(line) lcolor(white) lwidth(medthick)) /// 
          (scatteri 17 0.95 17 5.25, recast(line) lcolor(white) lwidth(medthick) leg(off) ///
          xtitle("Repair Record 1978") ytitle("Linear Prediction")  ///
          title("Predictive Margins with 95% CI") ///
          note("Note: CIs without caps indicate truncation") saving(gr2, replace))
          gr combine gr1.gph gr2.gph, scheme(s1color)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	77.6 KB
ID:	1567086

          Comment


          • #6
            Using spikes rather than caps seems to me be a good convention. I am not wedded to arrows, despite https://www.stata-journal.com/articl...article=gr0015

            Comment


            • #7
              Thank you Nick Cox Andrew Musau I will try it out!

              Comment

              Working...
              X