Skip to content

Commit

Permalink
test support for non-continuous terms (e.g., categorical0 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinschmidt authored May 3, 2021
1 parent 73562f0 commit 8d3122f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/centering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ function center(t::AbstractTerm, c::Center)
end

StatsModels.modelcols(t::CenteredTerm, d::NamedTuple) = modelcols(t.term, d) .- t.center
StatsBase.coefnames(t::CenteredTerm) = "$(coefnames(t.term))(centered: $(t.center))"
function StatsBase.coefnames(t::CenteredTerm)
if StatsModels.width(t.term) == 1
return "$(coefnames(t.term))(centered: $(t.center))"
elseif length(t.center) > 1
return string.(vec(coefnames(t.term)), "(centered: ", vec(t.center), ")")
else
return string.(coefnames(t.term), "(centered: ", t.center, ")")
end
end
# coef table: "x: centered at 5.5"
Base.show(io::IO, t::CenteredTerm) = show(io, t.term)
# regular show: "x"
Expand Down
20 changes: 20 additions & 0 deletions test/centering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
xc2 = center(x, Center(2))
xc22 = center(x, 2)
@test xc2.center == xc22.center == 2

end

@testset "plays nicely with formula" begin
Expand Down Expand Up @@ -82,4 +83,23 @@
@test coefnames(xc) == "x(centered: 5.5)"
end

@testset "categorical term" begin
z = concrete_term(term(:z), data)
@test_throws ArgumentError center(z)
@test_throws ArgumentError center(z, Center())
zc = center(z, Center(0.5))
@test modelcols(zc, data) == modelcols(z, data) .- 0.5
@test StatsModels.width(zc) == StatsModels.width(z)
@test coefnames(zc) == coefnames(z) .* "(centered: 0.5)"

zc2 = center(z, Center([1 2 3 4]))
@test modelcols(zc2, data) == modelcols(z, data) .- [1 2 3 4]
@test coefnames(zc2) == coefnames(z) .* "(centered: " .* string.([1, 2, 3, 4]) .* ")"
end

# @testset "utilities" begin


# end

end

2 comments on commit 8d3122f

@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/36113

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>" 8d3122f18164b49ce7988a04aa5a7e99e99b6035
git push origin v0.1.0

Please sign in to comment.