Announcement

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

  • Export T-test to Latex

    Please, I would like export test results from stata to latex. Here's what I want to do.

    Code:
    sysuse auto
    test of the difference between the variables 'price' and 'turn'

    Code:
    ttest price=turn
    which yields

    Code:
    Paired t test
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
       price |      74    6165.257    342.8719    2949.496    5481.914      6848.6
        turn |      74    39.64865    .5114145    4.399354     38.6294     40.6679
    ---------+--------------------------------------------------------------------
        diff |      74    6125.608    342.7139    2948.137     5442.58    6808.636
    ------------------------------------------------------------------------------
         mean(diff) = mean(price - turn)                              t =  17.8738
     Ho: mean(diff) = 0                              degrees of freedom =       73
    
     Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
     Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
    Please, how can I export the last row ('diff') to latex? I am aware of estpost ttest, but it requires specifying a group variable which does not apply to my case. I am also aware of asdoc, but it's for exporting to word. I have resorted to manually coping and pasting results from stata into a table in latex but that's tedious especially when many variables are involved and one needs to run some regression several times and results change.

  • #2
    estout is from the Stata Journal. Consider this:

    Code:
    sysuse auto
    ttest price=turn
    preserve
    expand 2, g(new)
    gen diff= cond(new, turn, price)
    estpost ttest diff, by(new)
    esttab
    restore
    Res.:

    Code:
    . ttest price=turn
    
    Paired t test
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
       price |      74    6165.257    342.8719    2949.496    5481.914      6848.6
        turn |      74    39.64865    .5114145    4.399354     38.6294     40.6679
    ---------+--------------------------------------------------------------------
        diff |      74    6125.608    342.7139    2948.137     5442.58    6808.636
    ------------------------------------------------------------------------------
         mean(diff) = mean(price - turn)                              t =  17.8738
     Ho: mean(diff) = 0                              degrees of freedom =       73
    
     Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
     Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
    
    
    . ttest diff, by(new)
    
    Two-sample t test with equal variances
    ------------------------------------------------------------------------------
       Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
           0 |      74    6165.257    342.8719    2949.496    5481.914      6848.6
           1 |      74    39.64865    .5114145    4.399354     38.6294     40.6679
    ---------+--------------------------------------------------------------------
    combined |     148    3102.453    304.9675     3710.09    2499.766     3705.14
    ---------+--------------------------------------------------------------------
        diff |            6125.608    342.8723                5447.974    6803.242
    ------------------------------------------------------------------------------
        diff = mean(0) - mean(1)                                      t =  17.8656
    Ho: diff = 0                                     degrees of freedom =      146
    
        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
     Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
    
    
    . esttab
    
    ----------------------------
                          (1)  
                                
    ----------------------------
    diff               6125.6***
                      (17.87)  
    ----------------------------
    N                     148  
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Thanks Andrew. One more thing, please. Let's say groups 0 and 1 are actual and predicted values of some variable. Is it possible to reshape the table to the form below?:
      Code:
                                       (1)                     (2)                       (3)
                              actual                    predicted          ATE=actual-predicted
              mean           6165.275           39.64865                         6125.6 (17.87)
      Sorry the table alignment is poor. But the point is that under column (1) you have mean for actual, (2) for predicted, and (3) shows (1) - (2).

      Comment


      • #4
        Code:
        sysuse auto
        ttest price=turn
        preserve
        expand 2, g(new)
        gen diff= cond(new, turn, price)
        estpost ttest diff, by(new)
        esttab, cells(" b(fmt(3) pvalue(p) star) mu_1(fmt(3)) mu_2(fmt(3))" ///
        t(par fmt(2))) collab("ATE" "actual" "predicted") nonumbers coeflab(diff "mean")
        restore
        Res.:

        Code:
        . esttab, cells(" b(fmt(3) pvalue(p) star) mu_1(fmt(3)) mu_2(fmt(3))" ///
        > t(par fmt(2))) collab("ATE" "actual" "predicted") nonumbers coeflab(diff "mean")
        
        ------------------------------------------------------
                                                              
                              ATE          actual    predicted
        ------------------------------------------------------
        mean             6125.608***     6165.257       39.649
                          (17.87)                             
        ------------------------------------------------------
        N                     148                             
        ------------------------------------------------------

        Comment


        • #5
          Thanks a lot Andrew. Please, how can I include the standard errors in brackets (as it usually appears in regression tables) under ATE, actual and predicted. I simply took out "t(par fmt(2)))" and put "se(par))" in the code you gave as follows:

          Code:
          esttab, cells(" b(fmt(3) pvalue(p) star) mu_1(fmt(3)) mu_2(fmt(3))" /// 
          se(par)) collab("ATE" "actual" "predicted") nonumbers coeflab(diff "mean")
          which gives

          Code:
          ------------------------------------------------------
                                                                
                                ATE          actual    predicted
          ------------------------------------------------------
          mean             6125.608***     6165.257       39.649
                          (342.872)                             
          ------------------------------------------------------
          N                     148                             
          ------------------------------------------------------
          Please, could you show how to include the standard errors for actual and predicted?

          Comment


          • #6
            Code:
            sysuse auto
            ttest price=turn
            preserve
            expand 2, g(new)
            gen diff= cond(new, turn, price)
            eststo m1: mean diff if !new
            eststo m2: mean diff if new
            eststo m3: estpost ttest diff, by(new)
            esttab m1 m2 m3, se mlab(actual predicted ATE) coeflab(diff "mean")
            restore
            Res.:

            Code:
             
            . esttab m1 m2 m3, se mlab(actual predicted ATE) coeflab(diff "mean")
            
            ------------------------------------------------------------
                                  (1)             (2)             (3)   
                               actual       predicted             ATE   
            ------------------------------------------------------------
            mean               6165.3***        39.65***       6125.6***
                              (342.9)         (0.511)         (342.9)   
            ------------------------------------------------------------
            N                      74              74             148   
            ------------------------------------------------------------
            Standard errors in parentheses
            * p<0.05, ** p<0.01, *** p<0.001
            Last edited by Andrew Musau; 17 Mar 2021, 06:10.

            Comment


            • #7
              Thanks a lot. Sorry to drag this on. I want the stars to appear only on ATE. From the esttab documentation I see it's the stardrop or starkeep option. I've tried stardrop(actual predicted), starkeep(diff) among others but all result in errors.

              Comment


              • #8
                You can use the option -nostar- to suppress all the starts. If you ask me, it does not make sense to show standard errors for all and then only show stars for some. Either just show the means for statistics without stars or be consistent if displaying standard errors.
                Last edited by Andrew Musau; 17 Mar 2021, 12:22.

                Comment


                • #9
                  Ok. The stars are a very minor issue. Thanks a lot for your help.

                  Comment


                  • #10
                    If you must, use the -substitute- option, but also change the note at the foot of the table so that the reader is not misled.

                    Code:
                    sysuse auto, clear
                    ttest price=turn
                    preserve
                    expand 2, g(new)
                    gen diff= cond(new, turn, price)
                    eststo m1: mean diff if !new
                    eststo m2: mean diff if new
                    eststo m3: estpost ttest diff, by(new)
                    esttab m1 m2 m3, se mlab(actual predicted ATE) coeflab(diff "mean") ///
                    substitute("39.65***" "39.65   "  "6165.3***"   "6165.3   ") ///
                    addnote("Significance stars only shown for ATE estimates")
                    Res.:

                    Code:
                    ------------------------------------------------------------
                                          (1)             (2)             (3)  
                                       actual       predicted             ATE  
                    ------------------------------------------------------------
                    mean               6165.3           39.65          6125.6***
                                      (342.9)         (0.511)         (342.9)  
                    ------------------------------------------------------------
                    N                      74              74             148  
                    ------------------------------------------------------------
                    Standard errors in parentheses
                    Significance stars only shown for ATE estimates
                    * p<0.05, ** p<0.01, *** p<0.001
                    Last edited by Andrew Musau; 17 Mar 2021, 14:22.

                    Comment


                    • #11
                      Perfect. Thanks a lot.

                      Comment


                      • #12
                        Thank you all for helpful tips. I have been looking for code to export summary tables (mean, SD) for categorical variables.
                        Any simple way to export “oneway price rep78, tab” results to Latex, excel or similar?
                        PS: As example here am assuming rep78 as a categorial variable.

                        Click image for larger version

