Skip to content

Commit

Permalink
Merge pull request #3 from PharmCat/dev
Browse files Browse the repository at this point in the history
update 0.1.2
  • Loading branch information
PharmCat authored Mar 16, 2023
2 parents ae33ebc + 0498a52 commit 1c249dc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ 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]
Documenter = "≥0.25"
Documenter = "≥0.26"
MetidaFreq = "0.1"
HypothesisTests = "0.10"
CSV = "0.7, 0.8"
DataFrames = "0.22, 1"
PrettyTables = "1"
Weave = "0.9, 0.10"
Plots = "1"
CSV = "0.7, 0.8, 0.9, 0.10"
DataFrames = "1"
PrettyTables = "2"

2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Documenter, MetidaFreq, Weave, PrettyTables, CSV, DataFrames, HypothesisTests
using Documenter, MetidaFreq, PrettyTables, CSV, DataFrames, HypothesisTests
#using DocumenterLaTeX


Expand Down
5 changes: 5 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ MetidaFreq.freq
MetidaFreq.addcol
```

### MetidaFreq.colorder
```@docs
MetidaFreq.colorder
```

### MetidaFreq.colreduce
```@docs
MetidaFreq.colreduce
Expand Down
2 changes: 1 addition & 1 deletion src/MetidaFreq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 67 additions & 3 deletions src/contab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -296,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
Expand Down

2 comments on commit 1c249dc

@PharmCat
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82286

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.2 -m "<description of version>" 1c249dc2096da9c8fdbc24ad63a1c62ee2a27e00
git push origin v0.1.2

Please sign in to comment.