Announcement

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

  • How to plot a time series of a mean value by group, with confidence intervals?

    Dear Statalist:

    This seems like a simple question, but I can't seem to find an answer.

    I am using Stata 13.1.

    I have a monthly time series data set involving employees. The data set is at the employee-month level. The variable of interest is sales. Each employee belongs to one of two groups, X and Y.

    I would like to create a time series graph showing (A) average monthly sales, (B) by group, (C) with 95% confidence intervals superimposed as areas.

    I can achieve (A) and (B) with the following:

    Code:
    tsset employee month, monthly
    collapse (mean) sales, by(group month)
    tsline sales if group=="X" || tsline sales if group=="Y"
    However, I can't figure out how to accomplish (C). I also suspect my method is not the most elegant for (A) and (B).

    Any help would be much appreciated!

    Thank you,
    Jennifer

  • #2
    I'd use statsby to drive ci to get the means and confidence intervals. Then you have time series. tsset does no harm here, but it doesn't help much with this specific problem. Note for example that your tsset structure is quite different from the structure you want for the means and confidence limits, You could start with

    Code:
     
    statsby mean=r(mean) ub=r(ub) lb=r(lb), by(group month) : ci sales
    Note that confidence intervals from ci pay no attention here to any dependence structure in your data.

    Comment


    • #3
      This works great -- thank you as always, Prof. Cox!

      Comment

      Working...
      X