Announcement

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

  • Formatting r(mean) for use in graphs

    Hello,

    I am trying to automate the generation of graphs that highlight the coordinates where two lines intersect.

    I have written the following piece of code:
    Code:
    sum x if xcosteffective ==1
    
    
    twoway line  x wtp_threshold if wtp_threshold>0 & 6000>=wtp_threshold, xlabel(0(500)6000) xmtick(0(250)3500) ylabel(0(.5)1) ymtick(0(.25)1) ///
    xline(497, lcol(gs10) lp(dash)) xscale(r(0 3500)) ///
      yline(`r(mean)' ,  lcol(gs10) lp(dash))    ytitle("Prob(cost-effective)") xtitle("Willingness to pay threshold  (US$ 2018)")  xscale(titlegap(*+20)) ///
      title("Females only")  text(`r(mean)'  4000 "(Threshold: US$ 4000," "Prob. C-Eff: `r(mean)')", color(magenta) size(small) placement(ne)) scheme(s1color) lcolor(magenta) ///
        graphregion(color(white))  plotregion(lcolor(white) lwidth(medium))

    The challenge I have is that `r(mean)' returns a huge amount of decimal points (about 16). I tried a few ways of generating a new variable, including with
    Code:
    gen mean_cmb_prova=round(`r(mean)',.01)
    sum mean_cmb_prova
    but because Stata is so precise, even in this case r(mean) has about 16 decimal points, with a bunch of zeros between the first two and last five or six decimal points x-)
    Is there any way to get r(mean) to keep the format I want? As you may infer from the second bit of code I want two decimal points only. Ideally a percentage format, in fact, but would not dare ask for too much...

    Any suggestions would be much appreciated.

    Thanks,
    Giulia

    P.S.: I also tried generating an entire new y constant=r(mean), but this squashes the graph, which shold be all about the x variable above
    Last edited by Dr Giulia FERRARI; 19 Feb 2020, 04:50. Reason: Added a few tags to increase the likelihood of a response

  • #2
    round() with second argument a fraction less than 1 almost never does what people think it will or should do. It is not a formatting function.

    You need after summarize something more like

    Code:
    local toshow : di %3.2f r(mean)
    and then to use that local macro rather than `r(mean)' in the code.

    Comment


    • #3
      This is brilliant!

      I can even get the percentage to show automatically, now, by then simply typing
      Code:
      text(`r(mean)'   4000 "(US$ 4000, `=`toshow'*100'%)"
      Thanks very much, Nick. It's a little liberation....
      Giulia

      Comment

      Working...
      X