Skip to content

Commit bf92748

Browse files
dsliwkas3alfisc
andauthored
Layout improvements for etable, in particular for LaTex output (#583)
* Added feature to relabel variables in etable * Added explanation of relabeling feature * update etable docstring for order of keep, drop, label, felabels * add API tests * test only felabels * Regression table layout improvements; latex table layout; custom model headers & custom table notes * update stargazer nb on with new features * small cleanups * PR allows newline in tex * update readme * add tests * small update * bring back Dirk's notebook changes * bring back Dirks nb * delete fls changes * Small layout changes for type="df" * Option to print latex string in etable * Set default of print_tex to False (Reason: A user who includes the tables in a separate latex file or in overleaf will likely not want to see the printed code in the notebook. Moreover, for quarto rendering it must apparently be printed in any case in the end of the code block. When print_tex=True then the rendered document will also show the latex code block in addition to the table). * Edited the Stargazer notebook to document latex output and the use in quarto * Few edits in documentation * Corrected mistake I made in earlier commit * Display YAML code for quarto example as plain text * Update YAML code for quarto example as plain text * As the inclusion of the quarto example in the Stargazer notebook caused some issues, I have taken this out of the notebook and instead refer to an added qmd file that gives as an eample for including tables in quarto. * do not run, only format python code in quarto example * install tinytex in github action * add API test for print_tex argument * Allow to hide fixed effects and se-type rows. Allow to add custom model information/further model statistics. * Explain the new features in the Stargazer notebook * Update documentation * fix small typos --------- Co-authored-by: Alexander Fischer <[email protected]>
1 parent a1c3af7 commit bf92748

File tree

13 files changed

+2694
-830
lines changed

13 files changed

+2694
-830
lines changed

.coverage

0 Bytes
Binary file not shown.

.github/workflows/ci-tests.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ jobs:
6666
run: poetry install --with docs
6767
- name: Set up Quarto
6868
uses: quarto-dev/quarto-actions/setup@v2
69+
- name: install tinytex
70+
run: |
71+
quarto install tinytex
6972
- name: Compile docstrings with quartodoc
7073
run : |
7174
poetry run quartodoc build --verbose --config docs/_quarto.yml

docs/latexdocs/SampleTableDoc.pdf

105 KB
Binary file not shown.

docs/latexdocs/SampleTableDoc.tex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
\documentclass{article}%
2+
\usepackage[T1]{fontenc}%
3+
\usepackage[utf8]{inputenc}%
4+
\usepackage{lmodern}%
5+
\usepackage{textcomp}%
6+
\usepackage{lastpage}%
7+
\usepackage{booktabs}%
8+
\usepackage{threeparttable}%
9+
\usepackage{makecell}%
10+
%
11+
%
12+
%
13+
\begin{document}%
14+
\normalsize%
15+
\section{A PyFixest LateX Table}%
16+
\label{sec:APyFixestLateXTable}%
17+
18+
19+
\begin{table}[htbp]%
20+
\renewcommand\cellalign{t}
21+
\begin{threeparttable}
22+
\begin{tabular}{lcccccc}
23+
\toprule
24+
& \multicolumn{3}{c}{Y} & \multicolumn{3}{c}{Y2} \\
25+
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
26+
& (1) & (2) & (3) & (4) & (5) & (6) \\
27+
\midrule
28+
X1 & \makecell{-0.95*** \\ (0.07)} & \makecell{-0.92*** \\ (0.06)} & \makecell{-0.92*** \\ (0.06)} & \makecell{-1.27*** \\ (0.17)} & \makecell{-1.23*** \\ (0.19)} & \makecell{-1.23*** \\ (0.19)} \\
29+
X2 & \makecell{-0.17*** \\ (0.02)} & \makecell{-0.17*** \\ (0.01)} & \makecell{-0.19*** \\ (0.03)} & \makecell{-0.13*** \\ (0.04)} & \makecell{-0.12*** \\ (0.04)} & \makecell{-0.07 \\ (0.10)} \\
30+
X1:X2 & & & \makecell{0.01 \\ (0.02)} & & & \makecell{-0.04 \\ (0.08)} \\
31+
\midrule
32+
f1 & x & x & x & x & x & x \\
33+
f2 & - & x & x & - & x & x \\
34+
\midrule
35+
$R^2$ & 0.49 & 0.66 & 0.66 & 0.12 & 0.17 & 0.17 \\
36+
S.E. type & by: f1 & by: f1 & by: f1 & by: f1 & by: f1 & by: f1 \\
37+
Observations & 997 & 997 & 997 & 998 & 998 & 998 \\
38+
\bottomrule
39+
\end{tabular}
40+
\footnotesize Significance levels: $*$ p $<$ 0.1, $**$ p $<$ 0.05, $***$ p $<$ 0.01. Format of coefficient cell: Coefficient
41+
(Std. Error)
42+
\end{threeparttable}%
43+
\end{table}
44+
45+
%
46+
\end{document}

docs/latexdocs/SampleTableDoc2.pdf

104 KB
Binary file not shown.

docs/latexdocs/SampleTableDoc2.tex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
\documentclass{article}%
2+
\usepackage[T1]{fontenc}%
3+
\usepackage[utf8]{inputenc}%
4+
\usepackage{lmodern}%
5+
\usepackage{textcomp}%
6+
\usepackage{lastpage}%
7+
\usepackage{booktabs}%
8+
\usepackage{threeparttable}%
9+
\usepackage{makecell}%
10+
%
11+
%
12+
%
13+
\begin{document}%
14+
\normalsize%
15+
\section{A PyFixest LateX Table}%
16+
\label{sec:APyFixestLateXTable}%
17+
18+
19+
\begin{table}[htbp]%
20+
\renewcommand\cellalign{t}
21+
\begin{threeparttable}
22+
\begin{tabular}{lcccccc}
23+
\toprule
24+
& \multicolumn{2}{c}{US} & \multicolumn{2}{c}{China} & \multicolumn{2}{c}{EU} \\
25+
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}
26+
& Wage & Wealth & Wage & Wealth & Wage & Wealth \\
27+
& (1) & (2) & (3) & (4) & (5) & (6) \\
28+
\midrule
29+
Age & \makecell{-0.950*** \\ (0.067)} & \makecell{-1.267*** \\ (0.174)} & \makecell{-0.924*** \\ (0.061)} & \makecell{-1.232*** \\ (0.192)} & \makecell{-0.924*** \\ (0.061)} & \makecell{-1.231*** \\ (0.192)} \\
30+
Years of Schooling & \makecell{-0.174*** \\ (0.018)} & \makecell{-0.131** \\ (0.042)} & \makecell{-0.174*** \\ (0.015)} & \makecell{-0.118** \\ (0.042)} & \makecell{-0.185*** \\ (0.025)} & \makecell{-0.074 \\ (0.104)} \\
31+
Age $\times$ Years of Schooling & & & & & \makecell{0.011 \\ (0.018)} & \makecell{-0.041 \\ (0.081)} \\
32+
\midrule
33+
Industry & x & x & x & x & x & x \\
34+
Year & - & - & x & x & x & x \\
35+
\midrule
36+
$R^2$ & 0.489 & 0.120 & 0.659 & 0.172 & 0.659 & 0.172 \\
37+
S.E. type & by: f1 & by: f1 & by: f1 & by: f1 & by: f1 & by: f1 \\
38+
Observations & 997 & 998 & 997 & 998 & 997 & 998 \\
39+
\bottomrule
40+
\end{tabular}
41+
\footnotesize Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
42+
\end{threeparttable}%
43+
\end{table}
44+
45+
%
46+
\end{document}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: "Quarto and PyFixest"
3+
jupyter: python3
4+
format:
5+
pdf:
6+
number-sections: true
7+
include-in-header:
8+
- text: |
9+
\usepackage{booktabs}
10+
\usepackage{makecell}
11+
\usepackage{threeparttable}
12+
execute:
13+
echo: false
14+
warning: false
15+
author:
16+
- Peter Pan^[University of Neverland]
17+
date: last-modified
18+
abstract: |
19+
\
20+
\
21+
We study the effect of X1 and X2 on Y and Y2 using PyFixest. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
22+
\
23+
\
24+
**Keywords**: Regressions, PyFixest, Tables, Quarto
25+
---
26+
27+
{{< pagebreak >}}
28+
29+
# Introduction
30+
31+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
32+
33+
```{python}
34+
#| echo: false
35+
#| output: false
36+
import pandas as pd
37+
import pyfixest as pf
38+
39+
data = pf.get_data()
40+
41+
fit1 = pf.feols("Y ~ X1 + X2 | f1", data = data)
42+
fit2 = pf.feols("Y ~ X1 + X2 | f1 + f2", data = data)
43+
fit3 = pf.feols("Y ~ X1 *X2 | f1 + f2", data = data)
44+
fit4 = pf.feols("Y2 ~ X1 + X2 | f1", data = data)
45+
fit5 = pf.feols("Y2 ~ X1 + X2 | f1 + f2", data = data)
46+
fit6 = pf.feols("Y2 ~ X1 *X2 | f1 + f2", data = data)
47+
48+
labels={
49+
"Y": "Wage",
50+
"Y2": "Wealth",
51+
"X1": "Age",
52+
"X2": "Years of Schooling",
53+
"f1": "Industry",
54+
"f2": "Year"
55+
}
56+
57+
```
58+
59+
# A PyFixest Regression Table
60+
As @tbl-main shows, LaTex Tables generated by PyFixest can be easily integrated into Quarto document when rendered as pdf.
61+
62+
```{python}
63+
#| label: tbl-main
64+
#| tbl-cap: A PyFixest Regression Table
65+
#| output: asis
66+
#| echo: false
67+
#| tbl-pos: H
68+
69+
mynotes="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
70+
71+
tab=pf.etable([fit1, fit2, fit4, fit5],
72+
labels=labels, notes=mynotes, type="tex",
73+
model_heads=["US", "China", "US", "China"])
74+
75+
print(tab)
76+
```
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"---\n",
8+
"title: \"Quarto and PyFixest\"\n",
9+
"jupyter: python3\n",
10+
"format:\n",
11+
" pdf:\n",
12+
" number-sections: true\n",
13+
" include-in-header:\n",
14+
" - text: |\n",
15+
" \\usepackage{booktabs}\n",
16+
" \\usepackage{makecell}\n",
17+
" \\usepackage{threeparttable}\n",
18+
"execute:\n",
19+
" echo: false\n",
20+
" warning: false\n",
21+
"author:\n",
22+
"- Peter Pan^[University of Neverland]\n",
23+
"date: last-modified\n",
24+
"abstract: |\n",
25+
" \\\n",
26+
" \\\n",
27+
" We study the effect of X1 and X2 on Y and Y2 using PyFixest. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\n",
28+
" \\\n",
29+
" \\\n",
30+
" **Keywords**: Regressions, PyFixest, Tables, Quarto\n",
31+
"---\n",
32+
"\n",
33+
"{{< pagebreak >}}\n",
34+
"\n",
35+
"\n",
36+
"# Introduction\n",
37+
"\n",
38+
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\"\n"
39+
],
40+
"id": "78e2a9c8"
41+
},
42+
{
43+
"cell_type": "code",
44+
"metadata": {},
45+
"source": [
46+
"#| echo: false\n",
47+
"#| output: false\n",
48+
"import pandas as pd\n",
49+
"import pyfixest as pf\n",
50+
"\n",
51+
"data = pf.get_data()\n",
52+
"\n",
53+
"fit1 = pf.feols(\"Y ~ X1 + X2 | f1\", data = data)\n",
54+
"fit2 = pf.feols(\"Y ~ X1 + X2 | f1 + f2\", data = data)\n",
55+
"fit3 = pf.feols(\"Y ~ X1 *X2 | f1 + f2\", data = data)\n",
56+
"fit4 = pf.feols(\"Y2 ~ X1 + X2 | f1\", data = data)\n",
57+
"fit5 = pf.feols(\"Y2 ~ X1 + X2 | f1 + f2\", data = data)\n",
58+
"fit6 = pf.feols(\"Y2 ~ X1 *X2 | f1 + f2\", data = data)\n",
59+
"\n",
60+
"labels={\n",
61+
" \"Y\": \"Wage\",\n",
62+
" \"Y2\": \"Wealth\",\n",
63+
" \"X1\": \"Age\",\n",
64+
" \"X2\": \"Years of Schooling\",\n",
65+
" \"f1\": \"Industry\",\n",
66+
" \"f2\": \"Year\"\n",
67+
"}"
68+
],
69+
"id": "c444d358",
70+
"execution_count": null,
71+
"outputs": []
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"# A PyFixest Regression Table\n",
78+
"As @tbl-main shows, LaTex Tables generated by PyFixest can be easily integrated into Quarto document when rendered as pdf.\n"
79+
],
80+
"id": "4fc14b82"
81+
},
82+
{
83+
"cell_type": "code",
84+
"metadata": {
85+
"tbl-pos": "H"
86+
},
87+
"source": [
88+
"#| label: tbl-main\n",
89+
"#| tbl-cap: A PyFixest Regression Table\n",
90+
"#| output: asis\n",
91+
"#| echo: false\n",
92+
"\n",
93+
"mynotes=\"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\"\n",
94+
"\n",
95+
"tab=pf.etable([fit1, fit2, fit4, fit5],\n",
96+
" labels=labels, notes=mynotes, type=\"tex\",\n",
97+
" model_heads=[\"US\", \"China\", \"US\", \"China\"])\n",
98+
"\n",
99+
"print(tab)"
100+
],
101+
"id": "tbl-main",
102+
"execution_count": null,
103+
"outputs": []
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"name": "python3",
109+
"language": "python",
110+
"display_name": "Python 3 (ipykernel)",
111+
"path": "C:\\Users\\dirks\\AppData\\Roaming\\Python\\share\\jupyter\\kernels\\python3"
112+
}
113+
},
114+
"nbformat": 4,
115+
"nbformat_minor": 5
116+
}

0 commit comments

Comments
 (0)