Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show R^2 and F-statistic for regression models #225

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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"
Expand Down
2 changes: 1 addition & 1 deletion src/StatsModels.jl
Expand Up @@ -33,7 +33,7 @@ export
SeqDiffCoding,
HypothesisCoding,
ContrastsCoding,

coefnames,
setcontrasts!,
formula,
Expand Down
19 changes: 18 additions & 1 deletion src/statsmodel.jl
Expand Up @@ -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
Expand Down Expand Up @@ -193,6 +193,22 @@ function StatsBase.coeftable(model::TableModels; kwargs...)
ct
end

_show_fit_stats(io::IO, model::TableModels) = nothing

function _show_fit_stats(io::IO, model::TableRegressionModel)
try
println("R²: ", round(r2(model), sigdigits=4),
"\t Adjusted R²: ", round(adjr2(model), sigdigits=4))
Comment on lines +200 to +201
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ensure that 4 digits are always used, and that "Ajusted" is aligned with the value of the F-statistic on the next line.

Suggested change
println("R²: ", round(r2(model), sigdigits=4),
"\t Adjusted R²: ", round(adjr2(model), sigdigits=4))
@printf(io, "R²: %.4f Adjusted R²: %.4f", r2(model), adjr2(model))


fstat = fstatistic(model)
println(io, fstat)
Comment on lines +203 to +204
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fstatistic doesn't exist anywhere currently, and only GLM provides the needed functionality via ftest (JuliaStats/GLM.jl#424). An empty ftest definition should be added to StatsAPI first. Probably the API can just require that the returned object has the dof, fstat and pval properties so that you can extract them and print them.

Suggested change
fstat = fstatistic(model)
println(io, fstat)
ftr = ftest(model)
@printf(io, "F-statistic: %.4f on %s degrees of freedom, p-value %s",
ftr.fstat, ftr.dof, PValue(ftr.pvalue))

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nalimilan what if adding a print API to StatsAPI so packages can implement their own printing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xgdgsc every package is free to implement Base.show methods for their own types

catch e
if !(isa(e, MethodError) && (e.f == r2 || e.f == adjr2 || e.f == fstatistic))
rethrow(e)
end
end
end

# show function that delegates to coeftable
function Base.show(io::IO, model::TableModels)
println(io, typeof(model))
Expand All @@ -202,6 +218,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)
Expand Down