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

Get modelmatrix columns indices by Term #228

Open
PharmCat opened this issue Jul 23, 2021 · 4 comments
Open

Get modelmatrix columns indices by Term #228

PharmCat opened this issue Jul 23, 2021 · 4 comments

Comments

@PharmCat
Copy link

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!

@kleinschmidt
Copy link
Member

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)!

@PharmCat
Copy link
Author

ubject 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 :)

Thank you very much! I will try to use this functions. Is any roadmap or some vision about future of StatsModels?

@PharmCat
Copy link
Author

PharmCat commented May 20, 2022

@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

@PharmCat
Copy link
Author

the ModelMatrix struct is planned to be deprecated in the next breaking release

Is it possible to include 'termsyms' and 'termvars' to public API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants