-
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 |
---|---|---|
|
@@ -323,8 +323,8 @@ function setcontrasts!(mf::ModelFrame, new_contrasts::Dict) | |
end | ||
setcontrasts!(mf::ModelFrame; kwargs...) = setcontrasts!(mf, Dict(kwargs)) | ||
|
||
asmatrix(a::AbstractMatrix) = a | ||
asmatrix(v::AbstractVector) = reshape(v, (length(v), 1)) | ||
asmatrix(T::Type, a::AbstractMatrix) = convert(T, a) | ||
asmatrix(T::Type, v::AbstractVector) = convert(T, reshape(v, (length(v), 1))) | ||
|
||
""" | ||
StatsBase.model_response(mf::ModelFrame) | ||
|
@@ -339,33 +339,35 @@ function StatsBase.model_response(mf::ModelFrame) | |
end | ||
end | ||
|
||
modelmat_cols(v::DataVector) = asmatrix(convert(Vector{Float64}, v.data)) | ||
modelmat_cols(v::Vector) = asmatrix(convert(Vector{Float64}, v)) | ||
modelmat_cols{T<:ModelMatrixContainer}(::Type{T}, v::DataVector) = asmatrix(T, convert(Vector{Float64}, v.data)) | ||
modelmat_cols{T<:ModelMatrixContainer}(::Type{T}, v::Vector) = asmatrix(T, convert(Vector{Float64}, v)) | ||
|
||
## construct model matrix columns from model frame + name (checks for contrasts) | ||
function modelmat_cols(name::Symbol, mf::ModelFrame; non_redundant::Bool = false) | ||
function modelmat_cols{T<:ModelMatrixContainer}(::Type{T}, name::Symbol, mf::ModelFrame; non_redundant::Bool = false) | ||
if haskey(mf.contrasts, name) | ||
modelmat_cols(mf.df[name], | ||
modelmat_cols(T, mf.df[name], | ||
non_redundant ? | ||
ContrastsMatrix{FullDummyCoding}(mf.contrasts[name]) : | ||
mf.contrasts[name]) | ||
else | ||
modelmat_cols(mf.df[name]) | ||
modelmat_cols(T, mf.df[name]) | ||
end | ||
end | ||
|
||
""" | ||
modelmat_cols(v::PooledDataVector, contrast::ContrastsMatrix) | ||
modelmat_cols(T::Type{ModelMatrixContainer}, v::PooledDataVector, contrast::ContrastsMatrix) | ||
|
||
Construct `ModelMatrix` columns based on specified contrasts, ensuring that | ||
Construct `ModelMatrix` columns of type `T` based on specified contrasts, ensuring that | ||
levels align properly. | ||
""" | ||
function modelmat_cols(v::PooledDataVector, contrast::ContrastsMatrix) | ||
function modelmat_cols{T<:ModelMatrixContainer}(::Type{T}, v::PooledDataVector, contrast::ContrastsMatrix) | ||
## make sure the levels of the contrast matrix and the categorical data | ||
## are the same by constructing a re-indexing vector. Indexing into | ||
## reindex with v.refs will give the corresponding row number of the | ||
## contrast matrix | ||
reindex = [findfirst(contrast.levels, l) for l in levels(v)] | ||
return contrast.matrix[reindex[v.refs], :] | ||
contrastmatrix = convert(T, contrast.matrix) | ||
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. In what cases can 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.
|
||
return contrastmatrix[reindex[v.refs], :] | ||
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. This array creation can be extremely slow for sparse 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. No idea. Why is it slow? Indexing rows shouldn't be a problem for sparse matrices AFAIK. 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'm not an expert on sparse matrix indexing, but it seems to spend a lot of time sorting... Truncated profile output from a million-row reference vector and 5-column constrast matrix:
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. Hmm... You could ask on the mailing list for advice about the best algorithm to do this for sparse matrices. I guess working column by column (for |
||
end | ||
|
||
""" | ||
|
@@ -374,7 +376,7 @@ Create pairwise products of columns from a vector of matrices | |
""" | ||
function expandcols(trm::Vector) | ||
if length(trm) == 1 | ||
asmatrix(convert(Array{Float64}, trm[1])) | ||
asmatrix(Matrix{Float64}, convert(Array{Float64}, trm[1])) | ||
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. As far as I can tell, the conversions here (and just above) are redundant since elements of |
||
else | ||
a = convert(Array{Float64}, trm[1]) | ||
b = expandcols(trm[2 : end]) | ||
|
@@ -439,7 +441,8 @@ 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 (::Type{ModelMatrix{T}}){T<:ModelMatrixContainer}(mf::ModelFrame) | ||
@compat function (::Type{ModelMatrix{T}}){T<:ModelMatrixContainer}(mf::ModelFrame) | ||
sparsemm = T <: AbstractSparseMatrix | ||
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. This variable doesn't seem to be used anywhere. |
||
dfrm = mf.df | ||
terms = droprandomeffects(dropresponse!(mf.terms)) | ||
|
||
|
@@ -473,7 +476,7 @@ function (::Type{ModelMatrix{T}}){T<:ModelMatrixContainer}(mf::ModelFrame) | |
## and storing as necessary) | ||
for (et, nr) in zip(eterms, non_redundants) | ||
if ! haskey(eterm_cols, (et, nr)) | ||
eterm_cols[(et, nr)] = modelmat_cols(et, mf, non_redundant=nr) | ||
eterm_cols[(et, nr)] = modelmat_cols(T, et, mf, non_redundant=nr) | ||
end | ||
push!(term_cols, eterm_cols[(et, nr)]) | ||
end | ||
|
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.
Since this function is only used in
modelmat_cols
, probably better remove it altogether. You can callreshape(a, size(a, 1), size(a, 2)))
instead, which works for both vectors and matrices.