From deb1138a3d98ab042b301c8de104fc9b1ae07f95 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Thu, 16 Mar 2023 02:39:22 +0300 Subject: [PATCH 1/5] update --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f82ea32..17751b7 100644 --- a/Project.toml +++ b/Project.toml @@ -20,7 +20,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" CategoricalArrays = "0.8, 0.9, 0.10" Distributions = "0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25" HypothesisTests = "0.10" -MetidaBase = "0.8, 0.9, 0.10, 0.11" +MetidaBase = "0.11" Optim = "1" Roots = "1, 2" StatsBase = "0.27, 0.28, 0.29, 0.30, 0.31, 0.32, 0.33" From 3e5486a46d318061230a3803568318e573f65c07 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:17:58 +0300 Subject: [PATCH 2/5] update --- docs/src/api.md | 5 ++++ src/MetidaFreq.jl | 2 +- src/contab.jl | 68 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 4409017..a0f0303 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -20,6 +20,11 @@ MetidaFreq.freq MetidaFreq.addcol ``` +### MetidaFreq.colorder +```@docs +MetidaFreq.colorder +``` + ### MetidaFreq.colreduce ```@docs MetidaFreq.colreduce diff --git a/src/MetidaFreq.jl b/src/MetidaFreq.jl index 7c02cdd..99f47c6 100644 --- a/src/MetidaFreq.jl +++ b/src/MetidaFreq.jl @@ -15,7 +15,7 @@ import MetidaBase: AbstractData, AbstractIdData, DataSet, Proportion, PrettyTabl import HypothesisTests: ChisqTest, MultinomialLRTest, FisherExactTest import Base: ht_keyindex, size, show, permutedims -export contab, propci, diffci, orci, rrci, confint +export contab, propci, diffci, orci, rrci, confint, colorder, sumrows, addcol, colreduce export metaprop, metapropfixed, metaproprandom diff --git a/src/contab.jl b/src/contab.jl index 0552679..80e12c3 100644 --- a/src/contab.jl +++ b/src/contab.jl @@ -154,12 +154,75 @@ function colreduce(f::Function, data::DataSet{<:ConTab}; coln = nothing) id = Dict()) end +""" + colorder(ct::ConTab, v::AbstractVector{AbstrctString}) + +Make contigency trable with column order as in `v` vector. If item in `v` not present in tab collumn's names - collumn of zeros will be made for that item. + +Example + +``` +Contingency table: +--------- -------- ----- ------- + B D Total +--------- -------- ----- ------- + G1 1 123 124 + G2 0 124 124 +--------- -------- ----- ------- + ID: ColName => id; +``` + +after `colorder(ct, ["A", "B", "C", "D"])`: + +``` +Contingency table: +--------- -------- --------- --------- ----- ------- + A B C D Total +--------- -------- --------- --------- ----- ------- + G1 0 1 0 123 124 + G2 0 0 0 124 124 +--------- -------- --------- --------- ----- ------- +ID: ColName => id; +``` +""" +function colorder(ct::ConTab, v::AbstractVector{<:AbstractString}) + + foreach(x-> if x ∉ v error("Not all col names in new vector: "*x*" ID: "*string(ct.id)) end, ct.coln) + + tab = zeros(Int, size(ct.tab, 1), length(v)) + for i = 1:length(v) + ind = findfirst(x -> x == v[i], ct.coln) + if !isnothing(ind) tab[:, i] .= view(ct.tab, :, ind) end + end + ConTab(tab, ct.rown, v, ct.id) +end + +function colorder(data::DataSet{<:ConTab}, v::AbstractVector{<:AbstractString}) + DataSet(map(x -> colorder(x, v), getdata(data))) +end + +function contab(data, row::Union{Symbol, String}, col; sort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing) + namevec = names(data) + if eltype(col) <: Int icol = namevec[first(col)] else icol = first(col) end + tab = contab(data, row, icol; sort = sort, idp = :ColName => icol) + if isa(tab, ConTab) ds = DataSet([tab]) else ds = tab end + if length(col) > 1 + for i = 2:length(col) + if isa(col[i], Int) icol = namevec[col[i]] else icol = col[i] end + tab = contab(data, row, icol; sort = sort, idp = :ColName => icol) + if isa(tab, ConTab) push!(getdata(ds), tab) else append!(getdata(ds), getdata(tab)) end + end + end + return ds +end """ contab(data, row::Symbol, col::Symbol; sort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing, id = nothing) Make contingency table from data using `row` and `col` columns. """ -function contab(data, row::Symbol, col::Symbol; sort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing, id = nothing) +function contab(data, row::Union{Symbol, String}, col::Union{Symbol, String}; sort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing, idp::Union{Nothing, Pair} = nothing) + if isa(row, String) row = Symbol(row) end + if isa(col, String) col = Symbol(col) end cols = Tables.columns(data) c = isnothing(sort) ? (row, col) : isa(sort, Symbol) ? (row, col, sort) : Tuple(append!([row, col], sort)) res = contab_(Tuple(Tables.getcolumn(cols, y) for y in c)) @@ -187,6 +250,7 @@ function contab(data, row::Symbol, col::Symbol; sort::Union{Nothing, Symbol, Abs for k in keys(cdi) colstr[cdi[k]] = string(k) end + if !isnothing(idp) push!(id, idp) end v[i] = ConTab(Matrix(view(res[1], :, :, ci[i])), rowstr, colstr, Dict(id)) end return DataSet(v) @@ -197,7 +261,7 @@ function contab(data, row::Symbol, col::Symbol; sort::Union{Nothing, Symbol, Abs for k in keys(cdi) colstr[cdi[k]] = string(k) end - return ConTab(res[1], rowstr, colstr, (isnothing(id) ? Dict() : id)) + return ConTab(res[1], rowstr, colstr, (isnothing(idp) ? Dict() : Dict(idp))) end end From 04fb2df25b0067f622f53ae70728d6221832a366 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:25:00 +0300 Subject: [PATCH 3/5] update --- docs/Project.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 4f85324..377075e 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -11,11 +11,11 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" [compat] -Documenter = "≥0.25" +Documenter = "≥0.26" MetidaFreq = "0.1" HypothesisTests = "0.10" -CSV = "0.7, 0.8" -DataFrames = "0.22, 1" -PrettyTables = "1" +CSV = "0.7, 0.8, 0.9, 0.10" +DataFrames = "1" +PrettyTables = "1, 2" Weave = "0.9, 0.10" Plots = "1" From a504ef77b14a3018197feaffbf727dd222b60227 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:38:44 +0300 Subject: [PATCH 4/5] update --- docs/Project.toml | 8 +++----- src/contab.jl | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 377075e..b2e85d5 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -6,8 +6,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" + [compat] @@ -16,6 +15,5 @@ MetidaFreq = "0.1" HypothesisTests = "0.10" CSV = "0.7, 0.8, 0.9, 0.10" DataFrames = "1" -PrettyTables = "1, 2" -Weave = "0.9, 0.10" -Plots = "1" +PrettyTables = "2" + diff --git a/src/contab.jl b/src/contab.jl index 80e12c3..bf94ec5 100644 --- a/src/contab.jl +++ b/src/contab.jl @@ -360,7 +360,7 @@ function Base.show(io::IO, contab::ConTab) println(io, " Contingency table:") tab = hcat(contab.tab, sum(contab.tab, dims = 2)) coln = push!(copy(contab.coln), "Total") - PrettyTables.pretty_table(io, tab; header = coln, row_names = contab.rown, tf = PrettyTables.tf_compact) + PrettyTables.pretty_table(io, tab; header = coln, row_labels = contab.rown, tf = PrettyTables.tf_compact) if !isnothing(contab.id) && length(contab.id) > 0 print(io, " ID: ") for (k,v) in contab.id From 0498a52b8f7bcf652c48099fd95a581e812a05b6 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:45:24 +0300 Subject: [PATCH 5/5] fix --- docs/make.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/make.jl b/docs/make.jl index 9c5286e..cd93d41 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,4 +1,4 @@ -using Documenter, MetidaFreq, Weave, PrettyTables, CSV, DataFrames, HypothesisTests +using Documenter, MetidaFreq, PrettyTables, CSV, DataFrames, HypothesisTests #using DocumenterLaTeX