Name:	stata sample.png
Views:	1
Size:	5.0 KB
ID:	1604897
                        Last edited by Tiago Munhoz; 21 Apr 2021, 08:33.

                        Comment


                        • #13
                          With the new table building features in Stata 17 you can design custom styles and labels for your table, save them for use with the table command, and then export your table to LaTeX.

                          Here is some Stata 17 code to almost reproduce Tiago's example above.
                          Code:
                          sysuse auto
                          
                          * create custom labels and styles
                          * custom labels
                          collect label dim result "Summary of Price"
                          collect label levels result sd "Std. dev." frequency "Freq."
                          * start with with factory default styles
                          collect style clear
                          * usual borders between the table headers and table data
                          collect style cell border_block[corner], border(right bottom)
                          collect style cell border_block[row-header], border(right)
                          collect style cell border_block[column-header], border(bottom)
                          * border for "Total"
                          collect style cell cell[row-header]#rep78[.m], border(right)
                          collect style cell rep78[.m], border(top)
                          * numeric format
                          collect style cell result[mean sd], nformat(%9.2fc)
                          * header styles
                          collect style row stack, noindent length(11)
                          collect style column, dups(center)
                          * save label and styles to a single file, easy to reference
                          collect save mysumtab, replace
                          
                          * compute summary statistics and specify our styles and labels
                          table rep78, ///
                              stat(mean price) ///
                              stat(sd price) ///
                              stat(freq) ///
                              style(mysumtab) ///
                              label(mysumtab)
                          
                          * export to LaTeX
                          collect export example.tex, replace
                          type example.tex
                          Here is a log of the above.
                          Code:
                          . sysuse auto
                          (1978 automobile data)
                          
                          . 
                          . * create custom labels and styles
                          . * custom labels
                          . collect label dim result "Summary of Price"
                          
                          . collect label levels result sd "Std. dev." frequency "Freq."
                          
                          . * start with with factory default styles
                          . collect style clear
                          
                          . * usual borders between the table headers and table data
                          . collect style cell border_block[corner], border(right bottom)
                          (dimension border_block not found)
                          
                          . collect style cell border_block[row-header], border(right)
                          (level row-header of dimension border_block not found)
                          
                          . collect style cell border_block[column-header], border(bottom)
                          (level column-header of dimension border_block not found)
                          
                          . * border for "Total"
                          . collect style cell cell[row-header]#rep78[.m], border(right)
                          (dimension cell not found)
                          (dimension rep78 not found)
                          
                          . collect style cell rep78[.m], border(top)
                          
                          . * numeric format
                          . collect style cell result[mean sd], nformat(%9.2fc)
                          (dimension result not found)
                          
                          . * header styles
                          . collect style row stack, noindent length(11)
                          
                          . collect style column, dups(center)
                          
                          . * save label and styles to a single file, easy to reference
                          . collect save mysumtab, replace
                          (collection default saved to file mysumtab.stjson)
                          
                          . 
                          . * compute summary statistics and specify our styles and labels
                          . table rep78, ///
                          >         stat(mean price) ///
                          >         stat(sd price) ///
                          >         stat(freq) ///
                          >         style(mysumtab) ///
                          >         label(mysumtab)
                          
                                      |     Summary of Price    
                                      |     Mean Std. dev. Freq.
                          ------------+-------------------------
                               Repair |                         
                          record 1978 |                         
                                    1 | 4,564.50    522.55     2
                                    2 | 5,967.62  3,579.36     8
                                    3 | 6,429.23  3,525.14    30
                                    4 | 6,071.50  1,709.61    18
                                    5 | 5,913.00  2,615.76    11
                          ------------+-------------------------
                                Total | 6,146.04  2,912.44    69
                          
                          . 
                          . * export to LaTeX
                          . collect export example.tex, replace
                          (collection Table exported to file example.tex)
                          
                          . type example.tex
                          \documentclass{article}
                          \usepackage{multirow}
                          \usepackage{amsmath}
                          \usepackage{ulem}
                          \usepackage[table]{xcolor}
                          \begin{document}
                          \begin{tabular}{llll}
                          \multicolumn{1}{r}{} &
                            \multicolumn{3}{|c}{Summary of Price} \\
                          \multicolumn{1}{r}{} &
                            \multicolumn{1}{|r}{Mean} &
                            \multicolumn{1}{r}{Std. dev.} &
                            \multicolumn{1}{r}{Freq.} \\
                          \cline{1-4}
                          \multicolumn{1}{r}{Repair} &
                            \multicolumn{1}{|r}{} &
                            \multicolumn{1}{r}{} &
                            \multicolumn{1}{r}{} \\
                          \multicolumn{1}{r}{record 1978} &
                            \multicolumn{1}{|r}{} &
                            \multicolumn{1}{r}{} &
                            \multicolumn{1}{r}{} \\
                          \multicolumn{1}{r}{1} &
                            \multicolumn{1}{|r}{4,564.50} &
                            \multicolumn{1}{r}{522.55} &
                            \multicolumn{1}{r}{2} \\
                          \multicolumn{1}{r}{2} &
                            \multicolumn{1}{|r}{5,967.62} &
                            \multicolumn{1}{r}{3,579.36} &
                            \multicolumn{1}{r}{8} \\
                          \multicolumn{1}{r}{3} &
                            \multicolumn{1}{|r}{6,429.23} &
                            \multicolumn{1}{r}{3,525.14} &
                            \multicolumn{1}{r}{30} \\
                          \multicolumn{1}{r}{4} &
                            \multicolumn{1}{|r}{6,071.50} &
                            \multicolumn{1}{r}{1,709.61} &
                            \multicolumn{1}{r}{18} \\
                          \multicolumn{1}{r}{5} &
                            \multicolumn{1}{|r}{5,913.00} &
                            \multicolumn{1}{r}{2,615.76} &
                            \multicolumn{1}{r}{11} \\
                          \cline{1-4}
                          \multicolumn{1}{r}{Total} &
                            \multicolumn{1}{|r}{6,146.04} &
                            \multicolumn{1}{r}{2,912.44} &
                            \multicolumn{1}{r}{69} \\
                          \end{tabular}
                          \end{document}
                          And here is the PDF result after running pdflatex example.
                          Attached Files

                          Comment


                          • #14
                            Thanks a lot, Jeff. Fantastic solution/example.
                            Unfortunately, I am using Stata 16.1 and cannot run it. Anyway, thanks for helping me.

                            Comment


                            • #15
                              Thanks Jeff Pitblado (StataCorp) for the nice illustration. I suspect that these external exporting routines will fall into disuse over time. The statistics in the requested table can be generated using standard egen functions. The following uses estout from SSC to export these. All you need to be changing in the code are the names of the variables (highlighted in red).

                              Code:
                              sysuse auto, clear
                              local byvar rep78
                              gen Mean= price
                              drop if missing(`byvar')
                              expand 2, g(new)
                              qui sum `byvar'
                              assert `byvar'!=99999
                              replace `byvar'= 99999 if new
                              bys `byvar': egen Std_Err=sd(Mean)
                              bys `byvar': egen Freq=count(Mean)
                              collapse Mean Std_Err Freq, by("`byvar'")
                              estpost tabstat  Mean Std_Err Freq, by("`byvar'")
                              esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
                              collab(,lhs("`:var lab `byvar''")) drop(Total) ///
                              substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20)
                              *LaTeX Table
                              esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
                              collab(,lhs("`:var lab `byvar''")) drop(Total) ///
                              substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20) tex
                              Res.:

                              Code:
                              
                              .
                              . esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
                              > collab(,lhs("`:var lab `byvar''")) drop(Total) ///
                              > substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20)
                              
                              -----------------------------------------------------------
                              Repair Record 1978           Mean      Std. Err         Freq
                              -----------------------------------------------------------
                              1                          4564.5     522.5519            2
                              2                        5967.625     3579.357            8
                              3                        6429.233      3525.14           30
                              4                          6071.5     1709.608           18
                              5                            5913     2615.763           11
                              Total                    6146.043      2912.44           69
                              -----------------------------------------------------------
                              
                              . *LaTeX Table
                              . esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
                              > collab(,lhs("`:var lab `byvar''")) drop(Total) ///
                              > substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20) tex
                              
                              {
                              \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
                              \begin{tabular}{l*{1}{ccc}}
                              \hline\hline
                              Repair Record 1978  &        Mean&     Std. Err&        Freq\\
                              \hline
                              1                   &      4564.5&    522.5519&           2\\
                              2                   &    5967.625&    3579.357&           8\\
                              3                   &    6429.233&     3525.14&          30\\
                              4                   &      6071.5&    1709.608&          18\\
                              5                   &        5913&    2615.763&          11\\
                              Total               &    6146.043&     2912.44&          69\\
                              \hline\hline
                              \end{tabular}
                              }
                              Last edited by Andrew Musau; 21 Apr 2021, 13:58.

                              Comment

                              Working...
                              X