Announcement

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

  • How can I add confidence intervals to a two-way line graph?

    Greetings,

    I'm running Stata 15.1 and using longitudinal data from the General Social Survey. I'd like to graph the average number of male sex partners by political ideology and overtime. I created a weighted/by-year variables that store the mean # of sex partners for liberal, conservative, and all women, respectively. I then generated a line chart with the following syntax:

    Code:
    twoway (line mensex_all mensex_lib mensex_con nummen_year, sort(nummen_year))
    .

    However, I'd now like to add confidence intervals for each of these y variables but am unsure how to do so. Any help would be much appreciated. Thanks in advance!

    Here's my data:

    Code:
    * Example generated by -dataex-. To install:    ssc install dataex
    clear
    input float(mensex_all mensex_lib mensex_con    nummen_year)
    .         .         . 1989
    .         .         . 1989
    .         .         . 1989
    .         .         . 1989
    .         .         . 1989
    3.253476 4.3462496         . 1989
    .         .         . 1989
    3.253476         .         . 1989
    .         .         . 1989
    .         .         . 1989
    3.253476 4.3462496         . 1989
    3.253476         .         . 1989
    .         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         . 2.9069505 1989
    .         .         . 1989
    3.253476         .         . 1989
    .         .         . 1989
    .         .         . 1989
    3.253476         . 2.9069505 1989
    3.253476 4.3462496         . 1989
    .         .         . 1989
    3.253476         . 2.9069505 1989
    .         .         . 1989
    3.253476         . 2.9069505 1989
    3.253476         .         . 1989
    3.253476 4.3462496         . 1989
    3.253476         . 2.9069505 1989
    3.253476 4.3462496         . 1989
    .         .         . 1989
    3.253476         .         . 1989
    .         .         . 1989
    .         .         . 1989
    3.253476 4.3462496         . 1989
    .         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476         .         . 1989
    3.253476 4.3462496         . 1989
    .         .         . 1989
    3.253476         .         . 1989
    3.253476         . 2.9069505 1989
    .         .         . 1989
    3.253476         .         . 1989
    end



  • #2
    -help twoway rcap-

    Comment


    • #3
      This seems much easier in a regression setting with margins and marginsplot.

      Comment


      • #4
        Hey Clyde,

        Attempted what you suggested and entered the following

        Code:
         twoway rcap mensex_all mensex_lib mensex_con nummen_year || line mensex_all mensex_lib mensex_con nummen_year
        Stata returns the following error:
        rcap requires 3 variables: mensex_all mensex_lib mensex_con nummen_year
        Where am I doing wrong?

        Thanks!

        Comment


        • #5
          All that -rcap- does is plot the confidence limits contained in variables. It does not create variables containing the confidence limits. Your example data does not show the upper and lower confidence limits corresponding to the mensex_* variables: you need two variables, one upper limit and one lower limit, for each of the mensex_* variables. I don't know how you will calculate those variables as it is not clear from the data where the mensex_* variables themselves come from, and there is nothing in your example data that would make it possible to calculate them. Once you have those, you will need a separate -rcap- for each of the mensex_* variables. The three variable in each -rcap- will be the upper bound for that particular mensex_ variable, its lower bound, and nummen_year. So your full graph command will have the -line- component and three -rcap- components.

          Comment


          • #6
            What command do I use to create the hi and lo variables?

            Comment


            • #7
              That depends on how you got the mean variables in the first place. You need to show the code that created them.

              Comment


              • #8
                Okay, here's what I did:
                Code:
                egen mensex_lib=wtmean(nummen) if ideo3==1 & male==0, by(year) weight(weight)
                Code:
                egen mensex_mod=wtmean(nummen) if ideo3==2 & male==0, by(year) weight(weight)
                Code:
                egen mensex_con=wtmean(nummen) if ideo3==3 & male==0, by(year) weight(weight)
                Code:
                egen mensex_all=wtmean(nummen) if  male==0, by(year) weight(weight)
                ​​​​​​​Thanks for taking the time to help me!


                Comment


                • #9
                  Your example is difficult to follow since there are so many missing values.

                  I agree with Dimitriy V. Masterov in #3. The following is much easier. Note that you will need to specify the type of weight you are using where you see ??.


                  Code:
                  reg nummen i.year##i.ideo3 if male==0 [??=weight]
                  margins i.year#i.ideo3 i.year if male==0
                  marginsplot
                  Stata/MP 14.1 (64-bit x86-64)
                  Revision 19 May 2016
                  Win 8.1

                  Comment


                  • #10
                    You can also use statsby:ci but when I was trying different options with an example sample, I run in to a weird result.
                    In the example below, CI for age 46 is so different in statsby:ci compared to margins. There is only 2 observations for race = 1 and age = 46.

                    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)

                    Comment

                    Working...
                    X