Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.jl.mem
docs/build
Manifest.toml
Manifest-v*.toml
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsModels"
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
version = "0.7.5"
version = "0.7.6"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
9 changes: 9 additions & 0 deletions src/contrasts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ function StatsAPI.coefnames(C::AbstractContrasts, levels::AbstractVector, basein
DataAPI.unwrap.(levels[not_base])
end

function StatsAPI.coefnames(C::AbstractContrasts, levels::AbstractVector{Bool}, baseind::Integer)
not_base = [1:(baseind-1); (baseind+1):length(levels)]
# broadcasted DataAPI.unwrap converts Vector{Bool} to BitVector
convert(Vector{Bool}, DataAPI.unwrap.(levels[not_base]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this compares in terms of performance to something like

Bool[DataAPI.unwrap(level) for (i, level) in pairs(level) if i != baseind]

or perhaps

Bool[DataAPI.unwrap(@inbounds levels[i])
     for i in Iterators.flatten((firstindex(levels):(baseind - 1),
                                 (baseind + 1):lastindex(levels)))]

luv 2 micro-optimize

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is your performance bottleneck ... 😂

end

Base.getindex(contrasts::ContrastsMatrix, rowinds, colinds) =
getindex(contrasts.matrix, getindex.(Ref(contrasts.invindex), rowinds), colinds)

Expand Down Expand Up @@ -598,6 +604,9 @@ end
StatsAPI.coefnames(C::HypothesisCoding, levels::AbstractVector, baseind::Int) =
something(C.labels, DataAPI.unwrap.(levels[1:length(levels) .!= baseind]))

StatsAPI.coefnames(C::HypothesisCoding, levels::AbstractVector{Bool}, baseind::Int) =
something(C.labels, DataAPI.unwrap.(levels[1:length(levels) .!= baseind]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether it's worth using invoke to avoid duplicating the method body?

Suggested change
something(C.labels, DataAPI.unwrap.(levels[1:length(levels) .!= baseind]))
invoke(coefnames, Tuple{HypothesisCoding,AbstractVector,Int}, C, levels, baseind)

Probably doesn't really matter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I'll consider that next time, going with the "no change" lazy and fast option for now 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very reasonable


DataAPI.levels(c::HypothesisCoding) = c.levels

"""
Expand Down
36 changes: 26 additions & 10 deletions test/contrasts.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "contrasts" begin

cm = StatsModels.ContrastsMatrix(DummyCoding(), ["a", "b"])
@test_logs((:warn,
@test_logs((:warn,
"The `termnames` field of `ConstrastsMatrix` is deprecated; use `coefnames(cm)` instead."),
cm.termnames)
@test cm.termnames == cm.coefnames
Expand Down Expand Up @@ -87,9 +87,9 @@
1 -1 -1
1 0 1]
@test coefnames(mf) == ["(Intercept)"; "x: c"; "x: b"]

# respect order of levels

data = DataFrame(x = levels!(categorical(['A', 'B', 'C', 'C', 'D']), ['C', 'B', 'A', 'D']))
f = apply_schema(@formula(x ~ 1), schema(data))
@test modelcols(f.lhs, data) == [0 1 0; 1 0 0; 0 0 0; 0 0 0; 0 0 1]
Expand Down Expand Up @@ -239,7 +239,7 @@
f_effects = apply_schema(f, schema(d2, Dict(:x => effects_hyp)))

y_means = combine(groupby(d2, :x), :y => mean).y_mean

y, X_sdiff = modelcols(f_sdiff, d2)
@test X_sdiff \ y ≈ [mean(y_means); diff(y_means)]

Expand Down Expand Up @@ -271,18 +271,18 @@
[0 1 0 0
0 0 1 0
0 0 0 1]


cmat2 = contrasts_matrix(HelmertCoding(), 1, 4)
@test needs_intercept(cmat2) == false
hmat2 = hypothesis_matrix(cmat2)
hmat2 = hypothesis_matrix(cmat2)
@test hmat2 ≈
[-1/2 1/2 0 0
-1/6 -1/6 1/3 0
-1/12 -1/12 -1/12 1/4]

@test eltype(hmat2) <: Rational

@test hypothesis_matrix(cmat2, intercept=true) ≈
vcat([1/4 1/4 1/4 1/4], hmat2)

Expand Down Expand Up @@ -350,7 +350,7 @@
@test levels(c) == levs
# no notion of base level for ContrastsCoding
@test_throws MethodError ContrastsCoding(rand(4,3), base=base)

end

@testset "Non-unique levels" begin
Expand Down Expand Up @@ -396,10 +396,26 @@

mm = modelcols(term, (; x=repeat('a':'d'; inner=2)))
smm = modelcols(spterm, (; x=repeat('a':'d'; inner=2)))

@test mm isa Matrix
@test smm isa SparseMatrixCSC
@test mm == smm
end


@testset "booleans as categorical" begin
cm = ContrastsMatrix(EffectsCoding(), [true, false])
@test cm.coefnames == [false]
@test issetequal(cm.levels, [true, false])

hypothesis = [0 1]'
cm = ContrastsMatrix(EffectsCoding(), [true, false])
@test cm.coefnames == [false]
@test issetequal(cm.levels, [true, false])

hc = HypothesisCoding([1 0]; levels=[true, false], labels=["yes", "no"])
cm = ContrastsMatrix(hc, [true, false])
@test issetequal(cm.coefnames, ["yes", "no"])
@test issetequal(cm.levels, [true, false])
end

end
Loading