RegressionTables.jl
This package provides publication-quality regression tables for use with FixedEffectModels.jl, GLM.jl, GLFixedEffectModels.jl and MixedModels.jl, as well as any package that implements the RegressionModel abstraction.
In its objective it is similar to (and heavily inspired by) the Stata command esttab and the R package stargazer.
Table of Contents
- API of Underlying Functions
- Using RegressionTables with a Custom Model
- Customization of Defaults
- Examples
- Default
- Below Statistics
- Standard Errors on same line as coefficient
- Keep, drop and order
- Formatting Estimates, Statistics and decimal points
- Labeling Coefficients
- Grouping Regressions
- Do not print $X block
- Re-order Fixed Effects
- Change Labels for Regression Statistics
- Adding an extra row between coefficients
- All Available Statistics
- LaTeX Output
- Extralines
- Do Not Print Fixed Effect Suffix
- Standardize Coefficients
- Show Clustered Standard Errors
- MixedModels Support
- Typst Support
- Function Reference
- RegressionTables.jl
- Keep Drop and Order Arguments
- Regression Statistics
Installation
To install the package, type in the Julia command prompt
] add RegressionTablesA brief demonstration
using RegressionTables, DataFrames, FixedEffectModels, RDatasets, GLM
df = RDatasets.dataset("datasets", "iris")
rr1 = reg(df, @formula(SepalLength ~ SepalWidth + fe(Species)))
rr2 = reg(df, @formula(SepalLength ~ SepalWidth + PetalLength + fe(Species)))
rr3 = reg(df, @formula(SepalLength ~ SepalWidth * PetalLength + PetalWidth + fe(Species)))
rr4 = reg(df, @formula(SepalWidth ~ SepalLength + PetalLength + PetalWidth + fe(Species)))
rr5 = glm(@formula(SepalWidth < 2.9 ~ PetalLength + PetalWidth + Species), df, Binomial())
regtable(
rr1,rr2,rr3,rr4,rr5;
render = AsciiTable(),
labels = Dict(
"versicolor" => "Versicolor",
"virginica" => "Virginica",
"PetalLength" => "Petal Length",
),
regression_statistics = [
Nobs => "Obs.",
R2,
R2Within,
PseudoR2 => "Pseudo-R2",
],
extralines = [
["Main Coefficient", "SepalWidth", "SepalWidth", "Petal Length", "Petal Length", "Intercept"],
DataRow(["Coef Diff", 0.372 => 2:3, 1.235 => 3:4, ""], align="lccr")
],
order = [r"Int", r" & ", r": "]
)
----------------------------------------------------------------------------------------------------
SepalLength SepalWidth SepalWidth < 2.9
-------------------------------------- ------------ ----------------
(1) (2) (3) (4) (5)
----------------------------------------------------------------------------------------------------
(Intercept) -1.917
(1.242)
SepalWidth & Petal Length -0.070
(0.041)
Species: Versicolor 10.441***
(1.957)
Species: Virginica 13.230***
(2.636)
SepalWidth 0.804*** 0.432*** 0.719***
(0.106) (0.081) (0.155)
Petal Length 0.776*** 1.047*** -0.188* -0.773
(0.064) (0.143) (0.083) (0.554)
PetalWidth -0.259 0.626*** -3.782**
(0.154) (0.123) (1.256)
SepalLength 0.378***
(0.066)
----------------------------------------------------------------------------------------------------
Species Fixed Effects Yes Yes Yes Yes
----------------------------------------------------------------------------------------------------
Estimator OLS OLS OLS OLS Binomial
----------------------------------------------------------------------------------------------------
Obs. 150 150 150 150 150
R2 0.726 0.863 0.870 0.635
Within-R2 0.281 0.642 0.659 0.391
Pseudo-R2 0.527 0.811 0.831 0.862 0.347
Main Coefficient SepalWidth SepalWidth Petal Length Petal Length Intercept
Coef Diff 0.372 1.235
----------------------------------------------------------------------------------------------------
LaTeX output can be generated by using
regtable(rr1,rr2,rr3,rr4; render = LatexTable())\begin{tabular}{lrrrr}
\toprule
& \multicolumn{3}{c}{SepalLength} & \multicolumn{1}{c}{SepalWidth} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-5}
& (1) & (2) & (3) & (4) \\
\midrule
SepalWidth & 0.804*** & 0.432*** & 0.719*** & \\
& (0.106) & (0.081) & (0.155) & \\
PetalLength & & 0.776*** & 1.047*** & -0.188* \\
& & (0.064) & (0.143) & (0.083) \\
PetalWidth & & & -0.259 & 0.626*** \\
& & & (0.154) & (0.123) \\
SepalWidth $\times$ PetalLength & & & -0.070 & \\
& & & (0.041) & \\
SepalLength & & & & 0.378*** \\
& & & & (0.066) \\
\midrule
Species Fixed Effects & Yes & Yes & Yes & Yes \\
\midrule
$N$ & 150 & 150 & 150 & 150 \\
$R^2$ & 0.726 & 0.863 & 0.870 & 0.635 \\
Within-$R^2$ & 0.281 & 0.642 & 0.659 & 0.391 \\
\bottomrule
\end{tabular}
Similarly, HTML tables can be created with HtmlTable().
Send the output to a text file by passing the destination file as a keyword argument:
regtable(rr1,rr2,rr3,rr4; render = LatexTable(), file="myoutputfile.tex")then use \input in LaTeX to include that file in your code. Be sure to use the booktabs package:
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}
\label{tab:mytable}
\input{myoutputfile}
\end{table}
\end{document}regtable() can also print TableRegressionModel's from GLM.jl (and output from other packages that produce TableRegressionModel's):
using GLM
dobson = DataFrame(Counts = [18.,17,15,20,10,20,25,13,12],
Outcome = repeat(["A", "B", "C"], outer = 3),
Treatment = repeat(["a","b", "c"], inner = 3))
rr1 = fit(LinearModel, @formula(SepalLength ~ SepalWidth), df)
lm1 = fit(LinearModel, @formula(SepalLength ~ SepalWidth), df)
gm1 = fit(GeneralizedLinearModel, @formula(Counts ~ 1 + Outcome + Treatment), dobson,
Poisson())
regtable(rr1,lm1,gm1; renderSettings = AsciiTable())
---------------------------------------------
SepalLength Counts
------------------- --------
(1) (2) (3)
---------------------------------------------
(Intercept) 6.526*** 6.526*** 3.045***
(0.479) (0.479) (0.171)
SepalWidth -0.223 -0.223
(0.155) (0.155)
Outcome: B -0.454
(0.202)
Outcome: C -0.293
(0.193)
Treatment: b 0.000
(0.200)
Treatment: c -0.000
(0.200)
---------------------------------------------
Estimator OLS OLS Poisson
---------------------------------------------
N 150 150 9
R2 0.014 0.014
Pseudo R2 0.006 0.006 0.104
---------------------------------------------
Printing of StatsBase.RegressionModels (e.g., MixedModels.jl and GLFixedEffectModels.jl) generally works but are less well tested; please file as issue if you encounter problems printing them.
Main regtable Function
RegressionTables.regtable — FunctionProduces a publication-quality regression table, similar to Stata's esttab and R's stargazer.
Arguments
rr::FixedEffectModel...are theFixedEffectModels fromFixedEffectModels.jlthat should be printed. Only required argument.keepis aVectorof regressor names (Strings), integers, ranges or regex that should be shown, in that order. Defaults to an empty vector, in which case all regressors will be shown. The strings should be the relabeld names of the coefficients.dropis aVectorof regressor names (Strings), integers, ranges or regex that should not be shown. Defaults to an empty vector, in which case no regressors will be dropped. The strings should be the relabeld names of the coefficients.orderis aVectorof regressor names (Strings), integers, ranges or regex that should be shown in that order. Defaults to an empty vector, in which case the order of regressors will be unchanged. Other regressors are still shown (assumingdropis empty). The strings should be the relabeld names of the coefficients.fixedeffectsis aVectorof FE names (Strings), integers, ranges or regex that should be shown, in that order. Defaults to an empty vector, in which case all FE's will be shown.alignis aSymbolfrom the set[:l,:c,:r]indicating the alignment of results columns (default:rright-aligned). Currently works only with ASCII and LaTeX output.header_alignis aSymbolfrom the set[:l,:c,:r]indicating the alignment of the header row (default:ccentered). Currently works only with ASCII and LaTeX output.labelsis aDictthat contains displayed labels for variables (Strings) and other text in the table. If no label for a variable is found, it default to variable names. See documentation for special values.estimformatis aStringthat describes the format of the estimate.digitsis anIntthat describes the precision to be shown in the estimate. Defaults tonothing, which means the default (3) is used (default can be changed by settingRegressionTables.default_digits(render::AbstractRenderType, x) = 3).statisticformatis aStringthat describes the format of the number below the estimate (se/t).digits_statsis anIntthat describes the precision to be shown in the statistics. Defaults tonothing, which means the default (3) is used (default can be changed by settingRegressionTables.default_digits(render::AbstractRenderType, x) = 3).below_statisticis a type that describes a statistic that should be shown below each point estimate. Recognized values arenothing,StdError,TStat, andConfInt.nothingsuppresses the line. Defaults toStdError.regression_statisticsis aVectorof types that describe statistics to be shown at the bottom of the table. Built in recognized types areNobs,R2,PseudoR2,R2CoxSnell,R2Nagelkerke,R2Deviance,AdjR2,AdjPseudoR2,AdjR2Deviance,DOF,LogLikelihood,AIC,AICC,BIC,FStat,FStatPValue,FStatIV,FStatIVPValue, R2Within. Defaults vary based on regression inputs (simple linear model is [Nobs, R2]).extralinesis aVectoror aVector{<:AbsractVector}that will be added to the end of the table. A single vector will be its own row, a vector of vectors will each be a row. Defaults tonothing.number_regressionsis aBoolthat governs whether regressions should be numbered. Defaults totrue.groupsis aVector,Vector{<:AbstractVector}orMatrixof labels used to group regressions. This can be useful if results are shown for different data sets or sample restrictions.print_fe_sectionis aBoolthat governs whether a section on fixed effects should be shown. Defaults totrue.print_estimator_sectionis aBoolthat governs whether to print a section on which estimator (OLS/IV/Binomial/Poisson...) is used. Defaults totrueif more than one value is displayed.standardize_coefis aBoolthat governs whether the table should show standardized coefficients. Note that this only works withTableRegressionModels, and that only coefficient estimates and thebelow_statisticare being standardized (i.e. the R^2 etc still pertain to the non-standardized regression).render::AbstractRenderTypeis aAbstractRenderTypetype that governs how the table should be rendered. Standard supported types are ASCII (viaAsciiTable()) and LaTeX (viaLatexTable()). Defaults toAsciiTable().fileis aStringthat governs whether the table should be saved to a file. Defaults tonothing.transform_labelsis aDictor one of theSymbols:ampersand,:underscore,:underscore2space,:latexconfint_levelis aFloat64that governs the confidence level for the confidence interval. Defaults to0.95.
Details
A typical use is to pass a number of FixedEffectModels to the function, along with how it should be rendered (with render argument):
regtable(regressionResult1, regressionResult2; render = AsciiTable())Pass a string to the file argument to create or overwrite a file. For example, using LaTeX output,
regtable(regressionResult1, regressionResult2; render = LatexTable(), file="myoutfile.tex")See the full argument list for details.