lattab 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lattab-0.2.0/.github/workflows/tests.yml +37 -0
- lattab-0.2.0/.gitignore +4 -0
- lattab-0.2.0/LICENSE +21 -0
- lattab-0.2.0/PKG-INFO +264 -0
- lattab-0.2.0/README.md +228 -0
- lattab-0.2.0/README.pdf +0 -0
- lattab-0.2.0/README.tex +250 -0
- lattab-0.2.0/examples/multiple_databases.py +78 -0
- lattab-0.2.0/examples/multiple_databases.tex +37 -0
- lattab-0.2.0/lattab/README.md +50 -0
- lattab-0.2.0/lattab/__init__.py +44 -0
- lattab-0.2.0/lattab/core.py +181 -0
- lattab-0.2.0/lattab/estimation.py +246 -0
- lattab-0.2.0/lattab/regression.py +575 -0
- lattab-0.2.0/lattab/samples.py +44 -0
- lattab-0.2.0/lattab/styles.py +44 -0
- lattab-0.2.0/lattab-onboarding.bundle +0 -0
- lattab-0.2.0/pyproject.toml +41 -0
- lattab-0.2.0/tests/test_absorbed_iv.py +70 -0
- lattab-0.2.0/tests/test_common_sample.py +29 -0
- lattab-0.2.0/tests/test_econometrics.py +249 -0
- lattab-0.2.0/tests/test_lattab.py +107 -0
- lattab-0.2.0/tests/test_layout.py +127 -0
- lattab-0.2.0/tests/test_marginal_effects.py +65 -0
- lattab-0.2.0/tests/test_model_coverage.py +91 -0
- lattab-0.2.0/tests/test_regression_tables.py +241 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
name: tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["**"]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
include:
|
|
15
|
+
# ubuntu-latest (26.04) no longer ships Python 3.9 builds.
|
|
16
|
+
- python-version: "3.9"
|
|
17
|
+
os: ubuntu-24.04
|
|
18
|
+
- python-version: "3.10"
|
|
19
|
+
os: ubuntu-latest
|
|
20
|
+
- python-version: "3.11"
|
|
21
|
+
os: ubuntu-latest
|
|
22
|
+
- python-version: "3.12"
|
|
23
|
+
os: ubuntu-latest
|
|
24
|
+
- python-version: "3.13"
|
|
25
|
+
os: ubuntu-latest
|
|
26
|
+
steps:
|
|
27
|
+
- uses: actions/checkout@v4
|
|
28
|
+
- uses: actions/setup-python@v5
|
|
29
|
+
with:
|
|
30
|
+
python-version: ${{ matrix.python-version }}
|
|
31
|
+
- name: Install package with test dependencies
|
|
32
|
+
run: |
|
|
33
|
+
python -m pip install --upgrade pip
|
|
34
|
+
pip install -e ".[test]"
|
|
35
|
+
- name: Run test suite
|
|
36
|
+
# LaTeX compilation cases skip themselves when pdflatex is absent.
|
|
37
|
+
run: PYTHONPATH=. PYTHONDONTWRITEBYTECODE=1 pytest -q -ra -p no:cacheprovider tests/
|
lattab-0.2.0/.gitignore
ADDED
lattab-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jianqi Huang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
lattab-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lattab
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Convert DataFrames and regression results to LaTeX tables in multiple formats
|
|
5
|
+
Project-URL: Repository, https://github.com/jianqih/lattab
|
|
6
|
+
Project-URL: Issues, https://github.com/jianqih/lattab/issues
|
|
7
|
+
Author-email: Jianqi Huang <jianqi_huang@outlook.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: econometrics,latex,linearmodels,regression,statsmodels,tables
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Requires-Dist: pandas
|
|
23
|
+
Provides-Extra: econometrics
|
|
24
|
+
Requires-Dist: linearmodels>=6.1; extra == 'econometrics'
|
|
25
|
+
Requires-Dist: pyhdfe>=0.1; extra == 'econometrics'
|
|
26
|
+
Requires-Dist: scipy>=1.7; extra == 'econometrics'
|
|
27
|
+
Provides-Extra: statsmodels
|
|
28
|
+
Requires-Dist: statsmodels; extra == 'statsmodels'
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: linearmodels>=6.1; extra == 'test'
|
|
31
|
+
Requires-Dist: pyhdfe>=0.1; extra == 'test'
|
|
32
|
+
Requires-Dist: pytest; extra == 'test'
|
|
33
|
+
Requires-Dist: scipy>=1.7; extra == 'test'
|
|
34
|
+
Requires-Dist: statsmodels; extra == 'test'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# lattab
|
|
38
|
+
|
|
39
|
+
Fit OLS and instrumental-variable regressions with fixed effects, and export
|
|
40
|
+
DataFrames or fitted models to LaTeX.
|
|
41
|
+
|
|
42
|
+
[User guide (PDF)](README.pdf) · [LaTeX source](README.tex)
|
|
43
|
+
|
|
44
|
+
The base package requires pandas. Model fitting uses the optional
|
|
45
|
+
`econometrics` extra. The table exporter also accepts fitted statsmodels and
|
|
46
|
+
linearmodels results directly.
|
|
47
|
+
|
|
48
|
+
Tested result types include OLS, WLS, GLS, binary Logit, Probit, MNLogit,
|
|
49
|
+
GLM, IV2SLS and PanelOLS. GLM columns name the family in the estimator row,
|
|
50
|
+
e.g. `GLM (Poisson)`; a requested statistic that the backend does not carry
|
|
51
|
+
(such as Pseudo $R^2$ for GLM) prints blank. Pass a fitted statsmodels MNLogit
|
|
52
|
+
result to `regression_table([result])`.
|
|
53
|
+
Each non-reference category gets a column. The Outcome row names the comparison
|
|
54
|
+
and its reference category. When combining models, supply one `model_names`
|
|
55
|
+
entry and one cell in each `extra_rows` row per input model. Model-level entries
|
|
56
|
+
and summary statistics repeat across that model's outcome columns. Custom rows accept
|
|
57
|
+
plain text such as `R&D` and inline LaTeX math between dollar signs.
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
python -m pip install -e . # table exporters
|
|
63
|
+
python -m pip install -e ".[econometrics]" # include regression fitting
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Styles
|
|
67
|
+
|
|
68
|
+
For general-purpose regression tables, use `style="double-outer"`.
|
|
69
|
+
Both exporters accept the same styles. The default is `"hline-hline"`.
|
|
70
|
+
|
|
71
|
+
| name | rules |
|
|
72
|
+
|---|---|
|
|
73
|
+
| `"double-outer"` | `\hline\hline` at the top and bottom; single `\hline` inside |
|
|
74
|
+
| `"hline-hline"` (default) | `\hline\hline` at every separator |
|
|
75
|
+
| `"booktabs"` | `\toprule` / `\midrule` / `\bottomrule` |
|
|
76
|
+
| `"hline"` | single `\hline` |
|
|
77
|
+
| custom | pass your own `RuleStyle(name, top, mid, bottom)` |
|
|
78
|
+
|
|
79
|
+
## Usage
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from lattab import to_latex, regression_table
|
|
83
|
+
|
|
84
|
+
# DataFrame -> table
|
|
85
|
+
to_latex(df, caption="Summary statistics", label="tab:sum",
|
|
86
|
+
style="booktabs", digits=3, rename={"old": "New"})
|
|
87
|
+
# missing cells (NaN/None/pd.NA) print blank by default
|
|
88
|
+
to_latex(df_with_gaps, na_rep="--")
|
|
89
|
+
|
|
90
|
+
# Regression results -> table
|
|
91
|
+
tex = regression_table(
|
|
92
|
+
[m1, m2], # statsmodels results objects
|
|
93
|
+
model_names=[r"$\Delta$ UR", "Model 2"],
|
|
94
|
+
caption="Main results",
|
|
95
|
+
label="tab:reg",
|
|
96
|
+
style="double-outer",
|
|
97
|
+
rename={"d_fedfunds": r"$\Delta$ FFR"},
|
|
98
|
+
notes=r"Standard errors in parentheses. *** $p<0.01$.",
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Inline math in labels, headers, and notes is preserved automatically: text
|
|
103
|
+
inside `$...$` spans is passed through verbatim, everything else is escaped.
|
|
104
|
+
|
|
105
|
+
## Compare OLS, IV and fixed effects
|
|
106
|
+
|
|
107
|
+
This example estimates three specifications on the same Card wage observations.
|
|
108
|
+
The two IV columns instrument years of schooling with proximity to a four-year
|
|
109
|
+
college. The last column adds separate effects for metropolitan and Southern
|
|
110
|
+
residence in 1966.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from linearmodels.datasets import card
|
|
114
|
+
from lattab import fit_regression, regression_table
|
|
115
|
+
|
|
116
|
+
wages = card.load()
|
|
117
|
+
controls = ["exper", "expersq"]
|
|
118
|
+
iv = dict(endogenous="educ", instruments="nearc4")
|
|
119
|
+
ols = fit_regression(wages, "lwage", ["educ", *controls])
|
|
120
|
+
tsls = fit_regression(wages, "lwage", controls, **iv)
|
|
121
|
+
tsls_fe = fit_regression(wages, "lwage", controls, fixed_effects=["smsa66", "south66"], **iv)
|
|
122
|
+
|
|
123
|
+
tex = regression_table(
|
|
124
|
+
[ols, tsls, tsls_fe], model_names=["OLS", "IV", "IV with FE"],
|
|
125
|
+
keep=["educ"], rename={"educ": "Schooling"}, show_first_stage=True,
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Each fit uses observations complete for its own specification, including its
|
|
130
|
+
cluster variables. The default is HC0. Pass `clusters="firm"` or
|
|
131
|
+
`clusters=["firm", "year"]` for one- or two-way clustering. OLS absorbs the
|
|
132
|
+
fixed effects. IV uses explicit indicators by default. For many effect levels,
|
|
133
|
+
set `absorb_iv=True` to residualize the outcome, controls, endogenous variables
|
|
134
|
+
and instruments together before 2SLS. The absorbed fit reports slopes and keeps
|
|
135
|
+
singletons. Its native result uses within-transformed data. The table reports
|
|
136
|
+
full and within R-squared separately. Use `fit.first_stage_diagnostics` for
|
|
137
|
+
FE-adjusted diagnostics. With unadjusted covariance and three or more varying
|
|
138
|
+
effects, the first-stage test reports an asymptotic Wald chi-square. The native result, including IV first-stage
|
|
139
|
+
regressions, is available as `tsls.result`.
|
|
140
|
+
|
|
141
|
+
To compare specifications on the same observations, pass their column names
|
|
142
|
+
to `fit_regressions` on a single DataFrame:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from lattab import fit_regressions
|
|
146
|
+
|
|
147
|
+
specs = [
|
|
148
|
+
dict(dependent="lwage", exog=["educ", "exper", "expersq"]),
|
|
149
|
+
dict(dependent="lwage", exog=["exper", "expersq"],
|
|
150
|
+
endogenous="educ", instruments="nearc4"),
|
|
151
|
+
]
|
|
152
|
+
ols, iv = fit_regressions(wages, specs, common_sample=True)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The common sample keeps rows complete for every specification, including its
|
|
156
|
+
instruments, fixed effects and cluster identifiers. The default uses each
|
|
157
|
+
model's own complete observations.
|
|
158
|
+
|
|
159
|
+
Detailed tables label the outcome and estimator rows `Dep. var.` and `Est.`.
|
|
160
|
+
Override them with `rename={"Dependent variable": "Outcome", "Estimator": "Method"}`.
|
|
161
|
+
Use `rename={"Observations": "Obs.", "$R^2$": "R-sq."}` for regression
|
|
162
|
+
summary rows. For a DataFrame summary, use
|
|
163
|
+
`to_latex(df.describe(), index=True, rename={"count": "Obs."})`.
|
|
164
|
+
|
|
165
|
+
Choose the summary rows in display order, rename them, and set their precision independently of the coefficients. For example:
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
tex = regression_table(
|
|
169
|
+
[ols, tsls, tsls_fe], inference="ci",
|
|
170
|
+
summary_stats=["Observations", "Clusters", "Within $R^2$", "$R^2$"],
|
|
171
|
+
rename={"Observations": "Obs."}, stat_digits={"$R^2$": 2},
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The default `inference="se"` prints standard errors in parentheses. `"ci"` prints the fitted backend's 95% intervals in brackets. Use `summary_stats=[]` to omit the summary block. A requested statistic is blank where unavailable. `"Clusters"` reports counts recorded by `fit_regression`, in the order of its cluster dimensions. `"Within $R^2$"` uses PanelOLS's entity-within measure or AbsorbingLS's measure after removing all absorbed effects. `"$R^2$"` includes effects when the backend supplies an inclusive measure. `"Pseudo $R^2$"` uses the fitted result's pseudo R-squared.
|
|
176
|
+
|
|
177
|
+
Pass one label per fitted model with `column_groups=["Card", "Card", "Mroz"]` to group adjacent columns. MNLogit model headings span their outcome comparisons automatically. Use `control_groups={"Experience controls": ["exper", "expersq"]}` to summarize fitted terms, including terms hidden by `keep`. A partially included group prints `Partial`; full and absent groups follow `fe_style`.
|
|
178
|
+
|
|
179
|
+
Tables align coefficients by name and leave absent estimates blank. The fixed
|
|
180
|
+
effect rows name each dimension. Use `fe_style="checkmark"` for a tick on
|
|
181
|
+
included effects and a blank elsewhere, or `fe_style="yes_no"` for the default
|
|
182
|
+
text labels. The checkmark requires `\usepackage{amssymb}` in the paper preamble. `show_first_stage=True` adds the partial
|
|
183
|
+
R-squared and joint test of the excluded instruments for each endogenous
|
|
184
|
+
variable. The table prints the fitted test's reference distribution, including
|
|
185
|
+
the robust Wald chi-square. [First-stage diagnostics](https://bashtage.github.io/linearmodels/iv/iv/linearmodels.iv.results.FirstStageResults.diagnostics.html)
|
|
186
|
+
|
|
187
|
+
The model R-squared includes fixed effects. In the Card IV column it is
|
|
188
|
+
negative because the structural residual sum of squares exceeds the centered
|
|
189
|
+
total sum of squares.
|
|
190
|
+
|
|
191
|
+
### Report average marginal effects
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
tex = regression_table([binary_logit, multinomial_logit],
|
|
195
|
+
marginal_effects=True, inference="ci")
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Pass fitted statsmodels Logit, Probit or MNLogit results. Coefficients become
|
|
199
|
+
sample-average derivatives of predicted probabilities, with standard errors or
|
|
200
|
+
95% intervals from `get_margeff(at="overall", method="dydx")`. A binary outcome
|
|
201
|
+
has one event column. A multinomial outcome has a column per category, including
|
|
202
|
+
the reference. Binary regressors use continuous derivatives under this convention.
|
|
203
|
+
|
|
204
|
+
## Publication layout
|
|
205
|
+
|
|
206
|
+
### Align numbers on the decimal point
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
tex = regression_table([m1, m2, m3], siunitx=True)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`siunitx=True` typesets model columns as `S` columns so every coefficient,
|
|
213
|
+
standard error and statistic aligns on the decimal marker. The column format
|
|
214
|
+
is sized to the widest number actually displayed. Stars ride behind the
|
|
215
|
+
coefficient; standard errors keep their parentheses and stay aligned with the
|
|
216
|
+
coefficient above. The output requires `\usepackage{siunitx}` and begins with a
|
|
217
|
+
comment reminding you of it.
|
|
218
|
+
|
|
219
|
+
### Fit wide and long tables
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
tex = regression_table([m1, m2, m3, m4, m5, m6], resize=True) # shrink to \linewidth
|
|
223
|
+
tex = regression_table(long_output, longtable=True) # break across pages
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
`resize=True` wraps the tabular in `\resizebox{\linewidth}{!}{...}`
|
|
227
|
+
(requires `\usepackage{graphicx}`). `longtable=True` emits a `longtable` that
|
|
228
|
+
repeats the header on every page and carries the caption inside the
|
|
229
|
+
environment (requires `\usepackage{longtable}`); the `table` float wrapper and
|
|
230
|
+
`position` are ignored. The two options are mutually exclusive.
|
|
231
|
+
|
|
232
|
+
## Reproduce the five-column table
|
|
233
|
+
|
|
234
|
+
The example creates three separate temporary SQLite databases from datasets
|
|
235
|
+
included with the Python packages, reads each sample with SQL, and fits the
|
|
236
|
+
five columns below. It runs offline after installation. The wage models include
|
|
237
|
+
experience and its square. The Grunfeld columns of the source data measure
|
|
238
|
+
investment, firm value and capital at 1947 prices.
|
|
239
|
+
|
|
240
|
+
| Database | Specifications | Estimation sample |
|
|
241
|
+
| --- | --- | ---: |
|
|
242
|
+
| Card | OLS, IV, IV with two location effects | 3,010 per column |
|
|
243
|
+
| Mroz | IV with mother's and father's schooling as excluded instruments | 428 observed log wages among 753 records |
|
|
244
|
+
| Grunfeld | Firm and year effects, standard errors clustered by firm | 220 firm-years, 11 firms |
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
python -m examples.multiple_databases > comparison.tex
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
The column and fixed-effect layout follows Julia's RegressionTables.jl.
|
|
251
|
+
[Example script](examples/multiple_databases.py) · [Generated LaTeX table](examples/multiple_databases.tex) · [Dataset examples](https://bashtage.github.io/linearmodels/iv/examples/basic-examples.html) · [Grunfeld data](https://www.statsmodels.org/stable/datasets/generated/grunfeld.html) · [RegressionTables.jl](https://github.com/jmboehm/RegressionTables.jl)
|
|
252
|
+
|
|
253
|
+
## Tests
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
python -m pip install -e ".[test]"
|
|
257
|
+
python -m pytest tests/
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
GitHub Actions runs the suite on Python 3.9 through 3.13.
|
|
261
|
+
|
|
262
|
+
## License
|
|
263
|
+
|
|
264
|
+
MIT. See [LICENSE](LICENSE).
|
lattab-0.2.0/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# lattab
|
|
2
|
+
|
|
3
|
+
Fit OLS and instrumental-variable regressions with fixed effects, and export
|
|
4
|
+
DataFrames or fitted models to LaTeX.
|
|
5
|
+
|
|
6
|
+
[User guide (PDF)](README.pdf) · [LaTeX source](README.tex)
|
|
7
|
+
|
|
8
|
+
The base package requires pandas. Model fitting uses the optional
|
|
9
|
+
`econometrics` extra. The table exporter also accepts fitted statsmodels and
|
|
10
|
+
linearmodels results directly.
|
|
11
|
+
|
|
12
|
+
Tested result types include OLS, WLS, GLS, binary Logit, Probit, MNLogit,
|
|
13
|
+
GLM, IV2SLS and PanelOLS. GLM columns name the family in the estimator row,
|
|
14
|
+
e.g. `GLM (Poisson)`; a requested statistic that the backend does not carry
|
|
15
|
+
(such as Pseudo $R^2$ for GLM) prints blank. Pass a fitted statsmodels MNLogit
|
|
16
|
+
result to `regression_table([result])`.
|
|
17
|
+
Each non-reference category gets a column. The Outcome row names the comparison
|
|
18
|
+
and its reference category. When combining models, supply one `model_names`
|
|
19
|
+
entry and one cell in each `extra_rows` row per input model. Model-level entries
|
|
20
|
+
and summary statistics repeat across that model's outcome columns. Custom rows accept
|
|
21
|
+
plain text such as `R&D` and inline LaTeX math between dollar signs.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
python -m pip install -e . # table exporters
|
|
27
|
+
python -m pip install -e ".[econometrics]" # include regression fitting
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Styles
|
|
31
|
+
|
|
32
|
+
For general-purpose regression tables, use `style="double-outer"`.
|
|
33
|
+
Both exporters accept the same styles. The default is `"hline-hline"`.
|
|
34
|
+
|
|
35
|
+
| name | rules |
|
|
36
|
+
|---|---|
|
|
37
|
+
| `"double-outer"` | `\hline\hline` at the top and bottom; single `\hline` inside |
|
|
38
|
+
| `"hline-hline"` (default) | `\hline\hline` at every separator |
|
|
39
|
+
| `"booktabs"` | `\toprule` / `\midrule` / `\bottomrule` |
|
|
40
|
+
| `"hline"` | single `\hline` |
|
|
41
|
+
| custom | pass your own `RuleStyle(name, top, mid, bottom)` |
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from lattab import to_latex, regression_table
|
|
47
|
+
|
|
48
|
+
# DataFrame -> table
|
|
49
|
+
to_latex(df, caption="Summary statistics", label="tab:sum",
|
|
50
|
+
style="booktabs", digits=3, rename={"old": "New"})
|
|
51
|
+
# missing cells (NaN/None/pd.NA) print blank by default
|
|
52
|
+
to_latex(df_with_gaps, na_rep="--")
|
|
53
|
+
|
|
54
|
+
# Regression results -> table
|
|
55
|
+
tex = regression_table(
|
|
56
|
+
[m1, m2], # statsmodels results objects
|
|
57
|
+
model_names=[r"$\Delta$ UR", "Model 2"],
|
|
58
|
+
caption="Main results",
|
|
59
|
+
label="tab:reg",
|
|
60
|
+
style="double-outer",
|
|
61
|
+
rename={"d_fedfunds": r"$\Delta$ FFR"},
|
|
62
|
+
notes=r"Standard errors in parentheses. *** $p<0.01$.",
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Inline math in labels, headers, and notes is preserved automatically: text
|
|
67
|
+
inside `$...$` spans is passed through verbatim, everything else is escaped.
|
|
68
|
+
|
|
69
|
+
## Compare OLS, IV and fixed effects
|
|
70
|
+
|
|
71
|
+
This example estimates three specifications on the same Card wage observations.
|
|
72
|
+
The two IV columns instrument years of schooling with proximity to a four-year
|
|
73
|
+
college. The last column adds separate effects for metropolitan and Southern
|
|
74
|
+
residence in 1966.
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from linearmodels.datasets import card
|
|
78
|
+
from lattab import fit_regression, regression_table
|
|
79
|
+
|
|
80
|
+
wages = card.load()
|
|
81
|
+
controls = ["exper", "expersq"]
|
|
82
|
+
iv = dict(endogenous="educ", instruments="nearc4")
|
|
83
|
+
ols = fit_regression(wages, "lwage", ["educ", *controls])
|
|
84
|
+
tsls = fit_regression(wages, "lwage", controls, **iv)
|
|
85
|
+
tsls_fe = fit_regression(wages, "lwage", controls, fixed_effects=["smsa66", "south66"], **iv)
|
|
86
|
+
|
|
87
|
+
tex = regression_table(
|
|
88
|
+
[ols, tsls, tsls_fe], model_names=["OLS", "IV", "IV with FE"],
|
|
89
|
+
keep=["educ"], rename={"educ": "Schooling"}, show_first_stage=True,
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Each fit uses observations complete for its own specification, including its
|
|
94
|
+
cluster variables. The default is HC0. Pass `clusters="firm"` or
|
|
95
|
+
`clusters=["firm", "year"]` for one- or two-way clustering. OLS absorbs the
|
|
96
|
+
fixed effects. IV uses explicit indicators by default. For many effect levels,
|
|
97
|
+
set `absorb_iv=True` to residualize the outcome, controls, endogenous variables
|
|
98
|
+
and instruments together before 2SLS. The absorbed fit reports slopes and keeps
|
|
99
|
+
singletons. Its native result uses within-transformed data. The table reports
|
|
100
|
+
full and within R-squared separately. Use `fit.first_stage_diagnostics` for
|
|
101
|
+
FE-adjusted diagnostics. With unadjusted covariance and three or more varying
|
|
102
|
+
effects, the first-stage test reports an asymptotic Wald chi-square. The native result, including IV first-stage
|
|
103
|
+
regressions, is available as `tsls.result`.
|
|
104
|
+
|
|
105
|
+
To compare specifications on the same observations, pass their column names
|
|
106
|
+
to `fit_regressions` on a single DataFrame:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from lattab import fit_regressions
|
|
110
|
+
|
|
111
|
+
specs = [
|
|
112
|
+
dict(dependent="lwage", exog=["educ", "exper", "expersq"]),
|
|
113
|
+
dict(dependent="lwage", exog=["exper", "expersq"],
|
|
114
|
+
endogenous="educ", instruments="nearc4"),
|
|
115
|
+
]
|
|
116
|
+
ols, iv = fit_regressions(wages, specs, common_sample=True)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The common sample keeps rows complete for every specification, including its
|
|
120
|
+
instruments, fixed effects and cluster identifiers. The default uses each
|
|
121
|
+
model's own complete observations.
|
|
122
|
+
|
|
123
|
+
Detailed tables label the outcome and estimator rows `Dep. var.` and `Est.`.
|
|
124
|
+
Override them with `rename={"Dependent variable": "Outcome", "Estimator": "Method"}`.
|
|
125
|
+
Use `rename={"Observations": "Obs.", "$R^2$": "R-sq."}` for regression
|
|
126
|
+
summary rows. For a DataFrame summary, use
|
|
127
|
+
`to_latex(df.describe(), index=True, rename={"count": "Obs."})`.
|
|
128
|
+
|
|
129
|
+
Choose the summary rows in display order, rename them, and set their precision independently of the coefficients. For example:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
tex = regression_table(
|
|
133
|
+
[ols, tsls, tsls_fe], inference="ci",
|
|
134
|
+
summary_stats=["Observations", "Clusters", "Within $R^2$", "$R^2$"],
|
|
135
|
+
rename={"Observations": "Obs."}, stat_digits={"$R^2$": 2},
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The default `inference="se"` prints standard errors in parentheses. `"ci"` prints the fitted backend's 95% intervals in brackets. Use `summary_stats=[]` to omit the summary block. A requested statistic is blank where unavailable. `"Clusters"` reports counts recorded by `fit_regression`, in the order of its cluster dimensions. `"Within $R^2$"` uses PanelOLS's entity-within measure or AbsorbingLS's measure after removing all absorbed effects. `"$R^2$"` includes effects when the backend supplies an inclusive measure. `"Pseudo $R^2$"` uses the fitted result's pseudo R-squared.
|
|
140
|
+
|
|
141
|
+
Pass one label per fitted model with `column_groups=["Card", "Card", "Mroz"]` to group adjacent columns. MNLogit model headings span their outcome comparisons automatically. Use `control_groups={"Experience controls": ["exper", "expersq"]}` to summarize fitted terms, including terms hidden by `keep`. A partially included group prints `Partial`; full and absent groups follow `fe_style`.
|
|
142
|
+
|
|
143
|
+
Tables align coefficients by name and leave absent estimates blank. The fixed
|
|
144
|
+
effect rows name each dimension. Use `fe_style="checkmark"` for a tick on
|
|
145
|
+
included effects and a blank elsewhere, or `fe_style="yes_no"` for the default
|
|
146
|
+
text labels. The checkmark requires `\usepackage{amssymb}` in the paper preamble. `show_first_stage=True` adds the partial
|
|
147
|
+
R-squared and joint test of the excluded instruments for each endogenous
|
|
148
|
+
variable. The table prints the fitted test's reference distribution, including
|
|
149
|
+
the robust Wald chi-square. [First-stage diagnostics](https://bashtage.github.io/linearmodels/iv/iv/linearmodels.iv.results.FirstStageResults.diagnostics.html)
|
|
150
|
+
|
|
151
|
+
The model R-squared includes fixed effects. In the Card IV column it is
|
|
152
|
+
negative because the structural residual sum of squares exceeds the centered
|
|
153
|
+
total sum of squares.
|
|
154
|
+
|
|
155
|
+
### Report average marginal effects
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
tex = regression_table([binary_logit, multinomial_logit],
|
|
159
|
+
marginal_effects=True, inference="ci")
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Pass fitted statsmodels Logit, Probit or MNLogit results. Coefficients become
|
|
163
|
+
sample-average derivatives of predicted probabilities, with standard errors or
|
|
164
|
+
95% intervals from `get_margeff(at="overall", method="dydx")`. A binary outcome
|
|
165
|
+
has one event column. A multinomial outcome has a column per category, including
|
|
166
|
+
the reference. Binary regressors use continuous derivatives under this convention.
|
|
167
|
+
|
|
168
|
+
## Publication layout
|
|
169
|
+
|
|
170
|
+
### Align numbers on the decimal point
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
tex = regression_table([m1, m2, m3], siunitx=True)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
`siunitx=True` typesets model columns as `S` columns so every coefficient,
|
|
177
|
+
standard error and statistic aligns on the decimal marker. The column format
|
|
178
|
+
is sized to the widest number actually displayed. Stars ride behind the
|
|
179
|
+
coefficient; standard errors keep their parentheses and stay aligned with the
|
|
180
|
+
coefficient above. The output requires `\usepackage{siunitx}` and begins with a
|
|
181
|
+
comment reminding you of it.
|
|
182
|
+
|
|
183
|
+
### Fit wide and long tables
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
tex = regression_table([m1, m2, m3, m4, m5, m6], resize=True) # shrink to \linewidth
|
|
187
|
+
tex = regression_table(long_output, longtable=True) # break across pages
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`resize=True` wraps the tabular in `\resizebox{\linewidth}{!}{...}`
|
|
191
|
+
(requires `\usepackage{graphicx}`). `longtable=True` emits a `longtable` that
|
|
192
|
+
repeats the header on every page and carries the caption inside the
|
|
193
|
+
environment (requires `\usepackage{longtable}`); the `table` float wrapper and
|
|
194
|
+
`position` are ignored. The two options are mutually exclusive.
|
|
195
|
+
|
|
196
|
+
## Reproduce the five-column table
|
|
197
|
+
|
|
198
|
+
The example creates three separate temporary SQLite databases from datasets
|
|
199
|
+
included with the Python packages, reads each sample with SQL, and fits the
|
|
200
|
+
five columns below. It runs offline after installation. The wage models include
|
|
201
|
+
experience and its square. The Grunfeld columns of the source data measure
|
|
202
|
+
investment, firm value and capital at 1947 prices.
|
|
203
|
+
|
|
204
|
+
| Database | Specifications | Estimation sample |
|
|
205
|
+
| --- | --- | ---: |
|
|
206
|
+
| Card | OLS, IV, IV with two location effects | 3,010 per column |
|
|
207
|
+
| Mroz | IV with mother's and father's schooling as excluded instruments | 428 observed log wages among 753 records |
|
|
208
|
+
| Grunfeld | Firm and year effects, standard errors clustered by firm | 220 firm-years, 11 firms |
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
python -m examples.multiple_databases > comparison.tex
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
The column and fixed-effect layout follows Julia's RegressionTables.jl.
|
|
215
|
+
[Example script](examples/multiple_databases.py) · [Generated LaTeX table](examples/multiple_databases.tex) · [Dataset examples](https://bashtage.github.io/linearmodels/iv/examples/basic-examples.html) · [Grunfeld data](https://www.statsmodels.org/stable/datasets/generated/grunfeld.html) · [RegressionTables.jl](https://github.com/jmboehm/RegressionTables.jl)
|
|
216
|
+
|
|
217
|
+
## Tests
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
python -m pip install -e ".[test]"
|
|
221
|
+
python -m pytest tests/
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
GitHub Actions runs the suite on Python 3.9 through 3.13.
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
MIT. See [LICENSE](LICENSE).
|
lattab-0.2.0/README.pdf
ADDED
|
Binary file
|