Skip to content

Commit

Permalink
more docs more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Sep 2, 2023
1 parent 3fef311 commit 922235f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/statsmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ termnames(t::FormulaTerm) = (termnames(t.lhs), termnames(t.rhs))
"""
termnames(term::AbstractTerm)
Return the name(s) of column(s) generated by a term. Return value is either a
`String` or an iterable of `String`s.
Return the name(s) of column(s) generated by a term.
Return value is either a `String`, an iterable of `String`s or nothing if there
no associated name (e.g. `termnames(InterceptTerm{false}())`).
"""
termnames(::InterceptTerm{H}) where {H} = H ? "(Intercept)" : nothing
termnames(t::ContinuousTerm) = string(t.sym)
Expand All @@ -160,6 +162,11 @@ termnames(t::Term) = string(t.sym)
termnames(t::ConstantTerm) = string(t.n)
termnames(t::FunctionTerm) = string(t.exorig)
termnames(ts::TupleTerm) = mapreduce(termnames, vcat, ts)
# these have some surprising behavior:
# termnames(::InteractionTerm) always returns a vector
# termnames(MatrixTerm(term(:a))) returns a scalar
# termnames(MatrixTerm((term(a:), term(:b)))) returns a vector
# but this is the same behavior as coefnames
termnames(t::MatrixTerm) = mapreduce(termnames, vcat, t.terms)
termnames(t::InteractionTerm) =
kron_insideout((args...) -> join(args, " & "), vectorize.(termnames.(t.terms))...)
Expand Down
20 changes: 18 additions & 2 deletions test/statsmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,30 @@ Base.show(io::IO, m::DummyModTwo) = println(io, m.msg)

m2 = fit(DummyModTwo, f, d)
# make sure show() still works when there is no coeftable method
show(io, m2)
show(io, m2)
end

@testset "termnames" begin
# one final termnames check
# note that `1` is still a ConstantTerm and not yet InterceptTerm
# because apply_schema hasn't been called
@test termnames(@formula(y ~ 1 + log(x) * y + (1+x|g)))[2] ==
["1", "log(x)", "y", "log(x) & y", "(1 + x) | g"]

@test termnames(ConstantTerm(1)) == "1"
@test termnames(Term(:x)) == "x"
@test termnames(InterceptTerm{true}()) == "(Intercept)"
@test termnames(InterceptTerm{false}()) === nothing
@test termnames(ContinuousTerm(:x, 1, 0, 0, 0)) == "x"
cm = StatsModels.ContrastsMatrix([1 0; 0 1], ["b", "c"], ["a", "b", "c"], DummyCoding())
@test termnames(CategoricalTerm(:x, cm)) =="x"
@test termnames(FunctionTerm(log, [Term(:x)], :(log(x)))) == "log(x)"
# these next few seem a little weird but they're consistent with the
# definition of coefnames
@test termnames(InteractionTerm(term.((:a, :b, :c)))) == ["a & b & c"]
@test termnames(MatrixTerm(term(:a))) == "a"
@test termnames(MatrixTerm((term(:a), term(:b)))) == ["a", "b"]
@test termnames((term(:a), term(:b))) == ["a", "b"]
@test termnames((term(:a),)) == "a"
end

@testset "lrtest" begin
Expand Down

0 comments on commit 922235f

Please sign in to comment.