Skip to content

Commit

Permalink
Merge 7c69f5f into 14f3b54
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinschmidt committed Aug 1, 2019
2 parents 14f3b54 + 7c69f5f commit c77878f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
25 changes: 12 additions & 13 deletions test/modelmatrix.jl
Expand Up @@ -6,13 +6,12 @@

sparsetype = SparseMatrixCSC{Float64,Int}

d = DataFrame()
d[:y] = [1:4;]
d[:x1] = [5:8;]
d[:x2] = [9:12;]
d[:x3] = [13:16;]
d[:x4] = [17:20;]
d[:x1p] = CategoricalArray(d[:x1])
d = DataFrame(y = 1:4,
x1 = 5:8,
x2 = 9:12,
x3 = 13:16,
x4 = 17:20)
d.x1p = categorical(d.x1)

d_orig = deepcopy(d)

Expand Down Expand Up @@ -64,7 +63,7 @@
@test mm.m == [ones(4) x1 x2 x1.*x2]
@test mm.m == ModelMatrix{sparsetype}(mf).m

d[:x1] = CategoricalArray(x1)
d.x1 = categorical(x1)
x1e = [[0, 1, 0, 0] [0, 0, 1, 0] [0, 0, 0, 1]]
f = @formula(y ~ 1 + x1 * x2)
mf = ModelFrame(f, d)
Expand All @@ -81,7 +80,7 @@
@test response(mf) == y''

d = deepcopy(d_orig)
d[:x1] = CategoricalArray{Union{Missing, Float64}}(d[:x1])
d.x1 = CategoricalArray{Union{Missing, Float64}}(d.x1)

f = @formula(y ~ 1 + x2 + x3 + x3*x2)
mm = ModelMatrix(ModelFrame(f, d))
Expand Down Expand Up @@ -144,7 +143,7 @@
@test size(mm_sub) == (3,3)

## Missing data
d[:x1m] = [5, 6, missing, 7]
d.x1m = [5, 6, missing, 7]
mf = ModelFrame(@formula(y ~ 1 + x1m), d)
mm = ModelMatrix(mf)
@test mm.m[:, 2] == d[completecases(d), :x1m]
Expand All @@ -162,7 +161,7 @@
z = repeat([:e, :f], inner = 4))
categorical!(d)
cs = Dict([Pair(name, EffectsCoding()) for name in names(d)])
d[:n] = 1.:8
d.n = 1.:8


## No intercept
Expand All @@ -182,7 +181,7 @@
## promotion blocked when we block default model=StatisticalModel
mf = ModelFrame(@formula(n ~ 0 + x), d, model=Nothing, contrasts=cs)
mm = ModelMatrix(mf)
@test all(mm.m .== ifelse.(d[:x] .== :a, -1, 1))
@test all(mm.m .== ifelse.(d.x .== :a, -1, 1))
@test coefnames(mf) == ["x: b"]


Expand Down Expand Up @@ -315,7 +314,7 @@
@test coefnames(mf) == ["(Intercept)", "1 | x"]

mf = ModelFrame(@formula(y ~ 0 + (1 | x)), d)
@test all(ModelMatrix(mf).m .== float.(1 .| d[:x]))
@test all(ModelMatrix(mf).m .== float.(1 .| d.x))
@test coefnames(mf) == ["1 | x"]
end

Expand Down
24 changes: 12 additions & 12 deletions test/statsmodel.jl
Expand Up @@ -53,24 +53,24 @@ Base.show(io::IO, m::DummyModTwo) = println(io, m.msg)
@testset "stat model types" begin

## Test fitting
d = DataFrame()
d[:y] = [1:4;]
d[:x1] = Vector{Union{Missing, Int}}(5:8)
d[:x2] = [9:12;]
d[:x3] = [13:16;]
d[:x4] = [17:20;]
d[:x1p] = CategoricalArray{Union{Missing, Int}}(d[:x1])
d = DataFrame(y = 1:4,
x1 = allowmissing(5:8),
x2 = 9:12,
x3 = 13:16,
x4 = 17:20)

d.x1p = categorical(d.x1)

f = @formula(y ~ x1 * x2)
m = fit(DummyMod, f, d)
@test response(m) == Array(d[:y])
@test response(m) == Array(d.y)

## coefnames delegated to model frame by default
@test coefnames(m) == coefnames(ModelFrame(f, d)) == ["(Intercept)", "x1", "x2", "x1 & x2"]

## test prediction method
## vanilla
@test predict(m) == [ ones(size(d,1)) Array(d[:x1]) Array(d[:x2]) Array(d[:x1]).*Array(d[:x2]) ] * collect(1:4)
@test predict(m) == [ ones(size(d,1)) Array(d.x1) Array(d.x2) Array(d.x1).*Array(d.x2) ] * collect(1:4)

## new data from matrix
mm = ModelMatrix(ModelFrame(f, d))
Expand Down Expand Up @@ -103,7 +103,7 @@ Base.show(io::IO, m::DummyModTwo) = println(io, m.msg)
## predict w/ new data with _extra_ levels (throws an error)
d3 = deepcopy(d)
d3[1, :x1] = 0
d3[:x1p] = CategoricalVector{Union{Missing, Int}}(d3[:x1])
d3.x1p = categorical(d3.x1)
# TODO: check for level mismatch earlier...this throws a KeyError when it
# goes to do the lookup in the contrasts matrix from the previously
# generated categorical term.
Expand All @@ -112,14 +112,14 @@ Base.show(io::IO, m::DummyModTwo) = println(io, m.msg)

## predict with dataframe that doesn't have the dependent variable
d4 = deepcopy(d)
deletecols!(d4, [:y])
select!(d4, Not(:y))
@test predict(m, d4) == predict(m, d)

## attempting to fit with d4 should fail since it doesn't have :y
@test_throws ErrorException fit(DummyMod, f, d4)

## fit with contrasts specified
d[:x2p] = CategoricalVector{Union{Missing, Int}}(d[:x2])
d.x2p = categorical(d.x2)
f3 = @formula(y ~ x1p + x2p)
m3 = fit(DummyMod, f3, d)
fit(DummyMod, f3, d, contrasts = Dict(:x1p => EffectsCoding()))
Expand Down

0 comments on commit c77878f

Please sign in to comment.