Skip to content

Commit

Permalink
fix degree for ConstantTerm/InterceptTerm (#287)
Browse files Browse the repository at this point in the history
* add tests for currently expected behavior

* the fix

* also test intercept terms too

* bump patch
  • Loading branch information
kleinschmidt authored Apr 4, 2023
1 parent bdba80b commit 41b0254
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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.0"
version = "0.7.1"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
2 changes: 2 additions & 0 deletions src/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ Base.:*(a::TermOrTerms, b::TermOrTerms) = a + b + a&b
cleanup(terms::TupleTerm) = Tuple(sort!(unique!(collect(terms)), by=degree))
cleanup(x) = x

degree(::ConstantTerm) = 0
degree(::InterceptTerm) = 0
degree(::AbstractTerm) = 1
degree(t::InteractionTerm) = mapreduce(degree, +, t.terms)
# dirty hack, move to MixedModels.jl
Expand Down
11 changes: 11 additions & 0 deletions test/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,15 @@ StatsModels.apply_schema(mt::MultiTerm, sch::StatsModels.Schema, Mod::Type) =
@test_throws ArgumentError concrete_term(term(:not_there), t )
end

@testset "sort by degree in ~" begin
one, a, b = term.([1, :a, :b])
for zero_deg in [one, InterceptTerm{true}(), InterceptTerm{false}()]
@test a + zero_deg == (a, zero_deg)
@test (a ~ a + zero_deg) == (a ~ zero_deg + a)

@test a & b + zero_deg + a == (a & b, zero_deg, a)
@test (a ~ a & b + zero_deg + a) == (a ~ zero_deg + a + a & b)
end
end

end

2 comments on commit 41b0254

@kleinschmidt
Copy link
Member Author

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 created: JuliaRegistries/General/81005

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.7.1 -m "<description of version>" 41b025409af03c0e019591ac6e817b22efbb4e17
git push origin v0.7.1

Please sign in to comment.