Announcement

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

  • loop over histogram incorporating scalar

    Hi I'm coding a series of histograms at each value of a factor variable. This works fine, per syntax below:

    local x DIS1-DIS50
    foreach y of varlist `x' {
    hist distance if year ==5 & `y'==1, normal ///
    title ("Histogram of DIS score 2022/23:" "`y'")
    graph export "dis_hist/`y'.png", replace
    }

    In addition, I want to add a line to indicate the mean and median, taking these values from the return list in the summarise command:
    (this applies to the full distribution of distance, my loop breaks this out into teh dis groups)

    sum distance, d
    hist distance if year ==5 ///
    xline(`r(mean)', lcolor(red) lwidth (vthin)) ///
    xline(`r(p50)', lcolor(gs4) lwidth (vthin)) ///

    I'm having trouble thinking about how to incorporate this instruction within the loop, so that each histogram shows the xline at mean and median. Do you have any advice on how I could do this? I'm feeling my way a bit with loops

    Thanks.

    Brendan

  • #2
    Code:
    local x DIS1-DIS50
    foreach y of varlist `x' {
        qui sum distance if `y'==1, d
        hist distance if year ==5 & `y'==1  ///
        xline(`r(mean)', lcolor(red) lwidth (vthin)) ///
        xline(`r(p50)', lcolor(gs4) lwidth (vthin)) ///
        title ("Histogram of DIS score 2022/23:" "`y'") ///
        graph export "dis_hist/`y'.png", replace
    }

    Comment


    • #3
      Great, thank you.

      Brendan

      Comment

      Working...
      X