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

add hasintercept #17

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions src/regressionmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ Return the model matrix (a.k.a. the design matrix).
"""
function modelmatrix end

"""
hasintercept(model::RegressionModel)

Indicate whether the model has an intercept.
"""
function hasintercept(model::RegressionModel)
X = modelmatrix(model)
any(i -> all(==(1), view(X , :, i)), 1:size(X, 2))
end


"""
crossmodelmatrix(model::RegressionModel)

Expand Down
7 changes: 4 additions & 3 deletions test/regressionmodel.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestRegressionModel

using Test, LinearAlgebra, StatsAPI
using StatsAPI: RegressionModel, crossmodelmatrix
using StatsAPI: RegressionModel, hasintercept, crossmodelmatrix

struct MyRegressionModel <: RegressionModel
end
Expand All @@ -10,9 +10,10 @@ StatsAPI.modelmatrix(::MyRegressionModel) = [1 2; 3 4]

@testset "TestRegressionModel" begin
m = MyRegressionModel()


@test !hasintercept(m)
@test crossmodelmatrix(m) == [10 14; 14 20]
@test crossmodelmatrix(m) isa Symmetric
end

end # module TestRegressionModel
end # module TestRegressionModel
Loading