diff --git a/.github/workflows/Documenter.yml b/.github/workflows/Documenter.yml index 8fe81c1..9d392c4 100644 --- a/.github/workflows/Documenter.yml +++ b/.github/workflows/Documenter.yml @@ -22,7 +22,7 @@ jobs: Documenter: name: Documentation runs-on: ubuntu-latest - timeout-minutes: 40 + timeout-minutes: 45 steps: - uses: actions/checkout@v3 - uses: julia-actions/setup-julia@v1 diff --git a/Project.toml b/Project.toml index c72f6ad..c8b5cfd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MetidaFreq" uuid = "bd16ee1e-1b2f-4f89-b253-604a522f8c5f" authors = ["PharmCat "] -version = "0.1.5" +version = "0.2.0" [deps] @@ -18,11 +18,11 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] CategoricalArrays = "0.8, 0.9, 0.10" -Distributions = "0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25" +Distributions = "0.20, 0.21, 0.22, 0.23, 0.24, 0.25" HypothesisTests = "0.10, 0.11" MetidaBase = "0.11, 0.12" Optim = "1" -Roots = "1, 2" +Roots = "2" StatsBase = "0.30, 0.31, 0.32, 0.33, 0.34" Tables = "1" julia = "1" diff --git a/docs/Project.toml b/docs/Project.toml index b2e85d5..26c1ca7 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,10 +10,9 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] -Documenter = "≥0.26" -MetidaFreq = "0.1" -HypothesisTests = "0.10" -CSV = "0.7, 0.8, 0.9, 0.10" +Documenter = "1" +HypothesisTests = "0.10, 0.11" +CSV = "0.9, 0.10" DataFrames = "1" PrettyTables = "2" diff --git a/docs/src/api.md b/docs/src/api.md index 9b3c86d..0b201bd 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -12,6 +12,10 @@ MetidaFreq.contab MetidaFreq.freq ``` +### MetidaFreq.freqdict +```@docs +MetidaFreq.freqdict +``` ## Contingency tables utilities @@ -94,6 +98,12 @@ MetidaFreq.metapropfixed MetidaFreq.metaproprandom ``` +## Base + +### Base.getindex +```@docs +getindex +``` ## HypothesisTests @@ -111,3 +121,10 @@ HypothesisTests.MultinomialLRTest ```@docs HypothesisTests.FisherExactTest ``` + +## Experimental + +### MetidaFreq.oddsci +```@docs +MetidaFreq.oddsci +``` diff --git a/src/freq.jl b/src/freq.jl index 75bd7a9..22fba7a 100644 --- a/src/freq.jl +++ b/src/freq.jl @@ -1,13 +1,11 @@ - """ - freq(data, col; id = Dict()) - + freqdict(data::AbstractVector) + +Return frequencies as `Dict`. """ -function freq(data, col; id = Dict()) - if isa(col, String) cols = Symbol(col) else cols = col end - column = Tables.getcolumn(data, cols) - d = Dict{eltype(column), Int}() - for i in column +function freqdict(data::AbstractVector) + d = Dict{eltype(data), Int}() + for i in data ind = ht_keyindex(d, i) if ind > 0 @inbounds d.vals[ind] += one(Int) @@ -15,10 +13,39 @@ function freq(data, col; id = Dict()) @inbounds d[i] = one(Int) end end + d +end + +""" + freq(data, col; id = Dict()) + +Return frequencies as ix1 contingency table. +""" +function freq(data, col; id = Dict()) + if isa(col, String) cols = Symbol(col) else cols = col end + column = Tables.getcolumn(data, cols) + d = freqdict(column) k = collect(keys(d)) - mx = Matrix{Int}(undef, 1, length(k)) + mx = Matrix{Int}(undef, length(k), 1 ) for i = 1:length(k) - @inbounds mx[1, i] = d[k[i]] + @inbounds mx[i, 1] = d[k[i]] end - ConTab(mx, [string(col)], string.(k), id) + ConTab(mx, string.(k), [string(col)], id) end + +""" + freq(data::AbstractVector; id = Dict()) + +Return frequencies as ix1 contingency table. +""" +function freq(data::AbstractVector; id = Dict()) + d = freqdict(data) + k = collect(keys(d)) + mx = Matrix{Int}(undef, length(k), 1) + for i = 1:length(k) + @inbounds mx[i, 1] = d[k[i]] + end + ConTab(mx, string.(k), [""], id) +end + + diff --git a/test/tests.jl b/test/tests.jl index ab33740..4c9d496 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -307,8 +307,17 @@ end ct = MetidaFreq.freq(freqdat, :row) @test ct.tab[1,1] == 26 - @test ct.tab[1,2] == 17 - @test size(ct) == (1,2) + @test ct.tab[2,1] == 17 + @test size(ct) == (2,1) + + ct = MetidaFreq.freq(freqdat[!, :row]) + @test ct.tab[1,1] == 26 + @test ct.tab[2,1] == 17 + @test size(ct) == (2,1) + + ctd = MetidaFreq.freqdict(freqdat[!, :row]) + @test ctd["w"] == 26 + @test ctd["q"] == 17 ct = MetidaFreq.contab(freqdat, :row, :col) @test ct.tab[1,1] == 21