|
1 | | - |
2 | | -## coefficient tables with specialized show method |
3 | | - |
4 | | -mutable struct CoefTable |
5 | | - cols::Vector |
6 | | - colnms::Vector |
7 | | - rownms::Vector |
8 | | - pvalcol::Int |
9 | | - teststatcol::Int |
10 | | - function CoefTable(cols::Vector,colnms::Vector,rownms::Vector, |
11 | | - pvalcol::Int=0,teststatcol::Int=0) |
12 | | - nc = length(cols) |
13 | | - nrs = map(length,cols) |
14 | | - nr = nrs[1] |
15 | | - length(colnms) in [0,nc] || throw(ArgumentError("colnms should have length 0 or $nc")) |
16 | | - length(rownms) in [0,nr] || throw(ArgumentError("rownms should have length 0 or $nr")) |
17 | | - all(nrs .== nr) || throw(ArgumentError("Elements of cols should have equal lengths, but got $nrs")) |
18 | | - pvalcol in 0:nc || throw(ArgumentError("pvalcol should be between 0 and $nc")) |
19 | | - teststatcol in 0:nc || throw(ArgumentError("teststatcol should be between 0 and $nc")) |
20 | | - new(cols,colnms,rownms,pvalcol,teststatcol) |
21 | | - end |
22 | | - |
23 | | - function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector, |
24 | | - pvalcol::Int=0,teststatcol::Int=0) |
25 | | - nc = size(mat,2) |
26 | | - cols = Any[mat[:, i] for i in 1:nc] |
27 | | - CoefTable(cols,colnms,rownms,pvalcol,teststatcol) |
28 | | - end |
29 | | -end |
30 | | - |
31 | | -Base.length(ct::CoefTable) = length(ct.cols[1]) |
32 | | -function Base.eltype(ct::CoefTable) |
33 | | - names = isempty(ct.rownms) ? |
34 | | - tuple(Symbol.(ct.colnms)...) : |
35 | | - tuple(Symbol("Name"), Symbol.(ct.colnms)...) |
36 | | - types = isempty(ct.rownms) ? |
37 | | - Tuple{eltype.(ct.cols)...} : |
38 | | - Tuple{eltype(ct.rownms), eltype.(ct.cols)...} |
39 | | - NamedTuple{names, types} |
40 | | -end |
41 | | - |
42 | | -function Base.iterate(ct::CoefTable, i::Integer=1) |
43 | | - if i in 1:length(ct) |
44 | | - cols = getindex.(ct.cols, Ref(i)) |
45 | | - nt = isempty(ct.rownms) ? |
46 | | - eltype(ct)(tuple(cols...)) : |
47 | | - eltype(ct)(tuple(ct.rownms[i], cols...)) |
48 | | - (nt, i+1) |
49 | | - else |
50 | | - nothing |
51 | | - end |
52 | | -end |
53 | | - |
54 | 1 | """ |
55 | 2 | Show a p-value using 6 characters, either using the standard 0.XXXX |
56 | 3 | representation or as <Xe-YY. |
|
113 | 60 |
|
114 | 61 | show(io::IO, n::NoQuote) = print(io, n.s) |
115 | 62 |
|
116 | | -function show(io::IO, ct::CoefTable) |
| 63 | + |
| 64 | +## coefficient tables with specialized show method |
| 65 | + |
| 66 | +mutable struct CoefTable |
| 67 | + cols::Vector |
| 68 | + colnms::Vector |
| 69 | + rownms::Vector |
| 70 | + pvalcol::Int |
| 71 | + teststatcol::Int |
| 72 | + function CoefTable(cols::Vector,colnms::Vector,rownms::Vector, |
| 73 | + pvalcol::Int=0,teststatcol::Int=0) |
| 74 | + nc = length(cols) |
| 75 | + nrs = map(length,cols) |
| 76 | + nr = nrs[1] |
| 77 | + length(colnms) in [0,nc] || throw(ArgumentError("colnms should have length 0 or $nc")) |
| 78 | + length(rownms) in [0,nr] || throw(ArgumentError("rownms should have length 0 or $nr")) |
| 79 | + all(nrs .== nr) || throw(ArgumentError("Elements of cols should have equal lengths, but got $nrs")) |
| 80 | + pvalcol in 0:nc || throw(ArgumentError("pvalcol should be between 0 and $nc")) |
| 81 | + teststatcol in 0:nc || throw(ArgumentError("teststatcol should be between 0 and $nc")) |
| 82 | + new(cols,colnms,rownms,pvalcol,teststatcol) |
| 83 | + end |
| 84 | + |
| 85 | + function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector, |
| 86 | + pvalcol::Int=0,teststatcol::Int=0) |
| 87 | + nc = size(mat,2) |
| 88 | + cols = Any[mat[:, i] for i in 1:nc] |
| 89 | + CoefTable(cols,colnms,rownms,pvalcol,teststatcol) |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +Base.length(ct::CoefTable) = length(ct.cols[1]) |
| 94 | +function Base.eltype(ct::CoefTable) |
| 95 | + names = isempty(ct.rownms) ? |
| 96 | + tuple(Symbol.(ct.colnms)...) : |
| 97 | + tuple(Symbol("Name"), Symbol.(ct.colnms)...) |
| 98 | + types = isempty(ct.rownms) ? |
| 99 | + Tuple{eltype.(ct.cols)...} : |
| 100 | + Tuple{eltype(ct.rownms), eltype.(ct.cols)...} |
| 101 | + NamedTuple{names, types} |
| 102 | +end |
| 103 | + |
| 104 | +function Base.iterate(ct::CoefTable, i::Integer=1) |
| 105 | + if i in 1:length(ct) |
| 106 | + cols = getindex.(ct.cols, Ref(i)) |
| 107 | + nt = isempty(ct.rownms) ? |
| 108 | + eltype(ct)(tuple(cols...)) : |
| 109 | + eltype(ct)(tuple(ct.rownms[i], cols...)) |
| 110 | + (nt, i+1) |
| 111 | + else |
| 112 | + nothing |
| 113 | + end |
| 114 | +end |
| 115 | + |
| 116 | +function show(io::IO, ::MIME"text/plain", ct::CoefTable) |
117 | 117 | cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms; |
118 | 118 | nc = length(cols) |
119 | 119 | nr = length(cols[1]) |
|
0 commit comments