From e829c7e548481f0347b57bc5946f758e098d9f7f Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Mon, 29 Dec 2025 09:43:30 +0100 Subject: [PATCH 1/2] Adjust show method signature for CoefTable to include MIME"text/plain" The output is decorated so it shouldn't overload the two argument show method. Also, have struct definition and show methods next to eachother in the source file. --- src/statmodels.jl | 108 ++++++++++++++++++++++----------------------- test/statmodels.jl | 6 +-- 2 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/statmodels.jl b/src/statmodels.jl index 291b6e2e7..b487a30e3 100644 --- a/src/statmodels.jl +++ b/src/statmodels.jl @@ -1,56 +1,3 @@ - -## coefficient tables with specialized show method - -mutable struct CoefTable - cols::Vector - colnms::Vector - rownms::Vector - pvalcol::Int - teststatcol::Int - function CoefTable(cols::Vector,colnms::Vector,rownms::Vector, - pvalcol::Int=0,teststatcol::Int=0) - nc = length(cols) - nrs = map(length,cols) - nr = nrs[1] - length(colnms) in [0,nc] || throw(ArgumentError("colnms should have length 0 or $nc")) - length(rownms) in [0,nr] || throw(ArgumentError("rownms should have length 0 or $nr")) - all(nrs .== nr) || throw(ArgumentError("Elements of cols should have equal lengths, but got $nrs")) - pvalcol in 0:nc || throw(ArgumentError("pvalcol should be between 0 and $nc")) - teststatcol in 0:nc || throw(ArgumentError("teststatcol should be between 0 and $nc")) - new(cols,colnms,rownms,pvalcol,teststatcol) - end - - function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector, - pvalcol::Int=0,teststatcol::Int=0) - nc = size(mat,2) - cols = Any[mat[:, i] for i in 1:nc] - CoefTable(cols,colnms,rownms,pvalcol,teststatcol) - end -end - -Base.length(ct::CoefTable) = length(ct.cols[1]) -function Base.eltype(ct::CoefTable) - names = isempty(ct.rownms) ? - tuple(Symbol.(ct.colnms)...) : - tuple(Symbol("Name"), Symbol.(ct.colnms)...) - types = isempty(ct.rownms) ? - Tuple{eltype.(ct.cols)...} : - Tuple{eltype(ct.rownms), eltype.(ct.cols)...} - NamedTuple{names, types} -end - -function Base.iterate(ct::CoefTable, i::Integer=1) - if i in 1:length(ct) - cols = getindex.(ct.cols, Ref(i)) - nt = isempty(ct.rownms) ? - eltype(ct)(tuple(cols...)) : - eltype(ct)(tuple(ct.rownms[i], cols...)) - (nt, i+1) - else - nothing - end -end - """ Show a p-value using 6 characters, either using the standard 0.XXXX representation or as Date: Tue, 30 Dec 2025 17:34:58 +0100 Subject: [PATCH 2/2] Make CoefTable immutable It was probably introduced before immutable structs were a thing and mutations shouldn't really be needed. --- src/statmodels.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/statmodels.jl b/src/statmodels.jl index b487a30e3..11fc136d6 100644 --- a/src/statmodels.jl +++ b/src/statmodels.jl @@ -63,7 +63,7 @@ show(io::IO, n::NoQuote) = print(io, n.s) ## coefficient tables with specialized show method -mutable struct CoefTable +struct CoefTable cols::Vector colnms::Vector rownms::Vector