Announcement

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

  • Export tables to LaTex

    Hello!

    I have received a code snippet that runs regressions and exports the results into tables in a Word document using the putdocx command. Since I did not write this code and was given not much background information, and because I am not very familiar with the putdocx command, I am finding it challenging to fully understand it and then generate LaTeX .tex files using the esttab command. Could you please advise on how I should modify the itt_3regs program to generate the .tex files?

    Code:
    ***** Definitions 
    
    * Outcomes
    global outcomes_det_emp empquality sh_aminwage sh_wrcon sh_sosec sh_wrcon_emp empretention  empturnover_outgoing
    global outcomes_det_hr hr_all hr_hours_documented hr_organigram_public hr_rec_formal hr_pay_1 hr_pay_2 hr_perfeval training_py
    global outcomes_det_bp bp_all bp_advert bp_record bp_stock bp_planning 
    global outcomes_det_acc acc formalaccounting accounting_docs acc_nonmanual sh_finstat_3years sh_finstat_submitted
    
    * Controls
    global control i.broad_sector i.ag_ano educ_diri_tert
    
    * Heterogeneity     
    global het revcat empcat firmagecat sectorcat abidjan_rec tert_diri_rec not_viable knowledge_baseline // 3 categories for firm sample. Baseline dep. var. categories are defined in sub-file. 
    
    * Samples
    // Samples: sample2 (wave 2), sample3 (wave 3), sample4 (waves 2+3 pooled sample, balanced)
    
    * SEs & strata
        global se_cs ", a(strata) r"
        global se_2 ", a(strata) r"
        global se_3 ", a(strata) r"
        global se_4 "i.wave, a(strata) vce(cl id)"
        
    /* Put word docs together */
    cd "$writing/Tables"
    putdocx append     balancing_firm.docx ///
                    results_firm_empqual_det.docx ///
                    results_firm_empqual_det_iv.docx ///
                    firm_robustness.docx ///
                    , saving("${writing}/tables.docx", replace) pagebreak     
    
    cap program drop itt_3regs
    
    
        program define itt_3regs
    
            eststo clear
    
            // Row and column titles
            local ${tbl}_ctitle "Assignment"
            
        
    *** Run regressions
        collect clear 
        collect create c${tbl}, replace 
    
        // Run regressions in loop 
        local c=1 
        foreach o of global outcomes {
            
            local d=`c'+1
            
            collect _r_b _r_se , tag(model[`c']): areg `o' assignment `o'_2021 $control i.wave mis_`o' i.ag_ano   , vce(cl id) a(strata)  // misreport_am_`o'
            *collect _r_b _r_se , tag(model[`c']): areg `o' assignment assignment##(c.`o'_2021) $control i.wave assignment##(mis_`o') i.ag_ano   , vce(cl id) a(strata)  // misreport_am_`o'
            di "Outcomes: $outcomes"
            *eststo ${tbl}_`c'
            eststo `o'
        
            // save p-values in matrix 
            matrix define ${tbl}_p`c' = (r(table)[4,1])
            matlist ${tbl}_p`c'
            matrix rownames ${tbl}_p`c'=`o'
            
            // save control group means in macro to include in table 
            sum `o' if assignment==0  // $sample &
            local ${tbl}_cm`c'=round(r(mean),.001)
            
            
            local ++c 
        }
        local c=`c'-1
        
    
    
    
    *** Assemble results in collect table 
        di "${varstodisplay_${tbl}}"
        collect layout (colname[assignment]#result result[r2 N] ) (model) 
        collect style header result, level(hide)              // no separate row for names of independent vars
        collect style header result[r2], level(label)        // Label row for R2
        collect style header result[N], level(label)          // Label row for N
        collect style cell, nformat(%5.3f)                     // Define number format in table 
        collect style cell result[N], nformat(%5.0f)        // Different number format for N 
        collect style cell border_block, border(right, pattern(nil)) 
        collect style cell result[_r_se], sformat("(%s)") // put SEs in brackets
        collect style cell cell_type[item column-header], halign(center)
        
        collect style showbase off
        collect style row stack, nobinder 
        collect notes "${notes_${tbl}}"
    
        collect levelsof cell_type
        collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "* ", attach(_r_b) shownote
    
        collect preview
        
        
        
        
        
    
    *** Results section in word doc: 
        *putdocx pagebreak 
        putdocx paragraph, style(Heading3)
        putdocx text ("Table $z")
        global z=$z +1
        putdocx paragraph, style(Heading4)
        putdocx text ("${ttl_${tbl}}")    
        /*putdocx paragraph
        putdocx text ("Table: ${tbl}") */
    
        // Defining table 
        collect style putdocx, name(c${tbl}) layout(autofitcontents) 
        putdocx collect, name(c${tbl}) tablename(${tbl}) 
        putdocx table ${tbl}(1,.), addrows(3, before) valign(center)
        
        global end_body=6
        global bottom=8
        global addrows=3
        
        forv b=1/$addrows {
            global bottom`b'=${bottom}+`b'
            
        }
    
        // Add controls
        putdocx table ${tbl}($bottom,.), addrows($addrows)
        putdocx table ${tbl}(${bottom1},1)=("Control mean"), halign(left)  
        putdocx table ${tbl}(${bottom2},1)=("Strata controls"), halign(left)  
        putdocx table ${tbl}(${bottom3},1)=("Lagged dep. var"), halign(left)  
        
        
        
        local e=`c'/3
        local d=2 
        forv i=1/`e' {
            di "Iteration `i': d=`d', e=`e'"
            // Column titles 
            putdocx table ${tbl}(1,`d')=("${${tbl}_ctitle_`i'}"), halign(center) colspan(3)
            di "${tbl}_${ctitle_`i'b}"
            putdocx table ${tbl}(2,`d')=("${${tbl}_ctitle_`i'b}"), halign(center) colspan(3)
            local ++d 
        } 
        
        
        local d=2 
        forv i=1/`c' {
            // Months since treatment
            putdocx table ${tbl}(3,`d')=("${${tbl}_months`i'}"), halign(center) 
    
            // Control means 
            local ${tbl}_cm`i' : display %3.2f (`${tbl}_cm`i'')
            putdocx table ${tbl}($bottom1,`d')=("`${tbl}_cm`i''"), halign(center) 
            
            // Strata controls
            putdocx table ${tbl}($bottom2,`d')=("Yes"), halign(center) 
    
            // Lagged dep. var
            putdocx table ${tbl}($bottom3,`d')=("Yes"), halign(center) 
            
            local ++d
    
        }
        
    
        
        // Specify font for whole table 
        putdocx table ${tbl}(.,.), font("Times New Roman", 10) 
        
        // Horizontal lines 
        putdocx table ${tbl}(.,.), border(top, nil) border(left, nil) border(right, nil) border(bottom, nil)
        putdocx table ${tbl}(1,.), border(top, double) 
        forv i=2/`d' {
            putdocx table ${tbl}(2,`i'), border(bottom, single)         
            putdocx table ${tbl}(3,`i'), border(bottom, single)         
        }
    
        putdocx table ${tbl}(4,.), border(bottom, single) // fully drawn bottom border after header 
        putdocx table ${tbl}(${end_body},.), border(bottom, single)
        putdocx table ${tbl}(${bottom},.), border(bottom, single)
        putdocx table ${tbl}(${bottom3},.), border(bottom, single)
        
        // Vertical lines to mark different dependent variables 
        forv i=1/6 {
            forv j=1/2 {
                putdocx table ${tbl}(`j',`i'), border(right, dashed)
            }
            
        }    
        
        forv i=1(3)16 {
            forv j=3/$bottom3 {
                putdocx table ${tbl}(`j',`i'), border(right, dashed)
            }
        }
        
        end 
    
    
    
    **# Bookmark #1: Contracts and social security 
    
    *** Load dataset
        cd "$dofiles"
        use 2_firm_regressions.dta, clear  
    
        
    **# Bookmark #Start writing document 
    
    
        putdocx clear 
        putdocx begin, pagesize(A4) font("Times New Roman") landscape margin(all, 1.5cm)
        putdocx paragraph, style(Heading2)
        putdocx text ("Firm-level results - detailed")    
    
    
        
    *** Define 
        global tbl det_empqual 
        global ttl_${tbl} "Employment quality"
        global outcomes empquality_2 empquality_3 empquality_4  sh_aminwage_2 sh_aminwage_3 sh_aminwage_4 sh_wrcon_2 sh_wrcon_3 sh_wrcon_4 sh_sosec_2 sh_sosec_3 sh_sosec_4  sh_wrcon_emp_2 sh_wrcon_emp_3 sh_wrcon_emp_4 empretention_2 empretention_3 empretention_4 empturnover_outgoing_2 empturnover_outgoing_3 empturnover_outgoing_4 // 
        global notes_${tbl} "${notes} 6M/18M: 6 months/18 months post-treatment. P: pooled sample of two follow-up surveys."
    
        
        global ${tbl}_ctitle_1 "Employment Quality"     
        global ${tbl}_ctitle_2 "> min. wage "         
        global ${tbl}_ctitle_3 "written contract"
        global ${tbl}_ctitle_4 "social security"    
        global ${tbl}_ctitle_5 "Wrcon - emp"    
        
    
        global ${tbl}_ctitle_1b "(Index 0-1)"
        global ${tbl}_ctitle_2b "(share)"
        global ${tbl}_ctitle_3b "(share)"
        global ${tbl}_ctitle_4b "(share)"
        global ${tbl}_ctitle_5b "(share)"
    
        
        global ${tbl}_months "Sample"
        global ${tbl}_months1 "6M"
        global ${tbl}_months2 "18M"
        global ${tbl}_months3 "P"
        global ${tbl}_months4 "6M"
        global ${tbl}_months5 "18M"
        global ${tbl}_months6 "P"
        global ${tbl}_months7 "6M"
        global ${tbl}_months8 "18M"
        global ${tbl}_months9 "P"    
        global ${tbl}_months10 "6M"
        global ${tbl}_months11 "18M"
        global ${tbl}_months12 "P"    
        global ${tbl}_months13 "6M"
        global ${tbl}_months14 "18M"
        global ${tbl}_months15 "P"        
    
        
    
    *** Run 
        *putdocx pagebreak 
        itt_3regs
    
    
    **# Bookmark #99: save word doc 
        
        cd "$writing/Tables"
        putdocx save "results_firm_empqual_det.docx", replace  
        
        
        ** Effects awarness
        mlogit q1461 assignment $control i.wave i.ag_ano i.strata if wave == 2, base(1)
        margins, dydx(assignment)
        label define knowledge 1 "No knowledge" 2 "Moderate knowledge" 3 "High knowledge"
        label values q1461 knowledge
        marginsplot, ///
        plotopts(lcolor(red) mcolor(red)) ///
        ciopts(recast(rcap) lcolor(red)) ///
        graphregion(color(white)) ///
        bgcolor(white) ///
        xlabel(1 "No knowledge" 2 "Moderate knowledge" 3 "High knowledge")
    Thank you in advance for any help you can provide

  • #2
    Lisa,

    putdocx creates Word documents, not LaTex documents. Your example code uses Stata's table command, not esttab to create a table. esttab is part of Ben Jann's estout suite available from ssc. Also, your code is quite complicated and it does not seem to be reproducible. Why not start with a simple example and build up to what you want from there? The estout documentation provides example code to make a latex table.

    Devra Golbe
    Professor Emerita, Dept. of Economics
    Hunter College, CUNY

    Comment


    • #3
      Hi Devra,

      Thank you for your response! I know that putdocx creates a Word document, that is what my code does at the end. However, I included the whole code so you would know what kind of tables I wanted to create on LaTex.

      I will look into your example code in more detail now. Thanks! I will also try to create a more simple code but since I am having a difficult time understading the original one, it might take a while. Thank you again for your response and suggestions.

      Alejandra

      Comment

      Working...
      X