Announcement

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

  • Adding error bars to a two-way line graph

    Hello all,

    I am currently trying to find a way to add error bars to a two-way line graph but it has me completely stumped. I would like to produce something like the graph below, where the lines represent gender, the y-axis mean BMI, and the x-axis Age. Any ideas?? I have been trying different combinations for 2 days with no luck.

    Thanks,

    Sarah


  • #2
    Welcome to the Stata Forum/Statalist.

    The best way to entice a helpful reply is sharing data (full, abridged or mock) and for this you may use CODE delimiters or installl the SSC dataex.

    That said, you may get the graph you shared by using - margins- and - marginsplot - commands.
    Best regards,

    Marcos

    Comment


    • #3
      Hello Marcos,

      Thank you for the reply

      I just started using Stata a couple of weeks ago so much of what you have mentioned is unfamiliar to me. Are you saying I should refer to a specific dataset for my question?

      With the margins and marginsplot commands -can they be used in conjunction with line graphs?

      Thank you!!

      Comment


      • #4
        -marginsplot- defaults to a line graph with error bars, so yes. This is the easiest way to get a graph like the one you have above.

        For example,

        Code:
        webuse margex //you won't need this line. it just brings in artificial data to demonstrate the marginsplot command
        regress y i.age i.sex //you will want to replace y with your variable name for BMI, and of course change i.age to i.Age if it's called as such in your data (same again for i.sex and i.Sex / i.Gender...)
        margins age, over(sex)
        marginsplot

        Last edited by Chris Larkin; 13 Jun 2017, 21:41.

        Comment


        • #5

          Amazing! That works beautifully, thank you Chris

          Comment


          • #6
            Hi,
            I would like to implement a similar code, howbeit on a non-regression based output i.e. I used interactive fixed effects method.
            The following is the data from the matrix of the output.
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(time ATT ATT_sd ATT_p_value CI_Lower_Bound CI_Upper_Bound)
            -10  -9.349247e-17 4.0805066e-16   .8187758 -1.2415994e-15  9.827184e-16
             -9  -3.272236e-16  3.973218e-16   .4101821 -1.0066022e-15  1.060263e-15
             -8  -9.349247e-17 3.6642955e-16   .7986115 -1.1826797e-15  8.908218e-16
             -7 -1.8698493e-16   3.75479e-16   .6184908 -1.0932243e-15  9.029814e-16
             -6  1.8698493e-16  3.732421e-16   .6163888  -9.769963e-16  1.110223e-15
             -5  -4.674623e-17  3.504322e-16   .8938802 -1.0737884e-15  9.992007e-16
             -4   2.804774e-16  3.565715e-16    .431519  -9.349247e-16  9.473903e-16
             -3   3.272236e-16  4.131172e-16   .4283116 -1.0978873e-15 1.0713652e-15
             -2   4.674623e-17  3.817579e-16   .9025427 -1.0317833e-15  9.177843e-16
             -1  4.6746234e-16  4.045667e-16   .2479004 -1.0384342e-15  1.203028e-15
              0 -1.8698493e-16  3.511056e-16    .594338  -9.516197e-16  9.180441e-16
              1     -.08088255     .04013488  .04387657     -.19066775     .02875391
              2     -.09232894      .0458617  .04409322      -.2083101    .034001507
              3      -.1278605     .06158197  .03786943     -.28330165     .03234445
              4      -.1589148     .06748752 .018536225      -.3302763    .020880336
              5       -.193379     .08493743 .022802785      -.4423933    .004278724
              6     -.22596964     .09871003  .02206618      -.5092498    .013310323
              7     -.27231818     .12303404  .02687308      -.6170274     .02363522
              8      -.3135594     .13810422 .023179974      -.7076861    .036383346
              9      -.3171377     .14846183 .032666657      -.7330076     .04865605
             10      -.3370073     .15564077  .03036554       -.781558     .06119034
            end
            Here, I would like to plot "ATT" over time using "CI_Lower_Bound" and "CI_Upper_Bound" as confidence sets.
            PS: I am able to plot the confidence intervals as lines (line graphs) but I would like to present them using the type described here.

            Comment


            • #7
              Code:
              twoway scatter ATT time, con(line) || rcap CI_Upper_Bound CI_Lower_Bound time

              Comment


              • #8
                Thank you very much Øyvind Snilsberg. This worked perfectly.

                Comment

                Working...
                X