Skip to content

Commit

Permalink
RFC: Add compatibility with pre-contrasts ModelFrame constructor (Jul…
Browse files Browse the repository at this point in the history
…iaData#1042)

Add compatibility with pre-contrasts ModelFrame constructor
  • Loading branch information
Gord Stephen authored and maximerischard committed Sep 28, 2016
1 parent ada9091 commit e65192d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/statsmodels/formula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,11 @@ end

const DEFAULT_CONTRASTS = DummyCoding

function ModelFrame(trms::Terms, d::AbstractDataFrame;
contrasts::Dict = Dict())
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

## 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)
## 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)
function evalcontrasts(df::AbstractDataFrame, contrasts::Dict = Dict())
evaledContrasts = Dict()
for (term, col) in eachcol(df)
is_categorical(col) || continue
Expand All @@ -303,13 +298,24 @@ function ModelFrame(trms::Terms, d::AbstractDataFrame;
DEFAULT_CONTRASTS(),
col)
end
return evaledContrasts
end

function ModelFrame(trms::Terms, d::AbstractDataFrame;
contrasts::Dict = Dict())
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

evaledContrasts = evalcontrasts(df, contrasts)

## Check for non-redundant terms, modifying terms in place
check_non_redundancy!(trms, df)

ModelFrame(df, trms, msng, evaledContrasts)
end

ModelFrame(df::AbstractDataFrame, term::Terms, msng::BitArray) = ModelFrame(df, term, msng, evalcontrasts(df))
ModelFrame(f::Formula, d::AbstractDataFrame; kwargs...) = ModelFrame(Terms(f), d; kwargs...)
ModelFrame(ex::Expr, d::AbstractDataFrame; kwargs...) = ModelFrame(Formula(ex), d; kwargs...)

Expand Down

0 comments on commit e65192d

Please sign in to comment.