Announcement

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

  • Esttab: Stata & Latex

    Hi Everybody!

    Can someone help me with a formatting issue I have with my Stata code? I'm using esttab to create tables (see code below). Now this is not very pretty, since it puts the significance-stars behind my coefficients, which then does not align well with the SE and everything else. I have seen people use the "stardetach" option to solve this problem. However, I had problems finding a way on how to implement this into my code, without it looking very differently. Can someone help me?

    This is my current code creating my tables, which gives me the table below:

    Code:
    esttab col1 col2 col3 using "$tables/TABLE1.tex", replace ///
            noobs nomti nonumb  ///
            prehead("\begin{tabular}{lccc}" ///
            "%" "\addlinespace" "%" ///
            "%" "\toprule" "%" ///
            "      &          \multicolumn{1}{c}{Attended}  &         \multicolumn{1}{c}{Hours} &         \multicolumn{1}{c}{Overnight}\\" ///
            "      &          \multicolumn{1}{c}{Test Run}  &         \multicolumn{1}{c}{in Prison} &         \multicolumn{1}{c}{in Prison}\\" ///
            " & (1) & (2) & (3) \\" /// 
                    "%" "\midrule " "%") ///
            b(3) se star(* .1 ** .05 *** .01)  ///
            keep($coeff  ) ///
            order($coeff  )  ///
            varlabels($coeff_label1) ///
            substitute(\_ _ [1em] ) ///
            gaps nolines noeqlines ///
            prefoot("\midrule Strata FE        & \checkmark & \checkmark & \checkmark\\") ///
            stats(ymean N, labels("\midrule Control group mean" "Observations") fmt(3 a0)) ///
            postfoot("\bottomrule" ///          
            "\end{tabular}")
    Click image for larger version

