benjann / estout

Stata module to make regression tables
http://repec.sowi.unibe.ch/stata/estout/index.html
MIT License
71 stars 18 forks source link

esttab: using mgroups() with matrix #25

Closed luizaandrade closed 3 years ago

luizaandrade commented 4 years ago

I'm trying to export a LaTeX table with a custom matrix using esttab. I just wanted to confirm that the option mgroups() cannot be used with matrices.

It makes some sense, since matrix columns are not necessarily models, but would be a nice feature to add so it's easier to create custom tables.

(Big fan of your commands, btw. Thank you for developing them!)

Reproducible example

Create a mock matrix

sysuse census.dta, clear

reg death marriage pop      
matrix a = r(table)

I would expect to write something like this

esttab  matrix(a) using "../outputs/Raw/mat-test.tex", ///
    replace ///
    mgroups("Title 1" "Title 2", ///
        pattern(1 0 1) ///
        prefix(\multicolumn{@span}{c}{) ///
        suffix(}) ///
        span erepeat(\cmidrule(lr){@span}))

mat-test.tex looks like this:

\begin{tabular}{l*{3}{c}}
\hline\hline
            &\multicolumn{3}{c}{Title 1}           \\\cmidrule(lr){2-4}
            &           a&            &            \\
            &    marriage&         pop&       \_cons\\
\hline
b           &   -.0848932&     .009489&    650.9261\\
se          &     .051956&    .0004973&    1347.445\\
t           &   -1.633945&    19.08104&    .4830819\\
pvalue      &    .1089521&    9.03e-24&    .6312804\\
ll          &   -.1894152&    .0084886&   -2059.783\\
ul          &    .0196287&    .0104895&    3361.635\\
df          &          47&          47&          47\\
crit        &    2.011741&    2.011741&    2.011741\\
eform       &           0&           0&           0\\
\hline\hline
\end{tabular}

This one creates the table I want

esttab  matrix(a) using "../outputs/Raw/mat-works.tex", ///
    replace ///
    prehead("\begin{tabular}{l*{3}{c}}" ///
        "\hline\hline" ///
        "& \multicolumn{2}{c}{Title 1} & Title 2 \\" ///
        "\cmidrule(lr){2-3} {4-4}")

mat-works.tex looks like this:

\begin{tabular}{l*{3}{c}}
\hline\hline
& \multicolumn{2}{c}{Title 1} & Title 2 \\
\cmidrule(lr){2-3} {4-4}
            &           a&            &            \\
            &    marriage&         pop&       \_cons\\
\hline
b           &   -.0848932&     .009489&    650.9261\\
se          &     .051956&    .0004973&    1347.445\\
t           &   -1.633945&    19.08104&    .4830819\\
pvalue      &    .1089521&    9.03e-24&    .6312804\\
ll          &   -.1894152&    .0084886&   -2059.783\\
ul          &    .0196287&    .0104895&    3361.635\\
df          &          47&          47&          47\\
crit        &    2.011741&    2.011741&    2.011741\\
eform       &           0&           0&           0\\
\hline\hline
\end{tabular}
benjann commented 4 years ago

Exactly. Technically, the whole matrix is a single "model". I see no other solution to get what you want other then putting together the header manually as in your example. Alternatively, use option -fragment- to write only the table contents and take care of the latex code around the contents directly in LaTeX.

luizaandrade commented 4 years ago

I see. Thank you!