Skip to content

Commit 6680786

Browse files
authored
Add tests for statistical models (#11)
Cover functions for which we provide a fallback definition. Also turn `ExceptionError` into more specific `ArgumentError`.
1 parent 3b899d9 commit 6680786

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/statisticalmodel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function r2(model::StatisticalModel, variant::Symbol)
255255
dev0 = nulldeviance(model)
256256
1 - dev/dev0
257257
else
258-
error("variant must be one of $(join(loglikbased, ", ")) or :devianceratio")
258+
throw(ArgumentError("variant must be one of $(join(loglikbased, ", ")) or :devianceratio"))
259259
end
260260
end
261261

@@ -298,7 +298,7 @@ function adjr2(model::StatisticalModel, variant::Symbol)
298298
dev0 = nulldeviance(model)
299299
1 - (dev*(n-1))/(dev0*(n-k))
300300
else
301-
error("variant must be one of :McFadden or :devianceratio")
301+
throw(ArgumentError("variant must be one of :McFadden or :devianceratio"))
302302
end
303303
end
304304

test/regressionmodel.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module TestRegressionModel
2+
3+
using Test, LinearAlgebra, StatsAPI
4+
using StatsAPI: RegressionModel, crossmodelmatrix
5+
6+
struct MyRegressionModel <: RegressionModel
7+
end
8+
9+
StatsAPI.modelmatrix(::MyRegressionModel) = [1 2; 3 4]
10+
11+
@testset "TestRegressionModel" begin
12+
m = MyRegressionModel()
13+
14+
@test crossmodelmatrix(m) == [10 14; 14 20]
15+
@test crossmodelmatrix(m) isa Symmetric
16+
end
17+
18+
end # module TestRegressionModel

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Test, StatsAPI
22

33
@testset "StatsAPI" begin
44

5-
# Nothing to test currently apart from checking that package can be loaded
5+
include("regressionmodel.jl")
6+
include("statisticalmodel.jl")
67

78
end # @testset "StatsAPI"

test/statisticalmodel.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module TestStatisticalModel
2+
3+
using Test, StatsAPI
4+
using StatsAPI: StatisticalModel, stderror, aic, aicc, bic, r2, r², adjr2, adjr²
5+
6+
struct MyStatisticalModel <: StatisticalModel
7+
end
8+
9+
StatsAPI.vcov(::MyStatisticalModel) = [1 2; 3 4]
10+
StatsAPI.loglikelihood(::MyStatisticalModel) = 3
11+
StatsAPI.nullloglikelihood(::MyStatisticalModel) = 4
12+
StatsAPI.deviance(::MyStatisticalModel) = 25
13+
StatsAPI.nulldeviance(::MyStatisticalModel) = 40
14+
StatsAPI.dof(::MyStatisticalModel) = 5
15+
StatsAPI.nobs(::MyStatisticalModel) = 100
16+
17+
@testset "StatisticalModel" begin
18+
m = MyStatisticalModel()
19+
20+
@test stderror(m) == [1, 2]
21+
@test aic(m) == 4
22+
@test aicc(m) 4.638297872340425
23+
@test bic(m) 17.02585092994046
24+
@test r2(m, :McFadden) 0.25
25+
@test r2(m, :CoxSnell) -0.020201340026755776
26+
@test r2(m, :Nagelkerke) 0.24255074155803877
27+
@test r2(m, :devianceratio) 0.375
28+
29+
@test_throws ArgumentError r2(m, :err)
30+
@test_throws MethodError r2(m)
31+
@test adjr2(m, :McFadden) 1.5
32+
@test adjr2(m, :devianceratio) 0.3486842105263158
33+
@test_throws ArgumentError adjr2(m, :err)
34+
35+
@test r2 ===
36+
@test adjr2 === adjr²
37+
end
38+
39+
end # module TestStatisticalModel

0 commit comments

Comments
 (0)