Skip to content

Commit

Permalink
Handle MatrixTerm when calculating column-to-term indexes in ModelMat…
Browse files Browse the repository at this point in the history
…rix (#134)

* handle MatrixTerm in asgn model matrix calculation

* add test for #133

* bump version (bugfix)
  • Loading branch information
kleinschmidt committed Jul 18, 2019
1 parent 3891ba6 commit 14f3b54
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
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.6.1"
version = "0.6.2"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
11 changes: 8 additions & 3 deletions src/modelframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,16 @@ end

Base.size(mm::ModelMatrix, dim...) = size(mm.m, dim...)

asgn(f::FormulaTerm) = asgn(f.rhs)
asgn(mt::MatrixTerm) = asgn(mt.terms)
asgn(t) = mapreduce(((i,t), ) -> i*ones(width(t)),
append!,
enumerate(vectorize(t)),
init=Int[])

function ModelMatrix{T}(mf::ModelFrame) where T<:AbstractMatrix{<:AbstractFloat}
mat = modelmatrix(mf)
asgn = mapreduce((it)->first(it)*ones(width(last(it))), append!,
enumerate(vectorize(mf.f.rhs)), init=Int[])
ModelMatrix(convert(T, mat), asgn)
ModelMatrix(convert(T, mat), asgn(mf.f))
end

ModelMatrix(mf::ModelFrame) = ModelMatrix{Matrix{Float64}}(mf)
13 changes: 13 additions & 0 deletions test/modelframe.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testset "ModelFrame (legacy API)" begin

@testset "#133" begin
df = (x = rand(12),
y = categorical(repeat(1:3, inner=4)),
z = categorical(repeat(1:2, outer=6)));
f = @formula(x ~ y * z);
mf = ModelFrame(f, df)
mm = ModelMatrix(mf)
@test mm.assign == [1, 2, 2, 3, 4, 4]
end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ my_tests = ["formula.jl",
"temporal_terms.jl",
"schema.jl",
"modelmatrix.jl",
"modelframe.jl",
"statsmodel.jl",
"contrasts.jl",
"extension.jl"]
Expand Down

2 comments on commit 14f3b54

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

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.2 -m "<description of version>" 14f3b54df8fd537e565eb3c81503c341d52575c9
git push origin v0.6.2

Please sign in to comment.