Announcement

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

  • Adding confidence intervals to -esttab- table

    Dear Stata listers

    I have a descriptive table made with -esttab- and -estpost- (both from SSC) like this:

    Code:
    // Open data
    sysuse auto, clear
    
    // Create a set of dummy variables called repx*
    qui tabulate rep78, gen(repx)
    
    // Make table
    eststo clear
    eststo g1: estpost summarize mpg repx* if foreign == 0  // Sample
    eststo g2: estpost summarize mpg repx*                         // Population
    
    esttab g1 g2, cells(mean(fmt(2)) sd(fmt(2) keep(mpg) par)) mtitles("Sample" "Population") nonumber
    
    --------------------------------------
                       Sample   Population
                      mean/sd      mean/sd
    --------------------------------------
    mpg                 19.83        21.30
                       (4.74)       (5.79)
    repx1                0.04         0.03
    repx2                0.17         0.12
    repx3                0.56         0.43
    repx4                0.19         0.26
    repx5                0.04         0.16
    --------------------------------------
    N                      52           74
    --------------------------------------

    I would now like to add 95% confidence intervals for the Sample in the table (i.e. the left-hand column). What I have now come up with is:

    Code:
    eststo g1ci: estpost ci mpg repx* if foreign == 0
    esttab g1 g1ci g2, cells(mean(fmt(2)) sd(fmt(2) keep(mpg) par) "lb(fmt(2)) ub(fmt(2))") mtitles("Sample" "Also sample" "Population") nonumber
    
    ------------------------------------------------------------------------------------------
                       Sample               Also sample                Population             
                   mean/sd/lb           ub   mean/sd/lb           ub   mean/sd/lb           ub
    ------------------------------------------------------------------------------------------
    mpg                 19.83                                               21.30             
                       (4.74)                                              (5.79)             
                                                  18.51        21.15                          
    repx1                0.04                                                0.03             
                                                  -0.02         0.10                          
    repx2                0.17                                                0.12             
                                                   0.06         0.28                          
    repx3                0.56                                                0.43             
                                                   0.42         0.71                          
    repx4                0.19                                                0.26             
                                                   0.07         0.30                          
    repx5                0.04                                                0.16             
                                                  -0.02         0.10                          
    ------------------------------------------------------------------------------------------
    N                      52                        52                        74             
    ------------------------------------------------------------------------------------------
    I'm obviously very unhappy with this solution (misalignment in all directions, two sample columns, empty columns for ub). Is there a more elegant solution?

    Bonus question:

    Code:
    ci mpg repx* if foreign == 0
    leads to

    Code:
    you must specify one of means, proportions, or variances following ci
    but

    Code:
    estpost ci mpg repx* if foreign == 0
    does not? Can you explain that?


    Thanks so much
    Joaquin

  • #2
    You want to report means, so just use the mean command.

    Code:
    sysuse auto, clear
    eststo m1: mean  mpg i.rep78 if !foreign
    eststo m2: mean  mpg i.rep78
    esttab m1 m2, cells(b(fmt(2)) se(fmt(2) par) ci(fmt(2) par) wide) ///
    mtitles("Sample" "Population") nonumber collab(none) ///
    addnote("Standard errors in parenthesis." "Confidence intervals in brackets.")
    Res.:

    Code:
    . esttab m1 m2, cells(b(fmt(2)) se(fmt(2) par) ci(fmt(2) par) wide) ///
    > mtitles("Sample" "Population") nonumber collab(none) ///
    > addnote("Standard errors in parenthesis." "Confidence intervals in brackets.")
    
    --------------------------------------
                       Sample   Population
    --------------------------------------
    mpg                 19.54        21.29
                       (0.69)       (0.71)
                 [18.16,20.92] [19.88,22.70]
                                          
    1.rep78              0.04         0.03
                       (0.03)       (0.02)
                 [-0.02,0.10] [-0.01,0.07]
                                          
    2.rep78              0.17         0.12
                       (0.05)       (0.04)
                  [0.06,0.28]  [0.04,0.19]
                                          
    3.rep78              0.56         0.43
                       (0.07)       (0.06)
                  [0.42,0.71]  [0.31,0.55]
                                          
    4.rep78              0.19         0.26
                       (0.06)       (0.05)
                  [0.07,0.30]  [0.15,0.37]
                                          
    5.rep78              0.04         0.16
                       (0.03)       (0.04)
                 [-0.02,0.10]  [0.07,0.25]
                                          
    --------------------------------------
    N                      48           69
    --------------------------------------
    Standard errors in parenthesis.
    Confidence intervals in brackets.

    Comment


    • #3
      Thanks, this is great!

      With tiny modifications I was able to get the standard deviation instead of the standard error and was able to remove the SD for the binary variables:

      Code:
      sysuse auto, clear
      eststo m1: mean  mpg i.rep78 if !foreign
      eststo m2: mean  mpg i.rep78
      esttab m1 m2, cells(b(fmt(2)) sd(fmt(2) par keep(mpg)) ci(fmt(2) par) wide) ///
      mtitles("Sample" "Population") nonumber collab(none) ///
      addnote("Standard deviation in parenthesis." "Confidence intervals in brackets.")
      but is there also a way to suppress reporting the CI's for the Population column?

      Thanks again
      Joaquin

      Comment


      • #4
        These canned programs are designed with uniformity in mind. If you want something custom, you need to program it yourself. Without getting into any complicated coding, you can manually remove them using the -substitute()- option. "Parenthesis" in #2 should be plural "parentheses".

        Code:
        sysuse auto, clear
        eststo m1: mean  mpg i.rep78 if !foreign
        eststo m2: mean  mpg i.rep78
        esttab m1 m2, cells(b(fmt(2)) sd(fmt(2) par keep(mpg)) ci(fmt(2) par) wide) ///
        mtitles("Sample" "Population") nonumber collab(none) ///
        addnote("Standard deviations in parentheses." "Confidence intervals in brackets.") ///
        substitute("[19.88,22.70]" "" "[-0.01,0.07]" "" "[0.04,0.19]" ""  "[0.31,0.55]" "" ///
        "[0.15,0.37]" "" "[0.07,0.25]" "")
        Res.:

        Code:
        . esttab m1 m2, cells(b(fmt(2)) sd(fmt(2) par keep(mpg)) ci(fmt(2) par) wide) ///
        > mtitles("Sample" "Population") nonumber collab(none) ///
        > addnote("Standard deviations in parentheses." "Confidence intervals in brackets.") ///
        > substitute("[19.88,22.70]" "" "[-0.01,0.07]" "" "[0.04,0.19]" ""  "[0.31,0.55]" "" ///
        > "[0.15,0.37]" "" "[0.07,0.25]" "")
        
        --------------------------------------
                           Sample   Population
        --------------------------------------
        mpg                 19.54        21.29
                           (4.75)       (5.87)
                     [18.16,20.92] 
                                              
        1.rep78              0.04         0.03
                     [-0.02,0.10] 
                                              
        2.rep78              0.17         0.12
                      [0.06,0.28]  
                                              
        3.rep78              0.56         0.43
                      [0.42,0.71]  
                                              
        4.rep78              0.19         0.26
                      [0.07,0.30]  
                                              
        5.rep78              0.04         0.16
                     [-0.02,0.10]  
                                              
        --------------------------------------
        N                      48           69
        --------------------------------------
        Standard deviations in parentheses.
        Confidence intervals in brackets.

        Comment


        • #5
          Thank you very much, I did not even know about the substitute option in -esttab-! Seems like I will need to go for a more complicated solution, because my data set has missing data, and -mean- seems to enforce listwise deletion for the vector of variables, while -summarize- doesn't. But thanks nonetheless, I learned quite a bit!

          Comment


          • #6
            Not very elegant own solution but no less elegant than using -substitute-, I guess:

            Code:
            // Open data
            sysuse auto, clear
            
            // Create a set of dummy variables called repx*
            qui tabulate rep78, gen(repx)
            
            
            eststo m1:              mean  mpg repx* if !foreign
            estadd matrix mean = e(b)
            eststo m2: estpost summarize  mpg repx*
            esttab m1 m2, cells(mean(fmt(2)) sd(fmt(2) par keep(mpg)) ci(fmt(2) par) wide) ///
            mtitles("Sample" "Population") nonumber collab(none) ///
            addnote("Standard deviation in parentheses." "Confidence intervals in brackets.")
            Thanks again

            Comment


            • #7
              I have followed the discussions and it appears the esttab code I use is different. So I would want to find out how to report confidence intervals when using for instance the code below: esttab X1 X2 X3 using XM, rtf b(a3) se(a3) star(+ 0.10 * 0.1 ** 0.05 *** 0.01) scalars(N_g j ar2 ar2p hansen hansenp F p) label nogaps Thank you.

              Comment


              • #8
                You can choose to display confidence intervals in place of standard errors with your code, but not both. For both, use the -cells()- option as above.

                Code:
                esttab X1 X2 X3 using XM, rtf b(a3) ci(a3) star(+ 0.10 * 0.1 ** 0.05 *** 0.01) scalars(N_g j ar2 ar2p hansen hansenp F p) label nogaps

                Comment

                Working...
                X