Announcement

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

  • (parallel?) loop over varlist and thresholds while making graphs

    I am looping over a varlist to draw histograms. To each graph, I want to add an -xline- for the low and high extreme values (which I determine separately for each variable). I was wondering if there is a way to efficiently code this in a parallel loop? I have been playing around with the following code where I have defined three locals: the first is the var, which lists two variables that are to be looped over, the second and third macros are called high and low: these list the high and low extreme values of each variable in var (in the order that the variables are listed in var).

    I am able to loop over var-- but how do I also loop over high and low sequentially?So that when the histogram command is graphing variable x, it takes the first element of high and low to make xlines etc.

    Many thanks!

    Code:
    local var x y
    local high 100000 200000
    local low 10 20
    foreach v of var `var14' {
        #d ;
        histogram `v', frequency bin(50) color(red%30) ti("`v'", size(medsmall)) plotr(m(zero))
        xlabel(#15, angle(90) nogrid labsize(vsmall)) xtitle("") xmtick(#15, grid tstyle(none)) xscale(noline)
        ylabel(#10, labsize(vsmall) nogrid angle(0)) ytitle("") ymtick(#10, grid tstyle(none)) yscale(noline)
        xline(`high', lpattern(dash) lcolor(blue%40))
        xline(`low', lpattern(dash) lcolor(blue%40)) 
        scheme(s2mono) plotregion(color(gray%30)) graphregion(color(white)) aspectratio(1) ;
        #d cr
        graph export "`v'_test.png", replace
    }

  • #2
    What is in local var14 ?

    Comment


    • #3
      Sorry, that was my mistake. But this works!

      Code:
      local var14 " j6_monthly_income_3 b12_cow_count"
      local max "100000 200"
      local min "100 2"
      local n : word count `var14'
      forvalues i = 1/`n' {
            local a : word `i' of `var14'
            local b : word `i' of `max'
            local c : word `i' of `min'
          #d ;
          histogram `a', frequency bin(50) color(red%30) ti("`v'", size(medsmall)) plotr(m(zero))
          xlabel(#15, angle(90) nogrid labsize(vsmall)) xtitle("") xmtick(#15, grid tstyle(none)) xscale(noline)
          ylabel(#10, labsize(vsmall) nogrid angle(0)) ytitle("") ymtick(#10, grid tstyle(none)) yscale(noline)
          xline(`b', lpattern(dash) lcolor(blue%40))
          xline(`c', lpattern(dash) lcolor(blue%40))
          scheme(s2mono) plotregion(color(gray%30)) graphregion(color(white)) aspectratio(1) ;
          #d cr
          graph export "`a'_test.png", replace
      }

      Comment


      • #4
        Question of taste and style supervene, but we might suggest principles such as

        1. Don't use a macro if it doesn't simplify code.

        2. Don't set up a loop counter if Stata will loop for you any way.

        Thus your code could be rewritten

        Code:
        local max "100000 200"
        local min "100 2"
        foreach a in  j6_monthly_income_3 b12_cow_count {
              gettoken b max : max
              gettoken c min : min
              ...     
        }
        Another technique is to summarize the variable and then round the extremes up and down to "nice" values.

        Comment


        • #5
          Ah, that's much neater, thanks for the suggestion, Nick!

          Comment

          Working...
          X