Announcement

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

  • Formatting table using estout

    I am using the estout command to prepare my tables in Latex. Since I have lot of tables and I change them ofthen, I would like to automate everything so I minimize manual edit. Here is an example of the command I am using:

    estout model* using "C:\Users\asmobari\Box Sync\Projects\MP_Project\WriteUp\tabfig_MP\model1. tex", ///
    keep(t post post_T _cons) varlabels(post Post t Treatment post_T DiD _cons Constant) ///
    cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, labels("R-squared""Obs")) ///
    numbers collabels(none) mlabels(,titles) ///
    style(tex) replace

    This produces me the following Latex file:

    & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\
    & Male & Female &All Students & Male & Female &All Students & Male & Female &All Students \\
    Treatment & 0.35 & -5.11 & 0.05 & 21.10* & 19.09 & 26.07* & -3.20 & 1.64 & -1.75 \\
    & (8.16) & (14.72) & (7.33) & (9.87) & (16.02) & (9.62) & (8.37) & (4.46) & (6.68) \\
    Post & -8.29 & -10.94 & -7.35 & 19.15 & 21.87 & 22.84 & -10.82 & 4.94 & -5.94 \\
    & (7.66) & (14.41) & (6.51) & (12.62) & (16.14) & (11.98) & (5.83) & (4.27) & (5.49) \\
    DiD & 7.81 & 0.95 & 3.68 & -14.75 & -29.20 & -27.56 & 6.88 & 5.31 & 0.20 \\
    & (11.41) & (18.35) & (10.45) & (13.89) & (22.02) & (14.02) & (9.41) & (8.85) & (10.01) \\
    Constant & 195.25***& 187.05***& 190.88***& 67.82***& 60.15***& 62.49***& 40.92***& 22.63** & 52.96***\\
    & (5.92) & (12.15) & (5.48) & (8.98) & (10.84) & (8.55) & (6.51) & (6.21) & (5.98) \\
    R-squared & 0.02 & 0.04 & 0.02 & 0.16 & 0.06 & 0.14 & 0.03 & 0.04 & 0.02 \\
    Obs & 59.00 & 51.00 & 71.00 & 59.00 & 51.00 & 71.00 & 59.00 & 51.00 & 71.00 \\ However, I want to have horizontal line between the Constant and R-squared and a line space between R-squared and Obs. Manually I can do it as bellow:
    & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\
    & Male & Female &All Students & Male & Female &All Students & Male & Female &All Students \\
    Treatment & 0.35 & -5.11 & 0.05 & 21.10* & 19.09 & 26.07* & -3.20 & 1.64 & -1.75 \\
    & (8.16) & (14.72) & (7.33) & (9.87) & (16.02) & (9.62) & (8.37) & (4.46) & (6.68) \\
    Post & -8.29 & -10.94 & -7.35 & 19.15 & 21.87 & 22.84 & -10.82 & 4.94 & -5.94 \\
    & (7.66) & (14.41) & (6.51) & (12.62) & (16.14) & (11.98) & (5.83) & (4.27) & (5.49) \\
    DiD & 7.81 & 0.95 & 3.68 & -14.75 & -29.20 & -27.56 & 6.88 & 5.31 & 0.20 \\
    & (11.41) & (18.35) & (10.45) & (13.89) & (22.02) & (14.02) & (9.41) & (8.85) & (10.01) \\
    Constant & 195.25***& 187.05***& 190.88***& 67.82***& 60.15***& 62.49***& 40.92***& 22.63** & 52.96***\\
    & (5.92) & (12.15) & (5.48) & (8.98) & (10.84) & (8.55) & (6.51) & (6.21) & (5.98) \\
    \midrule
    R-squared & 0.02 & 0.04 & 0.02 & 0.16 & 0.06 & 0.14 & 0.03 & 0.04 & 0.02 \\
    \addlinespace
    Obs & 59.00 & 51.00 & 71.00 & 59.00 & 51.00 & 71.00 & 59.00 & 51.00 & 71.00 \\
    But I wonder if I can give this instruction in my stata code so I don't have to open my latex file each time I change my regression. I would be glad if somebody could help me with it. Thanks in advance!


  • #2
    If you do not want anything displayed when you run the command, add quietly in front of the code

    Code:
    qui estout ...
    See

    Code:
    help qui

    I would like to automate everything so I minimize manual edit.
    If you mean that you do not want to edit the output manually, then it depends on what you edit. You do not provide details on this.
    Last edited by Andrew Musau; 27 Oct 2019, 12:59.

    Comment


    • #3
      Andrew Musau , thank! But I meant to ask how can I add horizontal lines in my table output in Stata. For example, I want to add a horizontal line between two variables or a horizontal line after variables end and statistics begin. I know it can be done using \hline in Latex, but I wonder if I can code it in Stata so I do not have to edit my Latex file all time.

      Comment


      • #4
        You can use esttab (SSC) which will include the lines. It may be possible with estout, but I do not know how to.

        Code:
        esttab model* using "C:\Users\asmobari\Box Sync\Projects\MP_Project\WriteUp\tabfig_MP\model1. tex", ///
        keep(t post post_T _cons) varlabels(post Post t Treatment post_T DiD _cons Constant) ///
        cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, labels("R-squared""Obs")) ///
        numbers collabels(none) mlabels(,titles) ///
        tex replace

        Comment


        • #5
          Andrew Musau Thanks. I know esttab does it. But esttab produces extra headers and footers in the latex file that I do not want. I need only the data part of the table, that estout does pretty well. But I want to be able to include lines wherever I want.

          I have another question: In stats(r2 N, labels("R-squared""Obs")) how can I tell stata to produce N by sub-category. For example, if I have treated and control, I want the number of of observations for treated and control separately in addition to the total observations (N)

          Comment


          • #6
            Andrew Musau Thanks. I know esttab does it. But esttab produces extra headers and footers in the latex file that I do not want. I need only the data part of the table, that estout does pretty well. But I want to be able to include lines wherever I want.
            The output produced by estout with option -style(tex)- requires you to place this within the tabular environment of LaTeX, so there will always be some manual copying and pasting needed.

            The tex style, for example, modifies the output table for use with LaTeX's tabular environment:

            . estout *, style(tex) varlabels(_cons \_cons)
            I would recommend that you stick with esttab which automates everything. What you are looking for is the -plain- option of esttab to produce a minimally formatted table. In this thread, I suggest a way to introduce horizontal lines using scalars. The idea is to define a non-existent scalar and label it "\hline". Here is what you get specifying the -plain- option and adding two horizontal lines separating the coefficients and stats and at the bottom of the table.

            Code:
            sysuse auto, clear
            qui reg mpg weight displacement foreign
            esttab,stats(j r2 N j, labels("\hline" "R-squared""Obs" "\hline")) plain not tex nomtitle

            Res.:

            Code:
            .
            . esttab,stats(j r2 N j, labels("\hline" "R-squared""Obs" "\hline")) plain not tex nomtitle
            
            \begin{tabular}{l*{1}{c}}
                        &           b\\
            weight      &   -.0067745\\
            displacement&    .0019286\\
            foreign     &   -1.600631\\
            \_cons      &    41.84795\\
            \hline      &            \\
            R-squared   &    .6628796\\
            Obs         &          74\\
            \hline      &            \\
            \end{tabular}
            I have another question: In stats(r2 N, labels("R-squared""Obs")) how can I tell stata to produce N by sub-category. For example, if I have treated and control, I want the number of of observations for treated and control separately in addition to the total observations (N)
            You want something like

            Code:
            regress ...
            count if treated & e(sample)
            estadd local N1 = r(N)
            
            count if control & e(sample)
            estadd local N2 = r(N)
            stats(r2 N N1 N2, labels("R-squared""Obs" "Obs treated" "Obs control"))
            In my example,

            Code:
            sysuse auto, clear
            qui reg mpg weight displacement foreign
            count if foreign & e(sample)
            estadd local N1= r(N)
            count if !foreign & e(sample)
            estadd local N2= r(N)
            esttab,stats(j r2 N N1 N2 j, labels("\hline" "R-squared""Obs total" "Obs foreign" "Obs domestic" "\hline")) plain not tex nomtitle
            Res.:

            Code:
            \begin{tabular}{l*{1}{c}}
                        &           b\\
            weight      &   -.0067745\\
            displacement&    .0019286\\
            foreign     &   -1.600631\\
            \_cons      &    41.84795\\
            \hline      &            \\
            R-squared   &    .6628796\\
            Obs total   &          74\\
            Obs foreign &          22\\
            Obs domestic&          52\\
            \hline      &            \\
            \end{tabular}
            Finally, for anything very specific, you may need to edit the code of estout\esttab to add an option that gives you what you need. See

            Code:
            viewsource estout.ado

            Comment


            • #7
              @Adrew, thanks for your elaborated reply. I am almost there. I have pre-formatted and included top and bottom in a separate file. The esttab as per your code gives me this:

              \begin{tabular}{l*{9}{c}}
              &\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)} &\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\mul
              > ticolumn{1}{c}{(6)} &\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)} &\multicolumn{1}{c}{(9)} \\
              & Male & Female &All Students & Male & Female &All Students & Male & Female &All Student
              > s \\
              Treatment & 8.28 & 8.78 & 5.96 & 6.63 & 0.29 & 3.15 & 7.07 & -5.04 & 3.0
              > 9 \\
              & (6.31) & (6.32) & (6.18) & (5.51) & (7.18) & (5.27) & (5.77) & (4.12) & (5.30
              > ) \\
              Post & 0.60 & -8.20 & -3.58 & 46.45***& 55.90***& 48.65***& -20.06***& -0.73 & -18.2
              > 7***\\
              & (5.06) & (11.72) & (5.65) & (5.58) & (8.85) & (5.49) & (3.83) & (2.90) & (4.12
              > ) \\
              DiD & -6.23 & -6.54 & -2.52 & -9.84 & -19.82* & -11.26* & -7.49 & 9.56 & -9.1
              > 3 \\
              & (6.23) & (7.60) & (6.28) & (5.28) & (9.22) & (5.31) & (5.77) & (5.94) & (6.18
              > ) \\
              Constant & 196.18***& 185.57***& 192.71***& 43.61***& 31.19***& 39.75***& 51.47***& 26.01***& 67.1
              > 5***\\
              & (5.50) & (10.32) & (5.57) & (6.08) & (8.30) & (5.73) & (4.38) & (4.90) & (4.15
              > ) \\
              \hline & & & & & & & & &
              > \\
              R-squared & 0.02 & 0.04 & 0.02 & 0.46 & 0.34 & 0.44 & 0.28 & 0.02 & 0.2
              > 3 \\
              Obs & 113.00 & 99.00 & 131.00 & 113.00 & 99.00 & 131.00 & 111.00 & 98.00 & 129.0
              > 0 \\
              \hline & & & & & & & & &
              > \\
              \end{tabular}


              But I want something like this:

              & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\
              & Male & Female &All Students & Male & Female &All Students & Male & Female &All Students \\
              Treatment & 0.63 & -5.06 & -0.56 & 26.16** & 25.07 & 30.41** & -3.80 & -2.03 & -4.82 \\
              & (8.13) & (12.89) & (7.32) & (8.85) & (14.85) & (8.50) & (7.68) & (5.19) & (6.34) \\
              Post & -4.96 & -12.12 & -8.05 & 25.77* & 30.17* & 28.48** & -10.52* & 4.81 & -5.36 \\
              & (6.65) & (11.97) & (6.47) & (10.82) & (13.53) & (10.14) & (5.04) & (3.89) & (4.73) \\
              DiD & 4.47 & 2.13 & 4.37 & -21.36 & -37.50 & -33.21* & 6.58 & 5.45 & -0.38 \\
              & (10.42) & (15.77) & (10.01) & (12.13) & (19.54) & (12.36) & (8.92) & (8.60) & (9.52) \\
              Constant & 194.97***& 187.00***& 191.49***& 62.76***& 54.17***& 58.15***& 41.53***& 26.30***& 56.03***\\
              & (5.60) & (10.41) & (5.35) & (7.97) & (9.77) & (7.46) & (5.35) & (5.36) & (5.10) \\
              \midrule
              R-squared & 0.01 & 0.05 & 0.02 & 0.23 & 0.12 & 0.20 & 0.03 & 0.03 & 0.03 \\
              \addlinespace
              Obs & 66.00 & 59.00 & 80.00 & 66.00 & 59.00 & 80.00 & 65.00 & 58.00 & 79.00 \\
              Can I do that within Stata without a manual edit on Latex?

              Comment


              • #8
                Unless I am missing something, you simply want:

                Code:
                sysuse auto, clear
                qui reg mpg weight displacement foreign
                esttab,stats(j r2 j N, labels("\midrule" "R-squared""\addlinespace""Obs")) plain not tex nomtitle collabels(none)

                Res.:

                Code:
                 esttab,stats(j r2 j N, labels("\midrule" "R-squared""\addlinespace""Obs")) plain not tex nomtitle collabels(none)
                
                \begin{tabular}{l*{1}{c}}
                weight      &   -.0067745\\
                displacement&    .0019286\\
                foreign     &   -1.600631\\
                \_cons      &    41.84795\\
                \midrule    &            \\
                R-squared   &    .6628796\\
                \addlinespace&            \\
                Obs         &          74\\
                \end{tabular}

                Comment


                • #9
                  Unless I am missing something, you simply want:
                  Andrew Musau , thanks for being so responsive. I want esttab to produce the code as below, but with column number and model name.

                  Code:
                   
                   weight      &   -.0067745\\ displacement&    .0019286\\ foreign     &   -1.600631\\ \_cons      &    41.84795\\ \midrule    &            \\ R-squared   &    .6628796\\ \addlinespace&            \\ Obs         &          74\\

                  Comment


                  • #10
                    Code:
                     
                     weight & -.0067745\\  displacement& .0019286\\  foreign & -1.600631\\  \_cons & 41.84795\\  \midrule & \\  R-squared & .6628796\\  \addlinespace& \\  Obs & 74\\

                    Comment


                    • #11
                      I want esttab to produce the code as below, but with column number and model name.
                      These are just standard options which you can get by reading

                      Code:
                      help esttab
                      My editor is WinEdt and the following codes give me the tables below:

                      Code:
                      sysuse auto
                      eststo: qui reg mpg price weight
                      eststo: qui reg mpg price weight length
                      eststo: qui reg mpg price weight length displacement
                      *TITLE AND COLUMN NUMBERS
                      esttab est*,stats(j r2 j N, labels("\midrule" "R-squared""\addlinespace""Obs")) plain not tex nomtitle collabels(none) numbers title("Auto Regressions")
                      *COLUMN NUMBERS AND MODEL TITLES
                      esttab est*,stats(j r2 j N, labels("\midrule" "R-squared""\addlinespace""Obs")) plain not tex collabels(none) numbers mtitle("Good" "Better" "Best")
                      Res,:

                      Code:
                      \begin{table}[htbp]\centering
                      \caption{Auto Regressions}
                      \begin{tabular}{l*{3}{c}}
                                  &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}\\
                      price       &   -.0000935&   -.0001733&   -.0001742\\
                      weight      &   -.0058175&   -.0030371&   -.0035428\\
                      length      &            &   -.0965706&   -.0947189\\
                      displacement&            &            &    .0043254\\
                      \_cons      &    39.43966&    49.68492&    50.01615\\
                      \midrule    &            &            &            \\
                      R-squared   &    .6531447&    .6664835&    .6674163\\
                      \addlinespace&            &            &            \\
                      Obs         &          74&          74&          74\\
                      \end{tabular}
                      \end{table}
                      
                      
                      \begin{tabular}{l*{3}{c}}
                                  &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}\\
                                  &\multicolumn{1}{c}{Good}&\multicolumn{1}{c}{Better}&\multicolumn{1}{c}{Best}\\
                      price       &   -.0000935&   -.0001733&   -.0001742\\
                      weight      &   -.0058175&   -.0030371&   -.0035428\\
                      length      &            &   -.0965706&   -.0947189\\
                      displacement&            &            &    .0043254\\
                      \_cons      &    39.43966&    49.68492&    50.01615\\
                      \midrule    &            &            &            \\
                      R-squared   &    .6531447&    .6664835&    .6674163\\
                      \addlinespace&            &            &            \\
                      Obs         &          74&          74&          74\\
                      \end{tabular}
                      Click image for larger version

Name:	LaTeX.png
Views:	1
Size:	35.8 KB
ID:	1522766

                      Comment

                      Working...
                      X