Announcement

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

  • Difficulty in accessing my results tables via LaTex

    Hello,

    I am practicing how to create customised LaTex regression results tables in STATA. However, when I try compiling the LaTex results tables into pdf, Latex gives me an error message that reads "LaTex Error: Environment Table Undefined". Could there be a problem with the STATA code I wrote to create the tables? Here is the STATA code I wrote:

    use wagepan, clear

    xtset nr year
    gen lhour = ln(hour)

    levelsof year, loc(yr)
    loc nyr: word count `yr'
    di "NOTE: This is an `nyr' panel data"

    forval i = 1/`nyr' {
    loc ny: word `i' of `yr'
    quietly {
    reg lwage lhour educ if year == `ny'
    margins, dyex(educ)
    matrix eta = r(b)
    matrix s2eta = r(V)
    matrix list eta
    matrix list s2eta
    eststo, add(eta_educ eta[1, 1] s_educ sqrt(s2eta[1, 1]))
    reg lwage lhour educ black hisp if year == `ny'
    margins, dyex(educ)
    matrix eta = r(b)
    matrix s2eta = r(V)
    matrix list eta
    matrix list s2eta
    eststo, add(eta_educ eta[1, 1] s_educ sqrt(s2eta[1, 1]))
    reg lwage lhour educ c.exper##c.exper black hisp if year == `ny'
    margins, dyex(educ exper)
    matrix eta = r(b)
    matrix s2eta = r(V)
    matrix list eta
    matrix list s2eta
    eststo, add(eta_educ eta[1, 1] s_educ sqrt(s2eta[1, 1]) eta_exper eta[1, 2] s_exper sqrt(s2eta[1, 2]))
    }
    esttab _all using lwage`ny'.tex, replace ti("Wage Equation Estimation for `ny'") ///
    label nomtitle nodepvars not se noobs ar2 booktabs ///
    scalar(eta_educ s_educ eta_exper s_exper) ///
    addnotes("$\eta$: Semi-elasticity of lwage with respect to educ, exper" ///
    "$\eta_{se}$: Standard errors of the semi-elasticity") ///
    substitute("_cons" "Constant" "eta_educ" "$\eta_{educ}$" "s_educ" ///
    "$\eta_{se}$" "eta_exper" "$\eta_{exper}$" "s_exper" "$\eta_{se}$" ///
    "c.exper#c.exper" "Experience$^2$")
    }



    I NEED HELP PLEASE

  • #2
    estout is from SSC (FAQ Advice #12). When creating a LaTeX document, you need packages to provide additional options or functionality, see https://guides.lib.wayne.edu/latex/packages. Here is an example that defines a couple within Stata. Run in a do-file.

    Code:
    sysuse auto, clear
    gen all = 1
    estimates clear
    replace weight= weight/1000
    foreach var in foreign !foreign all{
        eststo: regress mpg weight disp if `var'
        eststo: regress mpg weight disp turn if `var'
    }
    esttab est* using mytable.tex, cells(b(star fmt(2)) t(par fmt(2))) ///
    label mgroups("Foreign" "Domestic" "All cars", pattern(1 0 1 0 1 0) ///
    prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    collabels(none) replace nomtitles
    
    // backup table and open new file
    copy mytable.tex tmp.tex
    file open fh using mytable.tex, write replace
    // write top lines
    file write fh "\documentclass{article}" _n
    file write fh "\usepackage{multirow}" _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 "\begin{document}" _n
    file write fh "\begin{table}[ht]" _n
    file write fh _n "\begin{footnotesize}" _n
    file write fh "\centering" _n
    // now insert the table
    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{footnotesize}" _n
    file write fh _n "\end{table}" _n
    file write fh _n "\end{document}" _n
    // clean up
    file close fh
    erase tmp.tex

    After compilation, you should get:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	46.3 KB
ID:	1661919

    Comment


    • #3
      Thank you very much Prof. for your help. I will follow your technique and give it a shot again. I respectfully revert if I encounter any further challenge

      Comment


      • #4
        Thank you very much Prof. It has worked perfectly now. This is a sample of the results attached. But one more issue that seems tricky to me here. The results I have in my analysis consist of 8 separate tables each containing 3 equations. So to compile them, I decided to loop the compilation through your helpful technique but that keeps duplicating some of the equations on some of the tables. For instance, you could see some of the equations in the 1980 table duplicated on the tables of 1981 and so on. This is how I constructed the loop.

        ** Backing up the tables and open new file
        loc resf lwage1980.tex lwage1981.tex ///
        lwage1982.tex lwage1983.tex ///
        lwage1984.tex lwage1985.tex ///
        lwage1986.tex lwage1987.tex

        foreach file of loc resf {
        copy `file' tmp.tex, replace
        ** Writting the top lines
        file open fh using `file', write replace
        file write fh "\documentclass{article}" _n
        file write fh "\usepackage{multirow}" _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 "\begin{document}" _n
        //file write fh "\begin{table}[ht]" _n
        file write fh _n "\begin{footnotesize}" _n
        file write fh "\centering" _n
        ** Inserting the tables
        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
        ** Writting the bottom lines
        file write fh "\end{footnotesize}" _n
        //file write fh "\end{table}" _n
        file write fh "\end{document}"
        ** Cleaning up
        file close fh
        erase tmp.tex
        }


        I need your kind assistance please.
        Attached Files
        Last edited by Felix Larry Essilfie; 30 Apr 2022, 05:16.

        Comment


        • #5
          Originally posted by Felix Larry Essilfie View Post
          The results I have in my analysis consist of 8 separate tables each containing 3 equations. So to compile them, I decided to loop the compilation through your helpful technique but that keeps duplicating some of the equations on some of the tables. For instance, you could see some of the equations in the 1980 table duplicated on the tables of 1981 and so on.
          I am not sure that I follow. You only show the table corresponding to the year 1980. Can you show what you describe? Also, do you simply want to append the tables or what?

          Comment


          • #6
            Thank you Prof. for your response. I have actually resolved the issue. My problem was the fact that I had to clear stored estimates before proceeding with the regression estimation by adding a command line "eststo clear" in my first loop for the regression estimation. That was why I was getting those funny tables. But once I added that command line, I got the issue resolved.

            Once again, thank you.

            I truly appreciate your time.

            Comment

            Working...
            X