From 70c61d1c8904378d94cdefbeb50fe903bff9f42b Mon Sep 17 00:00:00 2001 From: Ondrej Slamecka Date: Sun, 25 Apr 2021 12:15:00 +0100 Subject: [PATCH 1/4] Show R^2 for regression models --- Project.toml | 2 +- src/StatsModels.jl | 2 +- src/statsmodel.jl | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 95fdbda8..556892fd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "StatsModels" uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d" -version = "0.6.29" +version = "0.6.30" [deps] DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" diff --git a/src/StatsModels.jl b/src/StatsModels.jl index 6555ec23..cafe8e61 100644 --- a/src/StatsModels.jl +++ b/src/StatsModels.jl @@ -33,7 +33,7 @@ export SeqDiffCoding, HypothesisCoding, ContrastsCoding, - + coefnames, setcontrasts!, formula, diff --git a/src/statsmodel.jl b/src/statsmodel.jl index 0bb67c7c..002827ad 100644 --- a/src/statsmodel.jl +++ b/src/statsmodel.jl @@ -193,6 +193,12 @@ function StatsBase.coeftable(model::TableModels; kwargs...) ct end +_show_fit_stats(io::IO, model::TableModels) = nothing + +function _show_fit_stats(io::IO, model::TableRegressionModel) + println("R²: ", round(r2(model), sigdigits=4)) +end + # show function that delegates to coeftable function Base.show(io::IO, model::TableModels) println(io, typeof(model)) @@ -202,6 +208,7 @@ function Base.show(io::IO, model::TableModels) try println(io,"Coefficients:") show(io, coeftable(model)) + _show_fit_stats(io, model) catch e if isa(e, MethodError) || isa(e, ErrorException) && occursin("coeftable is not defined", e.msg) show(io, model.model) From 13c5d9b8b6fb89f478ab8788eba5dae90a13f5eb Mon Sep 17 00:00:00 2001 From: Ondrej Slamecka Date: Sun, 25 Apr 2021 14:11:11 +0100 Subject: [PATCH 2/4] Show F-statistic for regression models Co-authored-by: Phillip Alday --- src/statsmodel.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/statsmodel.jl b/src/statsmodel.jl index 002827ad..7f2a7fec 100644 --- a/src/statsmodel.jl +++ b/src/statsmodel.jl @@ -133,7 +133,7 @@ const TableModels = Union{TableStatisticalModel, TableRegressionModel} @delegate TableRegressionModel.model [StatsBase.modelmatrix, StatsBase.residuals, StatsBase.response, StatsBase.predict, StatsBase.predict!, - StatsBase.cooksdistance] + StatsBase.cooksdistance, fstatistic] StatsBase.predict(m::TableRegressionModel, new_x::AbstractMatrix; kwargs...) = predict(m.model, new_x; kwargs...) # Need to define these manually because of ambiguity using @delegate @@ -197,6 +197,14 @@ _show_fit_stats(io::IO, model::TableModels) = nothing function _show_fit_stats(io::IO, model::TableRegressionModel) println("R²: ", round(r2(model), sigdigits=4)) + try + fstat = fstatistic(model) + println(io, fstat) + catch e + if !(isa(e, MethodError) && e.f == fstatistic) + rethrow(e) + end + end end # show function that delegates to coeftable From 659dc51c8be5a0036233578a48e6471c989a4019 Mon Sep 17 00:00:00 2001 From: Ondrej Slamecka Date: Tue, 27 Apr 2021 00:14:48 +0100 Subject: [PATCH 3/4] Add adjusted R^2 --- src/statsmodel.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/statsmodel.jl b/src/statsmodel.jl index 7f2a7fec..65531ed0 100644 --- a/src/statsmodel.jl +++ b/src/statsmodel.jl @@ -196,7 +196,8 @@ end _show_fit_stats(io::IO, model::TableModels) = nothing function _show_fit_stats(io::IO, model::TableRegressionModel) - println("R²: ", round(r2(model), sigdigits=4)) + println("R²: ", round(r2(model), sigdigits=4), + "\t Adjusted R²: ", round(adjr2(model), sigdigits=4)) try fstat = fstatistic(model) println(io, fstat) From 88490cf708afc074d995171895632d42c21dbd55 Mon Sep 17 00:00:00 2001 From: Ondrej Slamecka Date: Tue, 26 Apr 2022 11:04:45 +0100 Subject: [PATCH 4/4] try..catch all of _show_fit_stats --- src/statsmodel.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/statsmodel.jl b/src/statsmodel.jl index 65531ed0..a229a451 100644 --- a/src/statsmodel.jl +++ b/src/statsmodel.jl @@ -196,13 +196,14 @@ end _show_fit_stats(io::IO, model::TableModels) = nothing function _show_fit_stats(io::IO, model::TableRegressionModel) - println("R²: ", round(r2(model), sigdigits=4), - "\t Adjusted R²: ", round(adjr2(model), sigdigits=4)) try + println("R²: ", round(r2(model), sigdigits=4), + "\t Adjusted R²: ", round(adjr2(model), sigdigits=4)) + fstat = fstatistic(model) println(io, fstat) catch e - if !(isa(e, MethodError) && e.f == fstatistic) + if !(isa(e, MethodError) && (e.f == r2 || e.f == adjr2 || e.f == fstatistic)) rethrow(e) end end