Skip to content

Commit

Permalink
Fix compat + nested TermTuple (#4)
Browse files Browse the repository at this point in the history
* compat for combinatorics

* support TupleTerm as innermost nesting

* slight consistency change
  • Loading branch information
palday authored Oct 22, 2021
1 parent f3e8daf commit 91a144f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"

[compat]
Combinatorics = "1"
StatsBase = "0.33"
StatsModels = "0.6.7"
julia = "1.6"
Expand Down
25 changes: 21 additions & 4 deletions src/nesting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ function _isfulldummy(x::CategoricalTerm)
return isa(x.contrasts, StatsModels.ContrastsMatrix{StatsModels.FullDummyCoding})
end

function _fulldummycheck(outer::InteractionTerm)
all(_isfulldummy, outer.terms[1:end-1]) ||
throw(ArgumentError("Outer interactions in a nesting must consist only " *
" of categorical terms with FullDummyCoding, got $outer"))
return nothing
end

"""
group / term
Expand All @@ -14,19 +21,29 @@ function Base.:(/)(outer::CategoricalTerm, inner::AbstractTerm)
return outer + fulldummy(outer) & inner
end

function Base.:(/)(outer::TermTuple, inner::AbstractTerm)
function Base.:(/)(outer::CategoricalTerm, inner::TermTuple)
fd = fulldummy(outer)
return mapfoldl(x -> fd & x, +, inner; init=outer)
end

function Base.:(/)(outer::TermTuple, inner::Union{AbstractTerm, TermTuple})
return outer[1:end-1] + last(outer) / inner
end

function Base.:(/)(outer::InteractionTerm, inner::AbstractTerm)
# we should only get here via expansion where the interaction term,
# but who knows what devious things users will try
all(_isfulldummy, outer.terms[1:end-1]) ||
throw(ArgumentError("Outer interactions in a nesting must consist only " *
" of categorical terms with FullDummyCoding, got $outer"))
_fulldummycheck(outer)
return outer + outer & inner
end

function Base.:(/)(outer::InteractionTerm, inner::TermTuple)
# we should only get here via expansion where the interaction term,
# but who knows what devious things users will try
_fulldummycheck(outer)
return mapfoldl(x -> outer & x, +, inner; init=outer)
end

function Base.:(/)(outer::AbstractTerm, inner::AbstractTerm)
throw(ArgumentError("nesting terms requires categorical grouping term, got $outer / $inner " *
"Manually specify $outer as `CategoricalTerm` in hints/contrasts"))
Expand Down
28 changes: 28 additions & 0 deletions test/nesting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ end
m = fit(DummyMod, @formula(y ~ 1 + a / x), dat)
@test coefnames(m) == ["(Intercept)", "a: o", "a: u",
"a: i & x", "a: o & x", "a: u & x"]

m = fit(DummyMod, @formula(y ~ 0 + a / (b + x)), dat)
@test coefnames(m) == ["a: i", "a: o", "a: u",
"a: i & b: q", "a: o & b: q", "a: u & b: q",
"a: i & b: w", "a: o & b: w", "a: u & b: w",
"a: i & x", "a: o & x", "a: u & x"]
m = fit(DummyMod, @formula(y ~ 0 + a / (b * x)), dat)
@test coefnames(m) == ["a: i", "a: o", "a: u",
"a: i & b: q", "a: o & b: q", "a: u & b: q",
"a: i & b: w", "a: o & b: w", "a: u & b: w",
"a: i & x", "a: o & x", "a: u & x",
"a: i & b: q & x", "a: o & b: q & x", "a: u & b: q & x",
"a: i & b: w & x", "a: o & b: w & x", "a: u & b: w & x"]
end

@testset "multiple nesting levels" begin
Expand Down Expand Up @@ -68,4 +81,19 @@ end
"a: i & b: q & x", "a: o & b: q & x",
"a: u & b: q & x", "a: i & b: w & x",
"a: o & b: w & x", "a: u & b: w & x"]

m = fit(DummyMod, @formula(y ~ 0 + a / b / (c * x)), dat)
@test coefnames(m) == ["a: i", "a: o", "a: u",
"a: i & b: q", "a: o & b: q", "a: u & b: q",
"a: i & b: w", "a: o & b: w", "a: u & b: w",
"a: i & b: q & c: f", "a: o & b: q & c: f", "a: u & b: q & c: f",
"a: i & b: w & c: f", "a: o & b: w & c: f", "a: u & b: w & c: f",
"a: i & b: q & c: s", "a: o & b: q & c: s", "a: u & b: q & c: s",
"a: i & b: w & c: s", "a: o & b: w & c: s", "a: u & b: w & c: s",
"a: i & b: q & x", "a: o & b: q & x", "a: u & b: q & x",
"a: i & b: w & x", "a: o & b: w & x", "a: u & b: w & x",
"a: i & b: q & c: f & x", "a: o & b: q & c: f & x", "a: u & b: q & c: f & x",
"a: i & b: w & c: f & x", "a: o & b: w & c: f & x", "a: u & b: w & c: f & x",
"a: i & b: q & c: s & x", "a: o & b: q & c: s & x", "a: u & b: q & c: s & x",
"a: i & b: w & c: s & x", "a: o & b: w & c: s & x", "a: u & b: w & c: s & x"]
end

3 comments on commit 91a144f

@palday
Copy link
Collaborator Author

@palday palday commented on 91a144f Oct 22, 2021

Choose a reason for hiding this comment

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

@kleinschmidt
Copy link
Owner

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request updated: JuliaRegistries/General/47299

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 91a144f5ee75e41be5fdaa9ae51e78f10b5b1390
git push origin v0.1.0

Please sign in to comment.