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

Better missing handling #295

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/contrasts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,13 @@ function termnames(C::AbstractContrasts, levels::AbstractVector, baseind::Intege
levels[not_base]
end

Base.getindex(contrasts::ContrastsMatrix, rowinds, colinds) =
getindex(contrasts.matrix, getindex.(Ref(contrasts.invindex), rowinds), colinds)
function Base.getindex(contrasts::ContrastsMatrix{C,T}, rowinds, colinds) where {C,T}
# allow rows to be missing
rows = get.(Ref(contrasts.invindex), rowinds, missing)
# create a row of nothing but missings for missing values
mrow = reduce(vcat, [missing for c in getindex(contrasts.matrix, 1, colinds)])
vcat([r === missing ? mrow : getindex(contrasts.matrix, r, colinds) for r in rows])
end

# Making a contrast type T only requires that there be a method for
# contrasts_matrix(T, baseind, n) and optionally termnames(T, levels, baseind)
Expand Down
6 changes: 3 additions & 3 deletions src/modelframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ function ModelFrame(f::FormulaTerm, data::ColumnTable;
throw(ArgumentError(msg))
end

data, _ = missing_omit(data, f)

sch = schema(f, data, contrasts)
f = apply_schema(f, sch, M)


data, _ = missing_omit(data, f)

ModelFrame(f, sch, data, model)
end

Expand Down
2 changes: 2 additions & 0 deletions src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ concrete_term(t::Term, x, hint::AbstractTerm) = hint
concrete_term(t, d, hint) = t

concrete_term(t::Term, xs::AbstractVector{<:Number}, ::Nothing) = concrete_term(t, xs, ContinuousTerm)
# and for missing values
concrete_term(t::Term, xs::AbstractVector{Union{Missing,T}} where T<:Number, ::Nothing) = concrete_term(t, xs, ContinuousTerm)
function concrete_term(t::Term, xs::AbstractVector, ::Type{ContinuousTerm})
μ, σ2 = StatsBase.mean_and_var(xs)
min, max = extrema(xs)
Expand Down
7 changes: 7 additions & 0 deletions src/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,13 @@ lazy_modelcols(x, d) = modelcols(x, d)



# this is weird, but using import Base: copy leads to exporting type piracy
# for non missing values, the compiler should hopefully optimize down the extra
# layer of indirection
function copy end
copy(x::Any) = Base.copy(x)
copy(m::Missing) = m

modelcols(t::ContinuousTerm, d::NamedTuple) = copy.(d[t.sym])

modelcols(t::CategoricalTerm, d::NamedTuple) = t.contrasts[d[t.sym], :]
Expand Down
11 changes: 11 additions & 0 deletions test/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ StatsModels.apply_schema(mt::MultiTerm, sch::StatsModels.Schema, Mod::Type) =
@test t0.min == 1.0
@test t0.max == 3.0

vals0m = [3, missing, 1]
t0m = concrete_term(t, vals0m)
@test string(t0m) == "aaa"
@test mimestring(t0m) == "aaa(continuous)"
# compute all these values to make sure the behavior of terms matches
# the behavior of other relevant packages
@test isequal(t0m.mean, mean(vals0m))
@test isequal(t0m.var, var(vals0m))
@test isequal(t0m.min, min(vals0m...))
@test isequal(t0m.max, max(vals0m...))

t1 = concrete_term(t, [:a, :b, :c])
@test t1.contrasts isa StatsModels.ContrastsMatrix{DummyCoding}
@test string(t1) == "aaa"
Expand Down