Name:	Bildschirmfoto 2025-03-28 um 11.28.01.png
Views:	1
Size:	82.9 KB
ID:	1774977

  • #2
    Can you create a reproducible example? Note that estout is from SSC, as you are asked to explain (FAQ Advice #12).

    Comment


    • #3
      Sorry, let me explain the data. The code below is being run before the code that I have put into my original question. I'm using esttab from SSC in Stata 16.1.
      • a is my binary dependent variable in the first regression
      • b is a continuous dependent variable in the second regression
      • c is my binary dependent variable in the third regression
      • x is my binary treatment assignment
      • s is my binary stratum

      Code:
      reg a x s, vce(hc3)
      eststo col1
      sum a if x==0
      estadd scalar ymean= r(mean)
      
      reg b x s, vce(hc3)
      eststo col2
      sum b if x==0
      estadd scalar ymean= r(mean)
      
      reg c x s, vce(hc3)
      eststo col3
      sum c if x==0
      estadd scalar ymean= r(mean) 
       esttab col1 col2 col3 using "$tables/TABLE1.tex", replace ///        noobs nomti nonumb  ///        prehead("\begin{tabular}{lccc}" ///        "%" "\addlinespace" "%" ///        "%" "\toprule" "%" ///        "      &          \multicolumn{1}{c}{Attended}  &         \multicolumn{1}{c}{Hours} &         \multicolumn{1}{c}{Overnight}\\" ///        "      &          \multicolumn{1}{c}{Test Run}  &         \multicolumn{1}{c}{in Prison} &         \multicolumn{1}{c}{in Prison}\\" ///        " & (1) & (2) & (3) \\" ///                  "%" "\midrule " "%") ///        b(3) se star(* .1 ** .05 *** .01)  ///        keep($coeff  ) ///        order($coeff  )  ///        varlabels($coeff_label1) ///        substitute(\_ _ [1em] ) ///        gaps nolines noeqlines ///        prefoot("\midrule Strata FE        & \checkmark & \checkmark & \checkmark\\") ///        stats(ymean N, labels("\midrule Control group mean" "Observations") fmt(3 a0)) ///        postfoot("\bottomrule" ///                  "\end{tabular}")
      Here is an example from my data:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(a b c x s)
      1 4.3430557 0 1 0
      1     14.77 0 1 1
      1   23.4425 1 1 1
      1 28.099167 1 1 0
      1 11.442778 0 1 0
      1 13.031667 1 1 0
      1 13.354167 1 1 0
      1 23.625555 1 1 1
      1  21.98611 1 1 0
      1 22.145834 1 0 0
      1  43.40083 1 1 0
      1  20.87389 1 1 0
      1 18.520277 1 1 0
      1  4.944722 0 1 0
      1  18.21611 1 1 1
      1 18.887222 1 1 0
      1  23.47111 1 0 1
      1      6.49 0 0 0
      1 18.405834 1 1 0
      1 18.079445 0 1 1
      1        25 1 1 1
      1  6.601944 0 1 0
      1 13.928333 1 1 0
      1  23.11361 1 0 0
      1  22.98611 1 1 0
      1  9.356112 0 1 0
      1 25.130556 1 1 1
      1 14.426666 1 0 1
      1 22.433056 1 1 0
      1    15.915 1 1 0
      1   19.6525 1 1 0
      1 23.795834 1 1 0
      1 16.606667 1 1 0
      1  15.02639 1 1 0
      1 14.207778 1 1 0
      1 18.195555 1 1 0
      1  23.98361 1 1 0
      1  23.11278 1 1 0
      1 10.685833 0 1 0
      1 30.309444 1 0 0
      1 13.466666 1 1 0
      1    20.155 1 0 0
      1  9.575556 1 1 1
      1 15.940278 1 1 1
      1 16.615278 1 0 0
      1  5.771111 0 1 1
      1 12.374166 1 1 0
      1 14.748055 1 1 0
      1  21.13667 1 1 0
      1 23.321945 1 1 0
      1 28.753334 1 1 0
      1    14.435 1 1 0
      1 17.554445 0 1 1
      1 15.053056 1 1 1
      1  46.38667 1 0 0
      1 18.603056 1 0 0
      1 16.770277 1 0 0
      1 17.812778 1 1 0
      1 21.746944 1 1 0
      1 28.918333 1 1 1
      1 23.528334 1 1 0
      1 29.935556 1 1 1
      1  23.80389 1 1 1
      1     48.02 1 0 0
      1  3.137778 0 1 0
      1  25.06861 1 1 0
      1 16.116667 1 1 0
      1 23.139444 1 1 0
      1  22.66889 1 1 0
      1 18.008612 1 1 0
      1  22.24861 1 1 0
      1  31.09611 1 1 0
      1 19.986944 1 1 0
      1  42.04306 1 1 0
      1  10.95111 0 1 0
      1  23.36389 1 1 0
      1  8.309167 0 0 0
      1  40.84889 1 1 0
      1 18.378334 1 1 0
      1  22.83472 1 1 0
      1  21.61611 1 1 1
      1  28.78639 1 1 1
      1  26.87389 1 1 1
      1 20.565556 1 1 0
      1  25.65389 1 1 0
      1 25.439444 1 1 0
      1 36.094444 1 1 0
      1  4.561111 0 1 1
      1  20.40389 1 1 1
      1  40.30194 1 1 0
      1   31.6125 1 1 0
      1  23.09611 1 1 0
      1  39.83055 1 1 0
      1  19.55778 1 1 0
      1 16.937222 1 1 0
      1 19.608334 1 1 0
      1 20.449444 1 1 0
      1 33.217777 1 1 0
      0         0 0 0 0
      0         0 0 0 1
      end

      Comment


      • #4
        Thanks for the reproducible example. I needed to adjust the significance thresholds so that the stars would display with your data example. You can define the significance stars separately as a command in LaTeX, which will ensure proper spacing (see highlighted).

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(a b c x s)
        1 4.3430557 0 1 0
        1     14.77 0 1 1
        1   23.4425 1 1 1
        1 28.099167 1 1 0
        1 11.442778 0 1 0
        1 13.031667 1 1 0
        1 13.354167 1 1 0
        1 23.625555 1 1 1
        1  21.98611 1 1 0
        1 22.145834 1 0 0
        1  43.40083 1 1 0
        1  20.87389 1 1 0
        1 18.520277 1 1 0
        1  4.944722 0 1 0
        1  18.21611 1 1 1
        1 18.887222 1 1 0
        1  23.47111 1 0 1
        1      6.49 0 0 0
        1 18.405834 1 1 0
        1 18.079445 0 1 1
        1        25 1 1 1
        1  6.601944 0 1 0
        1 13.928333 1 1 0
        1  23.11361 1 0 0
        1  22.98611 1 1 0
        1  9.356112 0 1 0
        1 25.130556 1 1 1
        1 14.426666 1 0 1
        1 22.433056 1 1 0
        1    15.915 1 1 0
        1   19.6525 1 1 0
        1 23.795834 1 1 0
        1 16.606667 1 1 0
        1  15.02639 1 1 0
        1 14.207778 1 1 0
        1 18.195555 1 1 0
        1  23.98361 1 1 0
        1  23.11278 1 1 0
        1 10.685833 0 1 0
        1 30.309444 1 0 0
        1 13.466666 1 1 0
        1    20.155 1 0 0
        1  9.575556 1 1 1
        1 15.940278 1 1 1
        1 16.615278 1 0 0
        1  5.771111 0 1 1
        1 12.374166 1 1 0
        1 14.748055 1 1 0
        1  21.13667 1 1 0
        1 23.321945 1 1 0
        1 28.753334 1 1 0
        1    14.435 1 1 0
        1 17.554445 0 1 1
        1 15.053056 1 1 1
        1  46.38667 1 0 0
        1 18.603056 1 0 0
        1 16.770277 1 0 0
        1 17.812778 1 1 0
        1 21.746944 1 1 0
        1 28.918333 1 1 1
        1 23.528334 1 1 0
        1 29.935556 1 1 1
        1  23.80389 1 1 1
        1     48.02 1 0 0
        1  3.137778 0 1 0
        1  25.06861 1 1 0
        1 16.116667 1 1 0
        1 23.139444 1 1 0
        1  22.66889 1 1 0
        1 18.008612 1 1 0
        1  22.24861 1 1 0
        1  31.09611 1 1 0
        1 19.986944 1 1 0
        1  42.04306 1 1 0
        1  10.95111 0 1 0
        1  23.36389 1 1 0
        1  8.309167 0 0 0
        1  40.84889 1 1 0
        1 18.378334 1 1 0
        1  22.83472 1 1 0
        1  21.61611 1 1 1
        1  28.78639 1 1 1
        1  26.87389 1 1 1
        1 20.565556 1 1 0
        1  25.65389 1 1 0
        1 25.439444 1 1 0
        1 36.094444 1 1 0
        1  4.561111 0 1 1
        1  20.40389 1 1 1
        1  40.30194 1 1 0
        1   31.6125 1 1 0
        1  23.09611 1 1 0
        1  39.83055 1 1 0
        1  19.55778 1 1 0
        1 16.937222 1 1 0
        1 19.608334 1 1 0
        1 20.449444 1 1 0
        1 33.217777 1 1 0
        0         0 0 0 0
        0         0 0 0 1
        end
        
        reg a x s, vce(hc3)
        eststo col1
        sum a if x==0
        estadd scalar ymean= r(mean)
        
        reg b x s, vce(hc3)
        eststo col2
        sum b if x==0
        estadd scalar ymean= r(mean)
        
        reg c x s, vce(hc3)
        eststo col3
        sum c if x==0
        estadd scalar ymean= r(mean) 
        esttab col1 col2 col3 using "TABLE1.tex", replace ///        
        noobs nomti nonumb  ///       
        prehead("\begin{tabular}{lccc}" ///        
        "%" "\addlinespace" "%" ///       
         "%" "\toprule" "%" ///       
         "      &          \multicolumn{1}{c}{Attended}  &         \multicolumn{1}{c}{Hours} &         \multicolumn{1}{c}{Overnight}\\" ///  
         "      &          \multicolumn{1}{c}{Test Run}  &         \multicolumn{1}{c}{in Prison} &         \multicolumn{1}{c}{in Prison}\\" ///        
        " & (1) & (2) & (3) \\" ///                  
        "%" "\midrule " "%") ///        
        b(3) se star(* .9 ** .8 *** .7)  ///        
        keep(x  ) ///        order($coeff  )  ///       
         varlabels(x "Treatment group") ///        
        substitute(\_ _ [1em] ) ///        
        gaps nolines noeqlines ///        
        prefoot("\midrule Strata FE        & \checkmark & \checkmark & \checkmark \\") ///        
        stats(ymean N, labels("\midrule Control group mean" "Observations") fmt(3 a0)) ///        
        postfoot("\bottomrule" ///                  
        "\end{tabular}")
        
        
        // backup table and open new file
        copy TABLE1.tex tmp.tex
        file open fh using TABLE1.tex, write replace
        // write top lines
        file write fh "\documentclass{article}" _n
        file write fh "\usepackage{multirow}" _n
        file write fh "\usepackage{tabularx}" _n
        file write fh "\usepackage{amsmath}" _n
        file write fh "\usepackage{booktabs}" _n
        file write fh "\usepackage{ulem}" _n
        file write fh "\usepackage[table]{xcolor}" _n
        file write fh "\usepackage{amssymb}" _n
        file write fh "% Define significance stars as a command" _n
        file write fh "\newcommand{\sym}[1]{\rlap{#1}} % Aligns stars without affecting spacing" _n
        
        file write fh "\begin{document}" _n
        // now insert the document
        file open fh2 using tmp.tex, read
        file read fh2 line
        while r(eof)==0 {
            file write fh `"`line'"' _n
            file read fh2 line
        }
        file close fh2
        // write bottom lines
        file write fh _n "\end{document}" _n
        // clean up
        file close fh
        erase tmp.tex
        Res.:

        Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	31.5 KB
ID:	1775004

        Comment


        • #5
          Thanks, this helps a lot! However, now I have the problem that in some tables the stars are a bit too close to the next columns (see screenshot). Is there a way to make the starts smaller?
          Click image for larger version

Name:	Bildschirmfoto 2025-04-14 um 10.27.58.png
Views:	1
Size:	48.5 KB
ID:	1775965

          Comment


          • #6
            This looks more like a column width issue, I do not think that reducing the size of the stars is the solution. I would recommend that you replace tabular with tabularx. Define the total width (e.g.,\textwidth) and control which columns are flexible using X. Finally, use >{\centering\arraybackslash}X to center the flexible columns (see highlighted).

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(a b c x s)
            1 4.3430557 0 1 0
            1     14.77 0 1 1
            1   23.4425 1 1 1
            1 28.099167 1 1 0
            1 11.442778 0 1 0
            1 13.031667 1 1 0
            1 13.354167 1 1 0
            1 23.625555 1 1 1
            1  21.98611 1 1 0
            1 22.145834 1 0 0
            1  43.40083 1 1 0
            1  20.87389 1 1 0
            1 18.520277 1 1 0
            1  4.944722 0 1 0
            1  18.21611 1 1 1
            1 18.887222 1 1 0
            1  23.47111 1 0 1
            1      6.49 0 0 0
            1 18.405834 1 1 0
            1 18.079445 0 1 1
            1        25 1 1 1
            1  6.601944 0 1 0
            1 13.928333 1 1 0
            1  23.11361 1 0 0
            1  22.98611 1 1 0
            1  9.356112 0 1 0
            1 25.130556 1 1 1
            1 14.426666 1 0 1
            1 22.433056 1 1 0
            1    15.915 1 1 0
            1   19.6525 1 1 0
            1 23.795834 1 1 0
            1 16.606667 1 1 0
            1  15.02639 1 1 0
            1 14.207778 1 1 0
            1 18.195555 1 1 0
            1  23.98361 1 1 0
            1  23.11278 1 1 0
            1 10.685833 0 1 0
            1 30.309444 1 0 0
            1 13.466666 1 1 0
            1    20.155 1 0 0
            1  9.575556 1 1 1
            1 15.940278 1 1 1
            1 16.615278 1 0 0
            1  5.771111 0 1 1
            1 12.374166 1 1 0
            1 14.748055 1 1 0
            1  21.13667 1 1 0
            1 23.321945 1 1 0
            1 28.753334 1 1 0
            1    14.435 1 1 0
            1 17.554445 0 1 1
            1 15.053056 1 1 1
            1  46.38667 1 0 0
            1 18.603056 1 0 0
            1 16.770277 1 0 0
            1 17.812778 1 1 0
            1 21.746944 1 1 0
            1 28.918333 1 1 1
            1 23.528334 1 1 0
            1 29.935556 1 1 1
            1  23.80389 1 1 1
            1     48.02 1 0 0
            1  3.137778 0 1 0
            1  25.06861 1 1 0
            1 16.116667 1 1 0
            1 23.139444 1 1 0
            1  22.66889 1 1 0
            1 18.008612 1 1 0
            1  22.24861 1 1 0
            1  31.09611 1 1 0
            1 19.986944 1 1 0
            1  42.04306 1 1 0
            1  10.95111 0 1 0
            1  23.36389 1 1 0
            1  8.309167 0 0 0
            1  40.84889 1 1 0
            1 18.378334 1 1 0
            1  22.83472 1 1 0
            1  21.61611 1 1 1
            1  28.78639 1 1 1
            1  26.87389 1 1 1
            1 20.565556 1 1 0
            1  25.65389 1 1 0
            1 25.439444 1 1 0
            1 36.094444 1 1 0
            1  4.561111 0 1 1
            1  20.40389 1 1 1
            1  40.30194 1 1 0
            1   31.6125 1 1 0
            1  23.09611 1 1 0
            1  39.83055 1 1 0
            1  19.55778 1 1 0
            1 16.937222 1 1 0
            1 19.608334 1 1 0
            1 20.449444 1 1 0
            1 33.217777 1 1 0
            0         0 0 0 0
            0         0 0 0 1
            end
            
            reg a x s, vce(hc3)
            eststo col1
            sum a if x==0
            estadd scalar ymean= r(mean)
            
            reg b x s, vce(hc3)
            eststo col2
            sum b if x==0
            estadd scalar ymean= r(mean)
            
            reg c x s, vce(hc3)
            eststo col3
            sum c if x==0
            estadd scalar ymean= r(mean)
            esttab col1 col2 col3 using "TABLE1.tex", replace ///        
            noobs nomti nonumb  ///      
            prehead("\begin{tabularx}{\textwidth}{l>{\centering\arraybackslash}X>{\centering\arraybackslash}X>{\centering\arraybackslash}X}" ///        
            "%" "\addlinespace" "%" ///      
             "%" "\toprule" "%" ///      
             "      &          \multicolumn{1}{c}{Attended}  &         \multicolumn{1}{c}{Hours} &         \multicolumn{1}{c}{Overnight}\\" ///  
             "      &          \multicolumn{1}{c}{Test Run}  &         \multicolumn{1}{c}{in Prison} &         \multicolumn{1}{c}{in Prison}\\" ///        
            " & (1) & (2) & (3) \\" ///                  
            "%" "\midrule " "%") ///        
            b(3) se star(* .9 ** .8 *** .7)  ///        
            keep(x  ) ///        order($coeff  )  ///      
             varlabels(x "Treatment group") ///        
            substitute(\_ _ [1em] )  ///        
            gaps nolines noeqlines ///        
            prefoot("\midrule Strata FE        & \checkmark & \checkmark & \checkmark \\") ///        
            stats(ymean N, labels("\midrule Control group mean" "Observations") fmt(3 a0)) ///        
            postfoot("\bottomrule" ///                  
            "\end{tabularx}")
            
            
            // backup table and open new file
            copy TABLE1.tex tmp.tex
            file open fh using TABLE1.tex, write replace
            // write top lines
            file write fh "\documentclass{article}" _n
            file write fh "\usepackage{multirow}" _n
            file write fh "\usepackage{tabularx}" _n
            file write fh "\usepackage{amsmath}" _n
            file write fh "\usepackage{booktabs}" _n
            file write fh "\usepackage{ulem}" _n
            file write fh "\usepackage[table]{xcolor}" _n
            file write fh "\usepackage{amssymb}" _n
            file write fh "% Define significance stars as a command" _n
            file write fh "\newcommand{\sym}[1]{\rlap{#1}} % Aligns stars without affecting spacing" _n
            
            file write fh "\begin{document}" _n
            // now insert the document
            file open fh2 using tmp.tex, read
            file read fh2 line
            while r(eof)==0 {
                file write fh `"`line'"' _n
                file read fh2 line
            }
            file close fh2
            // write bottom lines
            file write fh _n "\end{document}" _n
            // clean up
            file close fh
            erase tmp.tex
            Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	33.1 KB
ID:	1775987

            Comment

            Working...
            X