Announcement

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

  • Export 3-way table (frequency) into latex

    Dear everyone,

    I would like to export my 3-way table into Latex, but I don't know how to do. I hope that somebody can help me
    Code:
    table g g_lag dis, contents(freq)  sc col row
    .

    The results of this command is as follow
    Click image for larger version

Name:	Screen Shot 2020-03-17 at 10.37.57 PM.png
Views:	1
Size:	35.2 KB
ID:	1541725



    Thank you so much. I am really appreciated your help.

  • #2
    I find the estout package from SSC is the best way to export tables to Latex. Using the estpost command in that package, you can run your three-way table as three separate 2-way tables, then combine them with estout and output to Latex. Estout has a lot of options (see http://repec.sowi.unibe.ch/stata/estout/) that can help you get the look you want.

    Code:
    ssc install estout 
    
    *setup demonstration data with three binary variables
    sysuse auto, clear
    gen mp = (mpg>25)
    gen rep = (rep78>=4) if !mi(rep78)
    
    *replicate table format
    table foreign mp rep, contents(freq) sc col row
    
    *make 3 two-way tabs, saving them with eststo
    eststo col0: estpost tab foreign mp if rep==0
    eststo col1: estpost tab foreign mp if rep==1
    eststo colT: estpost tab foreign mp 
    
    *show how table looks with esttab defaults... pretty close, just missing the top header
    esttab col0 col1 colT, unstack not nodepvars noobs nonote nonumbers mtitles("0" "1" "Total")  type modelwidth(5)
    
    *modify to make latex table with top header and title
    estout col0 col1 colT using mytable.tex, style(tex) replace unstack /// setup
        mlabels("0" "1" "Total", span prefix(\multicolumn{@span}{c}{) suffix(})  erepeat(\cline{@span})) eqlabels(,lhs("Origin")) collabels(none) /// labels for columns
        title("Tabulation of Foreign by Repair Record and MPG") /// 
        prehead("\begin{table}[htbp]\centering" "\caption{@title}" "\begin{tabular}{l*{9}{c}}" "\hline" "& \multicolumn{9}{c}{Repair Record and MPG} \\") posthead(\hline) prefoot(\hline) postfoot("\end{tabular}" "\end{table}") // latex setup and last column header

    Comment


    • #3
      Originally posted by Kye Lippold View Post
      I find the estout package from SSC is the best way to export tables to Latex. Using the estpost command in that package, you can run your three-way table as three separate 2-way tables, then combine them with estout and output to Latex. Estout has a lot of options (see http://repec.sowi.unibe.ch/stata/estout/) that can help you get the look you want.

      Code:
      ssc install estout
      
      *setup demonstration data with three binary variables
      sysuse auto, clear
      gen mp = (mpg>25)
      gen rep = (rep78>=4) if !mi(rep78)
      
      *replicate table format
      table foreign mp rep, contents(freq) sc col row
      
      *make 3 two-way tabs, saving them with eststo
      eststo col0: estpost tab foreign mp if rep==0
      eststo col1: estpost tab foreign mp if rep==1
      eststo colT: estpost tab foreign mp
      
      *show how table looks with esttab defaults... pretty close, just missing the top header
      esttab col0 col1 colT, unstack not nodepvars noobs nonote nonumbers mtitles("0" "1" "Total") type modelwidth(5)
      
      *modify to make latex table with top header and title
      estout col0 col1 colT using mytable.tex, style(tex) replace unstack /// setup
      mlabels("0" "1" "Total", span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span})) eqlabels(,lhs("Origin")) collabels(none) /// labels for columns
      title("Tabulation of Foreign by Repair Record and MPG") ///
      prehead("\begin{table}[htbp]\centering" "\caption{@title}" "\begin{tabular}{l*{9}{c}}" "\hline" "& \multicolumn{9}{c}{Repair Record and MPG} \\") posthead(\hline) prefoot(\hline) postfoot("\end{tabular}" "\end{table}") // latex setup and last column header
      Thank you so much. I will try to do with my case in same logic. Many thanksssss

      Comment

      Working...
      X