Announcement

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

  • How I can add 95% CI error bar to my multiple line graph

    Hi,

    First, I have used the collapse command to make the mean and standard deviation by age and SES for smoking.
    Second, I have made the upper and lower values of the confidence interval for smoking.
    Third, I have used the command:
    xtset SES age

    Fourth, I have used the command:
    xtline smoking, overlay

    Now, I have the figure I am looking for without error bars.
    How I can add 95% error bars to each age group across all SES?

    I really appreciate your help.

  • #2
    Probably there's a smarter way, but you can use a combination of twoway line and twoway rcap, e.g.
    Code:
    twoway (line smoking age) (rcap upper_smoking lower_smoking age)
    where "upper_smoking" and "lower_smoking" are the confidence intervals' upper and lower bounds (as a side, are you computing the standard error of the mean, dividing the standard deviation by the square root of the number of observations?). If you want to plot the lines by SES, just add an "if" statement at the end (e.g. "if ses==1").

    Comment


    • #3
      Thanks for your suggestions. However, I have now only the proportion of smoking across age groups (only one line). How I can add 95% error bars to each age group across all SES (multiple lines)?

      I have used this command for CI:
      generate losmoking = meansmoking - invttail(n-1,0.025)*(sdsmoking / sqrt(n))
      generate hismoking = meansmoking + invttail(n-1,0.025)*(sdsmoking / sqrt(n))
      Last edited by Zahra Roustaei; 11 Feb 2020, 12:29.

      Comment


      • #4
        I have used this command for CI:
        generate losmoking = meansmoking - invttail(n-1,0.025)*(sdsmoking / sqrt(n))
        generate hismoking = meansmoking + invttail(n-1,0.025)*(sdsmoking / sqrt(n))

        Comment


        • #5
          It's difficult without having a look at the data, but I think it's enough you specify an if statement within parentheses. So - assuming "ses" can take on two values - something like
          Code:
          twoway (line smoking age if ses==1) (rcap losmoking hismoking age if ses==1) ///
             (line smoking age if ses==2) (rcap losmoking hismoking age if ses==2)
          ​​​​​​

          Comment


          • #6
            Thanks a lot that was really helpful.

            Comment


            • #7
              Two different ways to do it:

              Code:
              sysuse nlsw88.dta, clear
              
              reg grade i.age if race == 1
              margins age
              marginsplot
              
              statsby mean = r(mean) ub = r(ub) lb = r(lb), by(age race): ci grade
              
              twoway rcap ub lb age if race == 1, lcolor(blue) lwidth(medium) ///
                  || line mean age if race == 1, lcolor(blue) lwidth(medium)

              CI for age 46 is so different in statsby:ci compared to margins though. I guess it is about having only 2 observations for race = 1 and age = 46.

              Comment

              Working...
              X