From 49da97d4d9ceef37b801dafccf1e4cf3751a8af0 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 21 Jan 2026 22:45:00 -0500 Subject: [PATCH] ContrastsMatrix constructor accepts AbstractVector (including CategoricalArray) --- Project.toml | 2 +- src/contrasts.jl | 6 +++--- test/contrasts.jl | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 98151948..57d5ad8f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "StatsModels" uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d" -version = "0.7.8" +version = "0.7.9" [deps] DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" diff --git a/src/contrasts.jl b/src/contrasts.jl index 58f65757..13614674 100644 --- a/src/contrasts.jl +++ b/src/contrasts.jl @@ -108,12 +108,12 @@ struct ContrastsMatrix{C <: AbstractContrasts, M <: AbstractMatrix, T, U} contrasts::C invindex::Dict{T,Int} function ContrastsMatrix(matrix::M, - coefnames::Vector{U}, - levels::Vector{T}, + coefnames::AbstractVector{U}, + levels::AbstractVector{T}, contrasts::C) where {U, T, C <: AbstractContrasts, M <: AbstractMatrix} allunique(levels) || throw(ArgumentError("levels must be all unique, got $(levels)")) invindex = Dict{T,Int}(x=>i for (i,x) in enumerate(levels)) - new{C,M,T,U}(matrix, coefnames, levels, contrasts, invindex) + new{C,M,T,U}(matrix, convert(Vector{U}, coefnames), convert(Vector{T}, levels), contrasts, invindex) end end diff --git a/test/contrasts.jl b/test/contrasts.jl index 62df32fd..37fca516 100644 --- a/test/contrasts.jl +++ b/test/contrasts.jl @@ -418,4 +418,18 @@ @test issetequal(cm.levels, [true, false]) end + + @testset "FullDummy and AbstractVector (#337)" begin + cm = ContrastsMatrix(StatsModels.FullDummyCoding(), categorical([1, 2, 3])) + @test cm.levels == cm.coefnames == categorical([1, 2, 3]) + @test cm.levels isa Vector{<:CategoricalValue} + @test cm.coefnames isa Vector{<:CategoricalValue} + @test cm.matrix == I(3) + + cm = ContrastsMatrix(StatsModels.FullDummyCoding(), view([1 2 3], 1, 1:3)) + @test cm.levels == cm.coefnames == [1, 2, 3] + @test cm.levels isa Vector{Int} + @test cm.coefnames isa Vector{Int} + @test cm.matrix == I(3) + end end