|
1 | 1 | @testset "contrasts" begin |
2 | 2 |
|
3 | 3 | cm = StatsModels.ContrastsMatrix(DummyCoding(), ["a", "b"]) |
4 | | - @test_logs((:warn, |
| 4 | + @test_logs((:warn, |
5 | 5 | "The `termnames` field of `ConstrastsMatrix` is deprecated; use `coefnames(cm)` instead."), |
6 | 6 | cm.termnames) |
7 | 7 | @test cm.termnames == cm.coefnames |
|
87 | 87 | 1 -1 -1 |
88 | 88 | 1 0 1] |
89 | 89 | @test coefnames(mf) == ["(Intercept)"; "x: c"; "x: b"] |
90 | | - |
| 90 | + |
91 | 91 | # respect order of levels |
92 | | - |
| 92 | + |
93 | 93 | data = DataFrame(x = levels!(categorical(['A', 'B', 'C', 'C', 'D']), ['C', 'B', 'A', 'D'])) |
94 | 94 | f = apply_schema(@formula(x ~ 1), schema(data)) |
95 | 95 | @test modelcols(f.lhs, data) == [0 1 0; 1 0 0; 0 0 0; 0 0 0; 0 0 1] |
|
239 | 239 | f_effects = apply_schema(f, schema(d2, Dict(:x => effects_hyp))) |
240 | 240 |
|
241 | 241 | y_means = combine(groupby(d2, :x), :y => mean).y_mean |
242 | | - |
| 242 | + |
243 | 243 | y, X_sdiff = modelcols(f_sdiff, d2) |
244 | 244 | @test X_sdiff \ y ≈ [mean(y_means); diff(y_means)] |
245 | 245 |
|
|
271 | 271 | [0 1 0 0 |
272 | 272 | 0 0 1 0 |
273 | 273 | 0 0 0 1] |
274 | | - |
| 274 | + |
275 | 275 |
|
276 | 276 | cmat2 = contrasts_matrix(HelmertCoding(), 1, 4) |
277 | 277 | @test needs_intercept(cmat2) == false |
278 | | - hmat2 = hypothesis_matrix(cmat2) |
| 278 | + hmat2 = hypothesis_matrix(cmat2) |
279 | 279 | @test hmat2 ≈ |
280 | 280 | [-1/2 1/2 0 0 |
281 | 281 | -1/6 -1/6 1/3 0 |
282 | 282 | -1/12 -1/12 -1/12 1/4] |
283 | 283 |
|
284 | 284 | @test eltype(hmat2) <: Rational |
285 | | - |
| 285 | + |
286 | 286 | @test hypothesis_matrix(cmat2, intercept=true) ≈ |
287 | 287 | vcat([1/4 1/4 1/4 1/4], hmat2) |
288 | 288 |
|
|
350 | 350 | @test levels(c) == levs |
351 | 351 | # no notion of base level for ContrastsCoding |
352 | 352 | @test_throws MethodError ContrastsCoding(rand(4,3), base=base) |
353 | | - |
| 353 | + |
354 | 354 | end |
355 | 355 |
|
356 | 356 | @testset "Non-unique levels" begin |
|
396 | 396 |
|
397 | 397 | mm = modelcols(term, (; x=repeat('a':'d'; inner=2))) |
398 | 398 | smm = modelcols(spterm, (; x=repeat('a':'d'; inner=2))) |
399 | | - |
| 399 | + |
400 | 400 | @test mm isa Matrix |
401 | 401 | @test smm isa SparseMatrixCSC |
402 | 402 | @test mm == smm |
403 | 403 | end |
404 | | - |
| 404 | + |
| 405 | + @testset "booleans as categorical" begin |
| 406 | + cm = ContrastsMatrix(EffectsCoding(), [true, false]) |
| 407 | + @test cm.coefnames == [false] |
| 408 | + @test issetequal(cm.levels, [true, false]) |
| 409 | + |
| 410 | + hypothesis = [0 1]' |
| 411 | + cm = ContrastsMatrix(EffectsCoding(), [true, false]) |
| 412 | + @test cm.coefnames == [false] |
| 413 | + @test issetequal(cm.levels, [true, false]) |
| 414 | + |
| 415 | + hc = HypothesisCoding([1 0]; levels=[true, false], labels=["yes", "no"]) |
| 416 | + cm = ContrastsMatrix(hc, [true, false]) |
| 417 | + @test issetequal(cm.coefnames, ["yes", "no"]) |
| 418 | + @test issetequal(cm.levels, [true, false]) |
| 419 | + end |
| 420 | + |
405 | 421 | end |
0 commit comments