Get modelmatrix columns indices by Term
Is it possible to add to API some like this:
function term_n_to_modelmatrix_col(mm::ModelMatrix, i::Int)
inds = findall(x -> x==i, mm.assign)
end
Generally I try to make function that can get me columns of modelmatrix by known Term.
for example:
t = Term(:someterm)
f = eval(:(Term(:somevar) ~ ConstantTerm(1) + t))
mf = ModelFrame(f, ds) #ds known dataframe
mx = modelmatrix(mf)
Now how to get indeces of modelmatrix if I have only t ? Or just how to get Term from mf.f by t ?
Thx!
the ModelMatrix struct is planned to be deprecated in the next breaking release, so I don't think it's likely that we'd add an API that depends on it. But it's pretty straightforward to implement something similar. There are internal/non-exported functions (which are hence NOT subject to semver and may break or be removed at any time) like asgn (wraps width to compute the assign field) and symequal (which will tell you whether two terms are equal in terms of the set of columns they refer to, which you can use to find i above) that make it pretty easy. There's still a bit of juggling to do because teh RHS may be wrapped in a MatrixTerm but dealing with that is left as an exercise to the reader :)
Also, in your example, you don't need eval if all you're doing is adding Term(:somevar)!
ubject to semver and may break or be removed at any time) like
asgn(wrapswidthto compute theassignfield) andsymequal(which will tell you whether two terms are equal in terms of the set of columns they refer to, which you can use to findiabove) that make it pretty easy. There's still a bit of juggling to do because teh RHS may be wrapped in aMatrixTermbut dealing with that is left as an exercise to the reader :)
Thank you very much! I will try to use this functions. Is any roadmap or some vision about future of StatsModels?
@kleinschmidt , I thing using asgn with ModelFrame is not good with this: length(StatsModels.asgn(mf.f.rhs.terms[i])) is any other method to do this?
function lcontrast(mf::ModelFrame, n::Int)
terms = mf.f.rhs.terms
len = length(mf.f.rhs.terms)
w = zeros(Int, len)
for i = 1:len
w[i] = length(StatsModels.asgn(mf.f.rhs.terms[i]))
end
if n == 1
s = 0
else
s = sum(w[1:n-1])
end
mat = zeros(w[n], sum(w))
for i = 1:w[n]
mat[i, s+i] = 1
end
mat
end
the
ModelMatrixstruct is planned to be deprecated in the next breaking release
Is it possible to include 'termsyms' and 'termvars' to public API?