Announcement

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

  • graph: mean and 95% conf inv, set of variables in columns

    As the title suggests, I am looking for a way to create a graph, with bars for mean and overlapped 95% conf. inv. for a set of variables, in one graph (dataset appended).

    I have previously managed to use tips on overlapping twoway (bar) (rcap) for instances when data was used from one variable (in one column), and split into separate bars for separate groups. I append an example of that, to show the format of graph I am looking to create. The procedure I used for that included creating new variables for mean, sd, upper and lower conf bounds as follows:

    HTML Code:
    egen meanA = mean(A), by(Group)
    egen nA = count(A), by(Group)
    egen sdA = sd(A), by(Group)
    gen upperA = meanA + invttail(nA-1,0.025) * sdA / sqrt(nA)
    gen lowerA = meanA - invttail(nA-1,0.025) * sdA / sqrt(nA)
    twoway (bar meanA Group) (rcap upperA lowerA Group)
    This time, I want individual bars and conf inv. for variables, not groups. Or simply put, the x bar should have variables A through I, without any grouping within variables. I can't figure out an alternative. I can get bars for means with below code but the 'rcap' overlay doesn't seem to be an option here.

    HTML Code:
    graph bar (mean) A B C D E F G H I
    I am looking for an exact copy of the format because both graphs will go into one manuscript.



    Attached Files

  • #2
    Jorrit,

    Try this:

    Code:
    use "150505 - Copy.dta"
    
    drop Year CtryIDNR Group meanA nA sdA upperA lowerA
    
    xpose, clear v
    
    egen group = group(_v), label
    drop _v
    
    egen mean=rowmean(v*)
    egen n = rownonmiss(v*)
    egen sd = rowsd(v*)
    gen upper = mean + invttail(n-1,0.025) * sd / sqrt(n)
    gen lower = mean - invttail(n-1,0.025) * sd / sqrt(n)
    
    // graph
    tw (bar mean group) (rcap upper lower group), legend(order(1 "mean" 2 "CI")) xti("") xlab(1(1)9)
    Note that

    Code:
    egen... rowmean()/rowsd()
    do not include missing values in their calculations, so the results you get differ from those of

    Code:
    egen... mean()/sd()

    Comment


    • #3
      Thanks for that.
      Now that I think of data transformations as a workaround, I could also make two columns, one with all observations copied vertically and a second one with the original variable name to use as a group, then followed by the original code to create the graph, it seems.
      Either option would require using a separate sheet for creating this figure. That's not a issue, but I just wasn't thinking in that direction.

      Thanks again!

      Comment

      Working...
      X