Skip to content

Commit

Permalink
Merge pull request #32 from jmboehm/label-bug
Browse files Browse the repository at this point in the history
Label bug
  • Loading branch information
jmboehm committed Sep 25, 2019
2 parents 2faed06 + ea47534 commit ca8a37d
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[TimeZones]]
deps = ["Dates", "EzXML", "Mocking", "Printf", "Serialization", "Unicode"]
git-tree-sha1 = "0271dc890dd1447da1568510937bd5b3a6b51b58"
git-tree-sha1 = "2769a7ceffe237e651ed960a331a19258c8cf208"
uuid = "f269a46b-ccf7-5d73-abea-4c690281aa53"
version = "0.9.2"
version = "0.10.0"

[[TranscodingStreams]]
deps = ["Random", "Test"]
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RegressionTables"
uuid = "d519eb52-b820-54da-95a6-98e1306fdade"
authors = ["Johannes Boehm <johannes.boehm@gmail.com>"]
version = "0.2.2"
version = "0.2.3"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
8 changes: 4 additions & 4 deletions src/regtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Produces a publication-quality regression table, similar to Stata's `esttab` and
* `rr::FixedEffectModel...` are the `FixedEffectModel`s from `FixedEffectModels.jl` that should be printed. Only required argument.
* `regressors` is a `Vector` of regressor names (`String`s) that should be shown, in that order. Defaults to an empty vector, in which case all regressors will be shown.
* `fixedeffects` is a `Vector` of FE names (`String`s) that should be shown, in that order. Defaults to an empty vector, in which case all FE's will be shown.
* `labels` is a `Dict` that 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.
* `labels` is a `Dict` that contains displayed labels for variables (`String`s) and other text in the table. If no label for a variable is found, it default to variable names. See documentation for special values.
* `estimformat` is a `String` that describes the format of the estimate. Defaults to "%0.3f".
* `estim_decoration` is a `Function` that takes the formatted string and the p-value, and applies decorations (such as the beloved stars). Defaults to (* p<0.05, ** p<0.01, *** p<0.001).
* `statisticformat` is a `String` that describes the format of the number below the estimate (se/t). Defaults to "%0.4f".
* `statisticformat` is a `String` that describes the format of the number below the estimate (se/t). Defaults to "%0.3f".
* `below_statistic` is a `Symbol` that describes a statistic that should be shown below each point estimate. Recognized values are `:blank`, `:se`, and `:tstat`. Defaults to `:se`.
* `below_decoration` is a `Function` that takes the formatted statistic string, and applies a decorations. Defaults to round parentheses.
* `regression_statistics` is a `Vector` of `Symbol`s that describe statistics to be shown at the bottom of the table. Recognized symbols are `:nobs`, `:r2`, `:adjr2`, `:r2_within`, `:f`, `:p`, `:f_kp`, `:p_kp`, and `:dof`. Defaults to `[:nobs, :r2]`.
Expand Down Expand Up @@ -54,7 +54,7 @@ regtable(rr1,rr2,rr3,rr4; renderSettings = asciiOutput(), regressors = ["SepalLe
# format of the estimates
regtable(rr1,rr2,rr3,rr4; renderSettings = asciiOutput(), estimformat = "%02.5f")
# replace some variable names by other strings
regtable(rr1,rr2,rr3; renderSettings = asciiOutput(), labels = Dict("SepalLength" => "My dependent variable: SepalLength", "PetalLength" => "Length of Petal", "PetalWidth" => "Width of Petal", "(Intercept)" => "Const." , "isSmall" => "isSmall Dummies", "SpeciesDummy" => "Species Dummies"))
regtable(rr1,rr2,rr3; renderSettings = asciiOutput(), labels = Dict(:SepalLength => "My dependent variable: SepalLength", :PetalLength => "Length of Petal", :PetalWidth => "Width of Petal", Symbol("(Intercept)") => "Const." , :isSmall => "isSmall Dummies", :SpeciesDummy => "Species Dummies"))
# do not print the FE block
regtable(rr1,rr2,rr3,rr4; renderSettings = asciiOutput(), print_fe_section = false)
# re-order fixed effects
Expand All @@ -72,7 +72,7 @@ regtable(rr1,rr2,rr3,rr4; renderSettings = latexOutput("myoutfile.tex"))
function regtable(rr::Union{FixedEffectModel,TableRegressionModel}...;
regressors::Vector{String} = Vector{String}(),
fixedeffects::Vector{String} = Vector{String}(),
labels::Dict{Symbol,String} = Dict{Symbol,String}(),
labels::Dict{String,String} = Dict{String,String}(),
estimformat::String = "%0.3f",
estim_decoration::Function = default_ascii_estim_decoration,
statisticformat::String = "%0.3f",
Expand Down
59 changes: 59 additions & 0 deletions test/RegressionTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ end
# RegressionTables.regtable(rr1,rr2,rr3,rr5; renderSettings = RegressionTables.htmlOutput(), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
# RegressionTables.regtable(lm1, lm2, gm1; renderSettings = RegressionTables.htmlOutput(), regression_statistics = [:nobs, :r2])
# --------------

# RegressionTables.regtable(rr1,rr2,rr3,rr5; renderSettings = RegressionTables.asciiOutput("test1.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
# RegressionTables.regtable(rr1,rr2,rr3,rr5,rr6; renderSettings = RegressionTables.asciiOutput("test7.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
# RegressionTables.regtable(lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("test3.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
Expand All @@ -107,6 +108,64 @@ end
# RegressionTables.regtable(rr1,rr2,rr3,rr5; renderSettings = RegressionTables.htmlOutput("test1.html"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
# RegressionTables.regtable(lm1, lm2, gm1; renderSettings = RegressionTables.htmlOutput("test2.html"), regression_statistics = [:nobs, :r2])

# include("src/RegressionTables.jl")
# RegressionTables.regtable(rr1,rr2,rr3,rr5; renderSettings = RegressionTables.asciiOutput(), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof], labels = Dict("SepalWidth" => "The Sepal Width"))
# RegressionTables.regtable(rr1,rr2,rr3; renderSettings = RegressionTables.asciiOutput(), labels = Dict("SepalLength" => "My dependent variable: SepalLength", "PetalLength" => "Length of Petal", "PetalWidth" => "Width of Petal", "(Intercept)" => "Const." , "isSmall" => "isSmall Dummies", "SpeciesDummy" => "Species Dummies"))

# new tests: all features
# RegressionTables.regtable(rr4,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("ftest1.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
# # regressors and labels
# RegressionTables.regtable(rr4,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("ftest2.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof], regressors = ["SepalLength", "PetalWidth"])
# # fixedeffects, estimformat, statisticformat, number_regressions_decoration
# RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("ftest3.txt"), fixedeffects = ["SpeciesDummy"], estimformat = "%0.4f", statisticformat = "%0.4f", number_regressions_decoration = i -> "[$i]")
# # estim_decoration, below_statistic, below_decoration, number_regressions
# function dec(s::String, pval::Float64)
# if pval<0.0
# error("p value needs to be nonnegative.")
# end
# if (pval > 0.05)
# return "$s"
# elseif (pval <= 0.05)
# return "$s<-OMG!"
# end
# end
# RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("ftest4.txt"), estim_decoration = dec, below_statistic = :tstat, below_decoration = s -> "[$s]", number_regressions = false)
# # print_fe_section, print_estimator_section
# RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput("ftest5.txt"), print_fe_section = false, print_estimator_section = false)
# # transform_labels and custom labels
# RegressionTables.regtable(rr5,rr6,lm1, lm2, lm3; renderSettings = RegressionTables.asciiOutput("ftest6.txt"), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof], transform_labels = RegressionTables.escape_ampersand,
# labels = Dict("SepalLength" => "My dependent variable: SepalLength", "PetalLength" => "Length of Petal", "PetalWidth" => "Width of Petal", "(Intercept)" => "Const." , "isSmall" => "isSmall Dummies", "SpeciesDummy" => "Species Dummies"))

RegressionTables.regtable(rr4,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest1.txt")), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest1.txt"), joinpath(dirname(@__FILE__), "tables", "ftest1_reference.txt"))
# regressors and labels
RegressionTables.regtable(rr4,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest2.txt")), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof], regressors = ["SepalLength", "PetalWidth"])
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest2.txt"), joinpath(dirname(@__FILE__), "tables", "ftest2_reference.txt"))
# fixedeffects, estimformat, statisticformat, number_regressions_decoration
RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest3.txt")), fixedeffects = ["SpeciesDummy"], estimformat = "%0.4f", statisticformat = "%0.4f", number_regressions_decoration = i -> "[$i]")
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest3.txt"), joinpath(dirname(@__FILE__), "tables", "ftest3_reference.txt"))
# estim_decoration, below_statistic, below_decoration, number_regressions
function dec(s::String, pval::Float64)
if pval<0.0
error("p value needs to be nonnegative.")
end
if (pval > 0.05)
return "$s"
elseif (pval <= 0.05)
return "$s<-OMG!"
end
end
RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest4.txt")), estim_decoration = dec, below_statistic = :tstat, below_decoration = s -> "[$s]", number_regressions = false)
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest4.txt"), joinpath(dirname(@__FILE__), "tables", "ftest4_reference.txt"))
# print_fe_section, print_estimator_section
RegressionTables.regtable(rr3,rr5,lm1, lm2, gm1; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest5.txt")), print_fe_section = false, print_estimator_section = false)
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest5.txt"), joinpath(dirname(@__FILE__), "tables", "ftest5_reference.txt"))
# transform_labels and custom labels
RegressionTables.regtable(rr5,rr6,lm1, lm2, lm3; renderSettings = RegressionTables.asciiOutput(joinpath(dirname(@__FILE__), "tables", "ftest6.txt")), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof], transform_labels = RegressionTables.escape_ampersand,
labels = Dict("SepalLength" => "My dependent variable: SepalLength", "PetalLength" => "Length of Petal", "PetalWidth" => "Width of Petal", "(Intercept)" => "Const." , "isSmall" => "isSmall Dummies", "SpeciesDummy" => "Species Dummies"))
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "ftest6.txt"), joinpath(dirname(@__FILE__), "tables", "ftest6_reference.txt"))


regtable(rr1,rr2,rr3,rr5; renderSettings = asciiOutput(joinpath(dirname(@__FILE__), "tables", "test1.txt")), regression_statistics = [:nobs, :r2, :adjr2, :r2_within, :f, :p, :f_kp, :p_kp, :dof])
@test checkfilesarethesame(joinpath(dirname(@__FILE__), "tables", "test1.txt"), joinpath(dirname(@__FILE__), "tables", "test1_reference.txt"))

Expand Down
36 changes: 36 additions & 0 deletions test/tables/ftest1_reference.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

-----------------------------------------------------------------------------
SepalWidth SepalLength Counts
------------------ ------------------- --------
(1) (2) (3) (4) (5)
-----------------------------------------------------------------------------
SepalLength 0.378*** -0.313
(0.066) (0.239)
PetalLength -0.188* 1.048**
(0.083) (0.362)
PetalWidth 0.626*** 0.972***
(0.123) (0.052)
(Intercept) 6.526*** 3.457*** 3.045***
(0.479) (0.309) (0.126)
SepalWidth -0.223 0.399***
(0.155) (0.091)
Outcome: B -0.454
(0.202)
Outcome: C -0.293
(0.193)
-----------------------------------------------------------------------------
SpeciesDummy Yes Yes
-----------------------------------------------------------------------------
Estimator OLS IV OLS OLS NL
-----------------------------------------------------------------------------
N 150 150 150 150 9
R2 0.635 0.080 0.014 0.707
Adjusted R2 0.622 0.055
Within-R2 0.391 -0.535
F 30.836 17.468
F-test p value 0.000 0.000
First-stage F statistic 19.962
First-stage p value 0.000
Degrees of Freedom 144 145
-----------------------------------------------------------------------------

26 changes: 26 additions & 0 deletions test/tables/ftest2_reference.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

------------------------------------------------------------------------
SepalWidth SepalLength Counts
------------------ ---------------- ------
(1) (2) (3) (4) (5)
------------------------------------------------------------------------
SepalLength 0.378*** -0.313
(0.066) (0.239)
PetalWidth 0.626*** 0.972***
(0.123) (0.052)
------------------------------------------------------------------------
SpeciesDummy Yes Yes
------------------------------------------------------------------------
Estimator OLS IV OLS OLS NL
------------------------------------------------------------------------
N 150 150 150 150 9
R2 0.635 0.080 0.014 0.707
Adjusted R2 0.622 0.055
Within-R2 0.391 -0.535
F 30.836 17.468
F-test p value 0.000 0.000
First-stage F statistic 19.962
First-stage p value 0.000
Degrees of Freedom 144 145
------------------------------------------------------------------------

29 changes: 29 additions & 0 deletions test/tables/ftest3_reference.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

---------------------------------------------------------------------------
SepalLength SepalWidth SepalLength Counts
----------- ---------- --------------------- ---------
[1] [2] [3] [4] [5]
---------------------------------------------------------------------------
SepalWidth 0.5059*** -0.2234 0.3991***
(0.1031) (0.1551) (0.0911)
PetalLength 0.8300*** 1.0480**
(0.0691) (0.3621)
PetalWidth -0.3115* 0.9721***
(0.1536) (0.0521)
SepalLength -0.3126
(0.2388)
(Intercept) 6.5262*** 3.4573*** 3.0445***
(0.4789) (0.3092) (0.1260)
Outcome: B -0.4543
(0.2022)
Outcome: C -0.2930
(0.1927)
---------------------------------------------------------------------------
SpeciesDummy Yes Yes
---------------------------------------------------------------------------
Estimator OLS IV OLS OLS NL
---------------------------------------------------------------------------
N 150 150 150 150 9
R2 0.8673 0.0803 0.0138 0.7072
---------------------------------------------------------------------------

Loading

0 comments on commit ca8a37d

Please sign in to comment.