Skip to content
/ coeftable Public

Two command-line tools to generate arbitrarily formatted tables from JSON data files.

License

Notifications You must be signed in to change notification settings

gn0/coeftable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

coeftable

This Python package provides three command-line tools to generate arbitrarily formatted tables from JSON data files. The tools are:

  • coeftable: takes a template and a list of JSON data files, and generates the output.
  • csv2textemplate: takes a csv file, potentially with template shorthands, and converts it into a tex-formatted template for coeftable to use.
  • makecttemplate (beta): takes a set of parameters, and generates a template for coeftable to use.

The JSON data files can contain regression results or results from hypothesis tests. If you are using Stata, the json_this package can save your results in JSON for you.

Installation

To install this package using pip, type

pip install git+https://github.com/gn0/coeftable

or, alternatively,

git clone https://github.com/gn0/coeftable
pip install ./coeftable

Usage

Example 1: ASCII regression table

This example demonstrates how to use coeftable to assemble an ASCII regression table. Let the template be stored in table.tmpl:

+---+-----------+-----------+
|   |    (1)    |    (2)    |
+---+-----------+-----------+
| x |  %(1::coef::x::est)6.3f   |  %(2::coef::x::est)6.3f   |
|   | (%(1::coef::x::se)7.4f) | (%(2::coef::x::se)7.4f) |
| z |           |  %(2::coef::z::est)6.3f   |
|   |           | (%(2::coef::z::se)7.4f) |
+---+-----------+-----------+
| N | %(1::N)6d    | %(2::N)6d    |
+---+-----------+-----------+

Estimates from the two specifications are stored in model_1.json and model_2.json:

{"N": 276, "coef": {"x": {"est": 0.3886779, "se": 0.1054072, "stars": "***"}}}
{"N": 276, "coef": {"x": {"est": 0.4606558, "se": 0.0722247, "stars": "***"}, "z": {"est": -0.0450573, "se": 0.0584647, "stars": ""}}}

Then the output can be generated by running:

cat table.tmpl | coeftable --models model_1.json model_2.json

which prints:

+---+-----------+-----------+
|   |    (1)    |    (2)    |
+---+-----------+-----------+
| x |   0.389   |   0.461   |
|   | ( 0.1054) | ( 0.0722) |
| z |           |  -0.045   |
|   |           | ( 0.0585) |
+---+-----------+-----------+
| N |    276    |    276    |
+---+-----------+-----------+

Example 2: plaintext summary

Let the template be stored in summary.tmpl:

Estimating the model without z, the coefficient on x is %(1::coef::x::est).3f.
When we control for z, the coefficient on x is %(2::coef::x::est).3f.

The output can be generated by running:

cat summary.tmpl | coeftable --models model_1.json model_2.json

This generates:

Estimating the model without z, the coefficient on x is 0.389.
When we control for z, the coefficient on x is 0.461.

Example 3: csv regression table

Let the template be stored in csv_table.tmpl:

,(1),(2)
x,%(1::coef::x::est).3f,%(2::coef::x::est).3f
,(%(1::coef::x::se).4f),(%(2::coef::x::se).4f)
z,,%(2::coef::z::est).3f
,,(%(2::coef::z::se).4f)
N,%(1::N)d,%(2::N)d

As before, the output can be generated by:

cat csv_table.tmpl | coeftable --models model_1.json model_2.json

This yields:

,(1),(2)
x,0.389,0.461
,(0.1054),(0.0722)
z,,-0.045
,,(0.0585)
N,276,276

Example 4: tex regression table

Managing tables in tex is a pain. It is more efficient to edit table layouts in a spreadsheet software and save them as csv.

csv2textemplate takes such a csv-formatted template as input and generates the tex equivalent. Take the template from the previous example, add some tex markup so that minus signs would be properly rendered, add stars to indicate significance, and store it in tex_table.csv:

#: \toprule
,(1),(2)
#: \midrule
x,$%(1::coef::x::est).3f$%(1::coef::x::stars)s,$%(2::coef::x::est).3f$%(2::coef::x::stars)s
,($%(1::coef::x::se).4f$),($%(2::coef::x::se).4f$) \\[1em]
z,,$%(2::coef::z::est).3f$%(2::coef::z::stars)s
,,($%(2::coef::z::se).4f$) \\[1em]
N,%(1::N)d,%(2::N)d
#: \bottomrule

Feed it to csv2textemplate to see what it looks in tex:

\toprule
 & (1) & (2) \\
\midrule
x & $%(1::coef::x::est).3f$%(1::coef::x::stars)s & $%(2::coef::x::est).3f$%(2::coef::x::stars)s \\
 & ($%(1::coef::x::se).4f$) & ($%(2::coef::x::se).4f$) \\[1em]
z &  & $%(2::coef::z::est).3f$%(2::coef::z::stars)s \\
 &  & ($%(2::coef::z::se).4f$) \\[1em]
N & %(1::N)d & %(2::N)d \\
\bottomrule

So to generate the output as a tex table, we can run:

cat tex_table.csv | csv2textemplate | coeftable --models model_1.json model_2.json

This yields:

\toprule
 & (1) & (2) \\
\midrule
x & $0.389$*** & $0.461$*** \\
 & ($0.1054$) & ($0.0722$) \\[1em]
z &  & $-0.045$ \\
 &  & ($0.0585$) \\[1em]
N & 276 & 276 \\
\bottomrule

Example 5: tex regression table with shorthands

csv2textemplate allows for shorthands so that standard errors don't have to be explicitly added. The template from the previous example, in shorter form:

#: \toprule
,(1),(2)
#: \midrule
x,@@1::x@@,@@2::x@@
z,,@@2::z@@
N,%(1::N)d,%(2::N)d
#: \bottomrule

Feeding it to csv2textemplate, we see how the shorthand forms are expanded:

\toprule
 & (1) & (2) \\
\midrule
x & $%(1::coef::x::est).3f$%(1::coef::x::stars)s & $%(2::coef::x::est).3f$%(2::coef::x::stars)s \\
 & ($%(1::coef::x::se).4f$) & ($%(2::coef::x::se).4f$) \\[1em]
z &  & $%(2::coef::z::est).3f$%(2::coef::z::stars)s \\
 &  & ($%(2::coef::z::se).4f$) \\[1em]
N & %(1::N)d & %(2::N)d \\
\bottomrule

We can generate the output as before.

Author

Gabor Nyeki. Contact information is on http://www.gabornyeki.com/.

License

This package is licensed under the Creative Commons Attribution 4.0 International License: http://creativecommons.org/licenses/by/4.0/.

About

Two command-line tools to generate arbitrarily formatted tables from JSON data files.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages