-
Notifications
You must be signed in to change notification settings - Fork 370
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
Changes from 1 commit
c61a0ec
d46fc37
288552c
e1b068e
4a9a65f
fd12a5d
ff6f706
f94dd83
dd0ae91
db58318
25935c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,11 @@ type ModelFrame | |
contrasts::Dict{Symbol, ContrastsMatrix} | ||
end | ||
|
||
type ModelMatrix{T <: @compat(Union{Matrix{Float32}, Matrix{Float64}, SparseMatrixCSC{Float32,Int}, SparseMatrixCSC{Float64,Int}})} | ||
modelmatrixcontainertypes = [Matrix{Float32}, Matrix{Float64}, | ||
SparseMatrixCSC{Float32,Int}, | ||
SparseMatrixCSC{Float64,Int}] | ||
|
||
type ModelMatrix{T <: Union{modelmatrixcontainertypes...}} | ||
m::T | ||
assign::Vector{Int} | ||
end | ||
|
@@ -437,21 +441,21 @@ If there is an intercept in the model, that column occurs first and its | |
Mixed-effects models include "random-effects" terms which are ignored when | ||
creating the model matrix. | ||
""" | ||
function ModelMatrix(mf::ModelFrame) | ||
function ModelMatrix(T::Union{map(t->Type{t}, modelmatrixcontainertypes)...}, mf::ModelFrame) | ||
dfrm = mf.df | ||
terms = droprandomeffects(dropresponse!(mf.terms)) | ||
|
||
blocks = Matrix{Float64}[] | ||
blocks = T[] | ||
assign = Int[] | ||
if terms.intercept | ||
push!(blocks, ones(size(dfrm, 1), 1)) # columns of 1's is first block | ||
push!(assign, 0) # this block corresponds to term zero | ||
push!(blocks, convert(T, ones(size(dfrm, 1), 1))) # columns of 1's is first block | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
push!(assign, 0) # this block corresponds to term zero | ||
end | ||
|
||
factors = terms.factors | ||
|
||
## Map eval. term name + redundancy bool to cached model matrix columns | ||
eterm_cols = @compat Dict{Tuple{Symbol,Bool}, Array{Float64}}() | ||
eterm_cols = @compat Dict{Tuple{Symbol,Bool}, T}() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find any reason not to restrict the Array dimension here. Did I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only issue I can think of is the case where a single-column term would give a column vector instead of a one-column matrix. But conversion will probably happen automatically, and tests should catch this. Have you run the tests of GLM.jl on the modified package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I could tell |
||
## Accumulator for each term's vector of eval. term columns. | ||
|
||
## TODO: this method makes multiple copies of the data in the ModelFrame: | ||
|
@@ -462,7 +466,7 @@ function ModelMatrix(mf::ModelFrame) | |
## "promoted" full-rank versions of categorical columns for non-redundant | ||
## eval. terms: | ||
for (i_term, term) in enumerate(terms.terms) | ||
term_cols = Matrix{Float64}[] | ||
term_cols = T[] | ||
## Pull out the eval terms, and the non-redundancy flags for this term | ||
ff = Compat.view(factors, :, i_term) | ||
eterms = Compat.view(terms.eterms, ff) | ||
|
@@ -479,8 +483,9 @@ function ModelMatrix(mf::ModelFrame) | |
append!(assign, fill(i_term, size(blocks[end], 2))) | ||
end | ||
|
||
ModelMatrix{Matrix{Float64}}(reduce(hcat, blocks), assign) | ||
ModelMatrix{T}(reduce(hcat, blocks), assign) | ||
end | ||
ModelMatrix(mf::ModelFrame) = ModelMatrix(Matrix{Float64}, mf) | ||
|
||
|
||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should accept any subtype of
Union{AbstractMatrix{Float32}, AbstractMatrix{Float32}}
? Or is there anything specific that onlyMatrix
andSparseMatrixCSC
support?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I'm aware of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we even skip the
Union
and doAbstractMatrix{AbstractFloat}
, or does it have to be 32- or 64-bit floats?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea. @andreasnoack?