Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Sparse ModelMatrix support #1040

Merged
merged 11 commits into from
Aug 26, 2016
2 changes: 1 addition & 1 deletion src/statsmodels/formula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::DataVector) = convert(T, res
modelmat_cols{T<:AbstractFloatMatrix}(::Type{T}, v::Vector) = convert(T, reshape(v, length(v), 1))

"""
modelmat_cols(T::Type{AbstractFloatMatrix}, v::PooledDataVector, contrast::ContrastsMatrix)
modelmat_cols(::Type{T}, v::PooledDataVector, contrast::ContrastsMatrix)
Copy link
Member

Choose a reason for hiding this comment

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

You should also mention the restriction T<:AbstractFloatMatrix as a type parameter.


Construct `ModelMatrix` columns of type `T` based on specified contrasts, ensuring that
levels align properly.
Expand Down
84 changes: 42 additions & 42 deletions test/formula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,41 +405,41 @@ d[:n] = 1.:8
mf = ModelFrame(n ~ 0 + x, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m == [1 0
0 1
1 0
0 1
1 0
0 1
1 0
0 1]
0 1
1 0
0 1
1 0
0 1
1 0
0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["x: a", "x: b"]

## No first-order term for interaction
mf = ModelFrame(n ~ 1 + x + x&y, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m[:, 2:end] == [-1 -1 0
1 0 -1
-1 1 0
1 0 1
-1 -1 0
1 0 -1
-1 1 0
1 0 1]
1 0 -1
-1 1 0
1 0 1
-1 -1 0
1 0 -1
-1 1 0
1 0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["(Intercept)", "x: b", "x: a & y: d", "x: b & y: d"]

## When both terms of interaction are non-redundant:
mf = ModelFrame(n ~ 0 + x&y, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m == [1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1]
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["x: a & y: c", "x: b & y: c",
"x: a & y: d", "x: b & y: d"]
Expand All @@ -457,13 +457,13 @@ mm = ModelMatrix(mf)
mf = ModelFrame(n ~ 0 + x&y + x&z, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m == [1 0 0 0 -1 0
0 1 0 0 0 -1
0 0 1 0 -1 0
0 0 0 1 0 -1
1 0 0 0 1 0
0 1 0 0 0 1
0 0 1 0 1 0
0 0 0 1 0 1]
0 1 0 0 0 -1
0 0 1 0 -1 0
0 0 0 1 0 -1
1 0 0 0 1 0
0 1 0 0 0 1
0 0 1 0 1 0
0 0 0 1 0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["x: a & y: c", "x: b & y: c",
"x: a & y: d", "x: b & y: d",
Expand All @@ -475,13 +475,13 @@ mm = ModelMatrix(mf)
mf = ModelFrame(n ~ 0 + x&y + x&z + x&y&z, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m == [1 0 0 0 -1 0 1 0
0 1 0 0 0 -1 0 1
0 0 1 0 -1 0 -1 0
0 0 0 1 0 -1 0 -1
1 0 0 0 1 0 -1 0
0 1 0 0 0 1 0 -1
0 0 1 0 1 0 1 0
0 0 0 1 0 1 0 1]
0 1 0 0 0 -1 0 1
0 0 1 0 -1 0 -1 0
0 0 0 1 0 -1 0 -1
1 0 0 0 1 0 -1 0
0 1 0 0 0 1 0 -1
0 0 1 0 1 0 1 0
0 0 0 1 0 1 0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["x: a & y: c", "x: b & y: c",
"x: a & y: d", "x: b & y: d",
Expand All @@ -494,13 +494,13 @@ mm = ModelMatrix(mf)
mf = ModelFrame(n ~ 0 + x + x&y + x&z, d, contrasts=cs)
mm = ModelMatrix(mf)
@test mm.m == [1 0 -1 0 -1 0
0 1 0 -1 0 -1
1 0 1 0 -1 0
0 1 0 1 0 -1
1 0 -1 0 1 0
0 1 0 -1 0 1
1 0 1 0 1 0
0 1 0 1 0 1]
0 1 0 -1 0 -1
1 0 1 0 -1 0
0 1 0 1 0 -1
1 0 -1 0 1 0
0 1 0 -1 0 1
1 0 1 0 1 0
0 1 0 1 0 1]
@test mm.m == ModelMatrix{sparsetype}(mf).m
@test coefnames(mf) == ["x: a", "x: b",
"x: a & y: d", "x: b & y: d",
Expand Down