Skip to content

Commit e3f6ea1

Browse files
authored
Deprecate un-used base= kwarg for SeqDiffCoding (#238)
* remove base field from seqdiffcoding and depwarn when base= kwarg * typo * suggestion from code review: clearer deprecation warning * also error message in test
1 parent 50da19f commit e3f6ea1

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/contrasts.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Base.getindex(contrasts::ContrastsMatrix{C,T}, rowinds, colinds) where {C,T} =
234234
# Making a contrast type T only requires that there be a method for
235235
# contrasts_matrix(T, baseind, n) and optionally termnames(T, levels, baseind)
236236
# The rest is boilerplate.
237-
for contrastType in [:DummyCoding, :EffectsCoding, :HelmertCoding, :SeqDiffCoding]
237+
for contrastType in [:DummyCoding, :EffectsCoding, :HelmertCoding]
238238
@eval begin
239239
mutable struct $contrastType <: AbstractContrasts
240240
base::Any
@@ -448,7 +448,22 @@ julia> StatsModels.hypothesis_matrix(seqdiff)
448448
"""
449449
SeqDiffCoding
450450

451-
function contrasts_matrix(C::SeqDiffCoding, baseind, n)
451+
mutable struct SeqDiffCoding <: AbstractContrasts
452+
levels::Union{AbstractVector,Nothing}
453+
end
454+
function SeqDiffCoding(; base=nothing, levels::Union{AbstractVector,Nothing}=nothing)
455+
if base !== nothing
456+
Base.depwarn("`base=` kwarg for `SeqDiffCoding` has no effect and is deprecated. " *
457+
"Specify full order of levels using `levels=` instead",
458+
:SeqDiffCoding)
459+
end
460+
return SeqDiffCoding(levels)
461+
end
462+
463+
baselevel(c::SeqDiffCoding) = c.levels === nothing ? nothing : c.levels[1]
464+
DataAPI.levels(c::SeqDiffCoding) = c.levels
465+
466+
function contrasts_matrix(C::SeqDiffCoding, _, n)
452467
mat = zeros(n, n-1)
453468
for col in 1:n-1
454469
mat[1:col, col] .= col-n

test/contrasts.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@
289289
using StatsModels: baselevel, FullDummyCoding, ContrastsCoding
290290

291291
levs = [:a, :b, :c, :d]
292-
base = [:a]
293-
for C in [DummyCoding, EffectsCoding, SeqDiffCoding, HelmertCoding]
292+
base = [:c]
293+
for C in [DummyCoding, EffectsCoding, HelmertCoding]
294294
c = C()
295295
@test levels(c) == nothing
296296
@test baselevel(c) == nothing
@@ -308,6 +308,25 @@
308308
@test baselevel(c) == base
309309
end
310310

311+
c = SeqDiffCoding()
312+
@test baselevel(c) == nothing
313+
@test levels(c) == nothing
314+
315+
c = SeqDiffCoding(levels=levs)
316+
@test baselevel(c) == levs[1]
317+
@test levels(c) == levs
318+
319+
c = @test_logs((:warn,
320+
"`base=` kwarg for `SeqDiffCoding` has no effect and is deprecated. " *
321+
"Specify full order of levels using `levels=` instead"),
322+
SeqDiffCoding(base=base))
323+
@test baselevel(c) == nothing
324+
@test levels(c) == nothing
325+
326+
c = SeqDiffCoding(base=base, levels=levs)
327+
@test baselevel(c) == levs[1]
328+
@test levels(c) == levs
329+
311330
c = FullDummyCoding()
312331
@test baselevel(c) == nothing
313332
@test levels(c) == nothing

0 commit comments

Comments
 (0)