Skip to content

Commit c41a7e8

Browse files
Show R^2 and F-statistic for regression models
1 parent 4af81b9 commit c41a7e8

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name = "StatsModels"
22
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
3-
version = "0.6.22"
3+
version = "0.6.23"
44

55
[deps]
66
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
8+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1011
ShiftedArrays = "1277b4bf-5013-50f5-be3d-901d8477a67a"
@@ -18,6 +19,7 @@ CategoricalArrays = "0.8"
1819
DataAPI = "1.1"
1920
DataFrames = "0.21"
2021
DataStructures = "0.17.0, 0.18"
22+
Distributions = "0.21, 0.22, 0.23, 0.24"
2123
ShiftedArrays = "1.0.0"
2224
StatsBase = "0.33.5"
2325
StatsFuns = "0.9"

src/StatsModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using ShiftedArrays: lag, lead
77
using DataStructures
88
using DataAPI
99
using DataAPI: levels
10+
using Distributions
1011
using Printf: @sprintf
1112
using StatsFuns: chisqccdf
1213

src/statsmodel.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const TableModels = Union{TableStatisticalModel, TableRegressionModel}
122122
StatsBase.stderror, StatsBase.vcov]
123123
@delegate TableRegressionModel.model [StatsBase.modelmatrix,
124124
StatsBase.residuals, StatsBase.response,
125-
StatsBase.predict, StatsBase.predict!,
125+
StatsBase.predict, StatsBase.predict!,
126126
StatsBase.cooksdistance]
127127
StatsBase.predict(m::TableRegressionModel, new_x::AbstractMatrix; kwargs...) =
128128
predict(m.model, new_x; kwargs...)
@@ -189,7 +189,12 @@ function Base.show(io::IO, model::TableModels)
189189
println(io, model.mf.f)
190190
println(io)
191191
println(io,"Coefficients:")
192-
show(io, ct)
192+
println(io, ct)
193+
if model isa TableRegressionModel
194+
println(io)
195+
println("R²: ", round(r2(model.model), sigdigits=4))
196+
println(io, fstatistic(model))
197+
end
193198
catch e
194199
if isa(e, ErrorException) && occursin("coeftable is not defined", e.msg)
195200
show(io, model.model)
@@ -198,3 +203,28 @@ function Base.show(io::IO, model::TableModels)
198203
end
199204
end
200205
end
206+
207+
struct FStatistic
208+
nobs::Int64
209+
ndims::Int64
210+
f::Float64
211+
fprob::Float64
212+
end
213+
214+
function fstatistic(obj::TableRegressionModel)
215+
rss = deviance(obj)
216+
tss = nulldeviance(obj)
217+
218+
n = nobs(obj)
219+
p = length(coef(obj)) - 1 # minus intercept
220+
fstat = ((tss - rss) / p) / (rss / (n - p - 1))
221+
fdist = FDist(n, p)
222+
223+
FStatistic(n, p, fstat, ccdf.(fdist, abs(fstat)))
224+
end
225+
226+
function show(io::IO, fstat::FStatistic)
227+
print(io, "F-statistic: ", round(fstat.f, sigdigits=4), " ")
228+
print(io, "on ", fstat.ndims, " and ", fstat.nobs, " degrees of freedom, ")
229+
print(io, "p-value: ", round(fstat.fprob, sigdigits=4))
230+
end

0 commit comments

Comments
 (0)