|
| 1 | +using StatsModels: hasintercept, omitsintercept |
| 2 | +import StatsModels: drop_intercept, implicit_intercept |
| 3 | + |
| 4 | +struct DroppyMod <: StatisticalModel end |
| 5 | +drop_intercept(::Type{DroppyMod}) = true |
| 6 | + |
| 7 | +# define structs for testing implicit intercept trait: |
| 8 | + |
| 9 | +# default for StatisticalModel is true |
| 10 | +struct DefaultImplicit <: StatisticalModel end |
| 11 | + |
| 12 | +# override default = true for StatisticalModels |
| 13 | +struct NoImplicit <: StatisticalModel end |
| 14 | +implicit_intercept(::Type{NoImplicit}) = false |
| 15 | + |
| 16 | +# manual override of default = false |
| 17 | +struct YesImplicit end |
| 18 | +implicit_intercept(::Type{YesImplicit}) = true |
| 19 | + |
| 20 | + |
| 21 | +@testset "Model traits" begin |
| 22 | + d = (y = rand(10), x = rand(10), z = [:a, :b, :c]) |
| 23 | + sch = schema(d) |
| 24 | + f = @formula(y ~ x) |
| 25 | + f1 = @formula(y ~ 1 + x) |
| 26 | + f0 = @formula(y ~ 0 + x) |
| 27 | + |
| 28 | + @testset "drop_intercept" begin |
| 29 | + @test_throws ArgumentError apply_schema(f1, sch, DroppyMod) |
| 30 | + ff = apply_schema(f, sch, DroppyMod) |
| 31 | + @test !hasintercept(ff) |
| 32 | + @test !omitsintercept(ff) |
| 33 | + ff0 = apply_schema(f0, sch, DroppyMod) |
| 34 | + @test !hasintercept(ff0) |
| 35 | + @test omitsintercept(ff0) |
| 36 | + |
| 37 | + @test drop_intercept(DroppyMod()) == drop_intercept(DroppyMod) |
| 38 | + # drop_intercept blocks implicit_intercept == true |
| 39 | + @test implicit_intercept(DroppyMod) |
| 40 | + |
| 41 | + @testset "categorical promotion" begin |
| 42 | + # drop_intercept == true means that model should always ACT like |
| 43 | + # intercept is present even if it's not specified or even ommitted. |
| 44 | + # (pushes intercept term to the FullRank already seen terms list). |
| 45 | + |
| 46 | + # full dummy coding |
| 47 | + @test width(apply_schema(@formula(y ~ 0 + z), sch, StatisticalModel).rhs) == 3 |
| 48 | + # droppy regular coding |
| 49 | + @test width(apply_schema(@formula(y ~ 0 + z), sch, DroppyMod).rhs) == 2 |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + @testset "implicit_intercept" begin |
| 54 | + @testset "default" begin |
| 55 | + ff, ff0, ff1 = apply_schema.((f, f0, f1), Ref(sch), Any) |
| 56 | + @test !hasintercept(ff) |
| 57 | + @test !hasintercept(ff0) |
| 58 | + @test hasintercept(ff1) |
| 59 | + @test !omitsintercept(ff) |
| 60 | + @test omitsintercept(ff0) |
| 61 | + @test !omitsintercept(ff1) |
| 62 | + end |
| 63 | + |
| 64 | + @testset "StatisticalModel default" begin |
| 65 | + ff, ff0, ff1 = apply_schema.((f, f0, f1), Ref(sch), DefaultImplicit) |
| 66 | + @test hasintercept(ff) |
| 67 | + @test !hasintercept(ff0) |
| 68 | + @test hasintercept(ff1) |
| 69 | + @test !omitsintercept(ff) |
| 70 | + @test omitsintercept(ff0) |
| 71 | + @test !omitsintercept(ff1) |
| 72 | + |
| 73 | + @test implicit_intercept(DefaultImplicit()) == implicit_intercept(DefaultImplicit) |
| 74 | + end |
| 75 | + |
| 76 | + @testset "Override StatisticalModel default" begin |
| 77 | + ff, ff0, ff1 = apply_schema.((f, f0, f1), Ref(sch), NoImplicit) |
| 78 | + @test !hasintercept(ff) |
| 79 | + @test !hasintercept(ff0) |
| 80 | + @test hasintercept(ff1) |
| 81 | + @test !omitsintercept(ff) |
| 82 | + @test omitsintercept(ff0) |
| 83 | + @test !omitsintercept(ff1) |
| 84 | + |
| 85 | + @test implicit_intercept(NoImplicit()) == implicit_intercept(NoImplicit) |
| 86 | + end |
| 87 | + |
| 88 | + @testset "Override Any default" begin |
| 89 | + ff, ff0, ff1 = apply_schema.((f, f0, f1), Ref(sch), YesImplicit) |
| 90 | + # broken because traits are not checked during apply_schema for |
| 91 | + # context that is not <:StatisticalModel |
| 92 | + @test_broken hasintercept(ff) |
| 93 | + @test !hasintercept(ff0) |
| 94 | + @test hasintercept(ff1) |
| 95 | + @test !omitsintercept(ff) |
| 96 | + @test omitsintercept(ff0) |
| 97 | + @test !omitsintercept(ff1) |
| 98 | + |
| 99 | + @test implicit_intercept(YesImplicit()) == implicit_intercept(YesImplicit) |
| 100 | + end |
| 101 | + |
| 102 | + end |
| 103 | +end |
0 commit comments