Skip to content
Merged
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
108 changes: 54 additions & 54 deletions src/statmodels.jl
Original file line number Diff line number Diff line change
@@ -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 <Xe-YY.
Expand Down Expand Up @@ -113,7 +60,60 @@ end

show(io::IO, n::NoQuote) = print(io, n.s)

function show(io::IO, ct::CoefTable)

## 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

function show(io::IO, ::MIME"text/plain", ct::CoefTable)
cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms;
nc = length(cols)
nr = length(cols[1])
Expand Down
6 changes: 3 additions & 3 deletions test/statmodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ct_noname = CoefTable(Any[v1, v2, v3, v4, v5],
["Estimate", "Comments", "df", "t", "p"],
[], 5, 4)

@test sprint(show, ct) == """
@test sprint(show, "text/plain", ct) == """
───────────────────────────────────────────────
Estimate Comments df t p
───────────────────────────────────────────────
Expand All @@ -23,7 +23,7 @@ x2 -23.14 Great 56 0.13 0.3467
x3 1.56734e-13 Bad 2 0.00 <1e-15
───────────────────────────────────────────────"""

@test sprint(show, ct_noname) == """
@test sprint(show, "text/plain", ct_noname) == """
────────────────────────────────────────────────
Estimate Comments df t p
────────────────────────────────────────────────
Expand Down Expand Up @@ -61,7 +61,7 @@ m = [0.11258244478647295 0.05664544616214151 0.38181274408522614 0.8197779704
0.36831406658084287 0.12078054506961555 0.8151038332483567 0.6699313951612162
0.3444540231363058 0.17957407667101322 0.2422083248151139 0.4530583319523316]
ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4)
@test sprint(show, ct) == """
@test sprint(show, "text/plain", ct) == """
──────────────────────────────────────────
Estimate Stderror df p
──────────────────────────────────────────
Expand Down
Loading