Announcement

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

  • Storing means of a categorical variable and using them as reference line

    Greetings.

    I have a dataset containing individual grades and indexes by institution and reference group (some institutions can belong to multiple reference groups). I need to display a scatter plot of the mean index and mean grade by institution and reference group, also adding the reference group grade mean as a horizontal reference line and the reference group index mean as a vertical reference line.

    So, I created the means by institution and reference group by using:

    bys inst_code refgroup: egen m_grade = mean(grade)
    bys inst_code refgroup: egen m_index = mean(index)

    And then proceeded to loop over the values of refgroup in order to make the graphs:

    su refgroup, meanonly
    forvalues i=1/`r(max)'{
    twoway (scatter m_grade m_index if refgroup== `i'), ytitle (Mean Grade) xtitle(Mean Index) xline(?) yline(?) ylabel(,nogrid) title(`i')
    graph save gr`i', replace
    }

    I tried typing before the loop this:
    bys refgroup: sum grade, meanonly
    local a=r(mean)
    bys refgroup: sum index, meanonly
    local b=r(mean)

    And then using them locals as arguments for xline and yline, but this would only take the mean grade of the last value from refgroup and use it for every graph. To sum up, I need help to store the means by refgroup correctly.

    Kind regards,

    MB.
    Last edited by Mateo Barraza; 26 Aug 2018, 00:25.

  • #2
    Welcome to Statalist.

    Without having tested this idea, perhaps it will indicate a useful direction to take in modifying your current code.
    Code:
    su refgroup, meanonly
    forvalues i=1/`r(max)'{
    sum grade if refgroup== `i', meanonly
    local a=r(mean)
    sum index if refgroup== `i', meanonly
    local b=r(mean)
    twoway (scatter m_grade m_index if refgroup== `i'), ytitle (Mean Grade) xtitle(Mean Index) xline(`b') yline(`a') ylabel(,nogrid) title(`i')
    graph save gr`i', replace
    }

    Comment

    Working...
    X