From 05516d90237599d478650632aaf5d6f8cf6dd10b Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Fri, 26 May 2023 17:41:04 -0400 Subject: [PATCH 1/5] test --- test/modelmatrix.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/modelmatrix.jl b/test/modelmatrix.jl index efa1ff2f..ddcb693c 100644 --- a/test/modelmatrix.jl +++ b/test/modelmatrix.jl @@ -402,5 +402,27 @@ f = apply_schema(@formula(0 ~ a&b&c), schema(t)) @test vec(modelcols(f.rhs, t)) == modelcols.(Ref(f.rhs), Tables.rowtable(t)) end + + @testset "#293 conversion of Float32s" begin + d = (; y=rand(Float32, 4), x=rand(Float32, 4), z=[1:4; ]) + f = @formula(y ~ 1 + x + log(x) * z) + y, x = modelcols(apply_schema(f, schema(d)), d) + @test eltype(y) == eltype(x) == Float32 + + contr = DummyCoding(; base=4, levels=1:4) + d = (; y=rand(Float32, 4), x=rand(Float32, 4), z=[1:4; ]) + + # currently this is the best way to construct contrasts with Float32s... + dummy_cmat = Float32[1 0 0 + 0 1 0 + 0 0 1 + 0 0 0] + contr = StatsModels.ContrastsCoding(dummy_cmat, [1:4;]) + + sch = schema(f, d, Dict(:z => contr)) + y, x = modelcols(apply_schema(f, sch), d) + @test size(x) == (4, 1 + 1 + 1 + 3 + 3) + @test eltype(x) == eltype(y) == Float32 + end end From 995146eac39b8f30e64fc26b36528653fbc00f04 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Fri, 26 May 2023 17:42:16 -0400 Subject: [PATCH 2/5] with no intercept --- test/modelmatrix.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/modelmatrix.jl b/test/modelmatrix.jl index ddcb693c..d873cee5 100644 --- a/test/modelmatrix.jl +++ b/test/modelmatrix.jl @@ -409,6 +409,10 @@ y, x = modelcols(apply_schema(f, schema(d)), d) @test eltype(y) == eltype(x) == Float32 + f0 = @formula(y ~ 0 + x + log(x) * z) + y, x = modelcols(apply_schema(f0, schema(d)), d) + @test eltype(y) == eltype(x) == Float32 + contr = DummyCoding(; base=4, levels=1:4) d = (; y=rand(Float32, 4), x=rand(Float32, 4), z=[1:4; ]) From a90e5122740bd9e088179d6115ed5268b1038969 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Fri, 26 May 2023 17:54:14 -0400 Subject: [PATCH 3/5] bool --- src/terms.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/terms.jl b/src/terms.jl index f9a89c17..42f12264 100644 --- a/src/terms.jl +++ b/src/terms.jl @@ -547,8 +547,10 @@ function modelcols(t::InteractionTerm, d::ColumnTable) row_kron_insideout(*, (modelcols(term, d) for term in t.terms)...) end -modelcols(t::InterceptTerm{true}, d::NamedTuple) = ones(size(first(d))) -modelcols(t::InterceptTerm{false}, d) = Matrix{Float64}(undef, size(first(d),1), 0) +# use Bool as the eltype here to avoid promoting e.g. Float32s in other columns +# to Float64 unless it's really necessary +modelcols(t::InterceptTerm{true}, d::NamedTuple) = ones(Bool, size(first(d))) +modelcols(t::InterceptTerm{false}, d) = Matrix{Bool}(undef, size(first(d),1), 0) modelcols(t::FormulaTerm, d::NamedTuple) = (modelcols(t.lhs,d), modelcols(t.rhs, d)) From 00915d7900d006e95e74f15d5a0cdc030654eda2 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Fri, 26 May 2023 17:55:14 -0400 Subject: [PATCH 4/5] test this behavior too --- test/modelmatrix.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/modelmatrix.jl b/test/modelmatrix.jl index d873cee5..dde34881 100644 --- a/test/modelmatrix.jl +++ b/test/modelmatrix.jl @@ -413,6 +413,10 @@ y, x = modelcols(apply_schema(f0, schema(d)), d) @test eltype(y) == eltype(x) == Float32 + fint = @formula(y ~ 1 + z) + y, x = modelcols(apply_schema(fint, schema(d)), d) + @test eltype(x) == Int + contr = DummyCoding(; base=4, levels=1:4) d = (; y=rand(Float32, 4), x=rand(Float32, 4), z=[1:4; ]) From 9e1fae63cdd0ce027ffd05e4e7ae6ef5ce95e2b3 Mon Sep 17 00:00:00 2001 From: Dave Kleinschmidt Date: Fri, 26 May 2023 18:21:28 -0400 Subject: [PATCH 5/5] unused --- test/modelmatrix.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/modelmatrix.jl b/test/modelmatrix.jl index dde34881..d0f95e86 100644 --- a/test/modelmatrix.jl +++ b/test/modelmatrix.jl @@ -417,9 +417,6 @@ y, x = modelcols(apply_schema(fint, schema(d)), d) @test eltype(x) == Int - contr = DummyCoding(; base=4, levels=1:4) - d = (; y=rand(Float32, 4), x=rand(Float32, 4), z=[1:4; ]) - # currently this is the best way to construct contrasts with Float32s... dummy_cmat = Float32[1 0 0 0 1 0