-
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
Add compatibility with pre-contrasts ModelFrame constructor #1042
Add compatibility with pre-contrasts ModelFrame constructor #1042
Conversation
LGTM |
Just curious, why do you need to do that? @kleinschmidt You're the most qualified to review this now that you've rewritten most of that code, so feel free to merge after leaving a chance for others to comment. |
Not sure why FixedEffectModels needs the raw constructor specifically, @matthieugomez would be the one to ask. My guess would be that it's to support explicitly specifying a subset of the input dataframe rows for use in the model? |
I do need to remove more observations than what Tbh, I'm a little bit confused about what I did and what has changed here, so I'm not sure if it's exact reason. |
df, msng = na_omit(DataFrame(map(x -> d[x], trms.eterms))) | ||
names!(df, convert(Vector{Symbol}, map(string, trms.eterms))) | ||
for c in eachcol(df) dropunusedlevels!(c[2]) end | ||
function evalcontrasts(df::AbstractDataFrame, contrasts::Dict = Dict()) | ||
|
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.
Remove empty line at top of function.
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.
And BTW, move the comments below before the signature, as they clearly document the function.
@matthieugomez When you'll have the time to look at this more deeply, it would be good to find a higher-level interface which would work for your use case. With the recent changes, we support arbitrary contrasts (not just dummy coding), but your custom evaluation function probably doesn't. |
I used this constructor because I already had a dataframe without missing values after doing a bunch of operations (in particular excluding obs with missing weights or missing variables used to compute standard errors). |
If that's the only problem, then we can probably change this line (or |
Honestly it's ok. There will probably be a better fix someday (using subdataframes). A temporary fix based on the number of missing values would just complicate the function ( |
Sorry: I actually do need a way to specify a function ModelFrame(trms::Terms, d::AbstractDataFrame; kwargs...)
select = complete_cases(DataFrame(map(x -> d[x], trms.eterms)))
ModelFrame(trms, d, select; kwargs...)
end
function ModelFrame(trms::Terms, d::AbstractDataFrame, select::AbstractVector; contrasts::Dict = Dict())
df = DataFrame(map(x -> d[select, :x], trms.eterms))
names!(df, convert(Vector{Symbol}, map(string, trms.eterms)))
for c in eachcol(df) dropunusedlevels!(c[2]) end
## Set up contrasts:
## Combine actual DF columns and contrast types if necessary to compute the
## actual contrasts matrices, levels, and term names (using DummyCoding
## as the default)
evaledContrasts = Dict()
for (term, col) in eachcol(df)
is_categorical(col) || continue
evaledContrasts[term] = ContrastsMatrix(haskey(contrasts, term) ?
contrasts[term] :
DEFAULT_CONTRASTS(),
col)
end
## Check for non-redundant terms, modifying terms in place
check_non_redundancy!(trms, df)
ModelFrame(df, trms, select, evaledContrasts)
end |
Yeah, makes sense as a public API. Please make a PR (with tests for the new method). Though I'm not sure Also, would this replace or complement the present PR? |
Sorry I'm not familiar with the package, why isn't this efficient? DataFrame(map(x -> d[x], trms.eterms)) |
@GordStephen Thanks. Unfortunately, recent changes on master have introduced conflicts. Could you rebase? (I would have done it myself, but I don't have the rights to push to your branch...) @matthieugomez I'm not sure it really inefficient, but if the simpler |
82e0958
to
88def09
Compare
88def09
to
a444fd6
Compare
Ah, sorry. I'll check that "Allow edits from maintainers" box. Rebased now. |
Thanks! |
For reference, I ended up using the basic ModelFrame function, replacing On Wed, Sep 14, 2016 at 4:13 AM, Milan Bouchet-Valat <
|
Can you make a PR to add this method? You shouldn't have to carry things like that in your package. |
…iaData#1042) Add compatibility with pre-contrasts ModelFrame constructor
Add compatibility with pre-contrasts ModelFrame constructor (cherry picked from commit 968e980)
Some downstream packages (like FixedEffectModels) call the direct
ModelFrame
constructor rather than use the outer constructor functions. This PR provides compatibility with the pre-#870 signature for that constructor method.