Skip to content

Commit

Permalink
Merge pull request #17 from markgpritchard/v0_2_2
Browse files Browse the repository at this point in the history
V0 2 2
  • Loading branch information
markgpritchard authored Nov 12, 2024
2 parents 22e767b + e4c53dc commit 54a843c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ jobs:
matrix:
version:
- '1.6'
- '1.9'
- '1.10'
- '1.11'
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TableOne"
uuid = "d36ae967-72a8-4ff4-a661-f51e671fe83a"
authors = ["Mark Pritchard <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
54 changes: 19 additions & 35 deletions src/TableOne.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ end

# If no strata supplied, default to showing totals column only
tableone(data, vars::Vector; kwargs...) = tableone(data, nothing, vars; kwargs...)

tableone(data; kwargs...) = tableone(data, nothing; kwargs...)

function tableone(data, strata::Nothing, vars::Vector; addtotal=true, kwargs...)
function tableone(
data, ::Nothing, vars::Vector;
addtotal=true, includemissingintotal=true, kwargs...
)
stratanames = String[ ]
return _tableone(
data, strata, vars, stratanames;
addtotal, includemissingintotal=true, kwargs...
data, nothing, vars, stratanames;
addtotal, includemissingintotal, kwargs...
)
end

Expand Down Expand Up @@ -111,13 +113,11 @@ function _tableone(
ids = findall(!ismissing, getproperty(data, strata))
push!(strataids, :Total => ids)
end

if addtotal
ids = strataids[:Total]
n = length(ids)
insertcols!(table1, :Total => [ "$n" ])
end

if addnmissing
if addtotal && includemissingintotal
# then all records are included in the `Total` column so nothing is missing
Expand Down Expand Up @@ -159,9 +159,7 @@ function _tableone_novars(
end

function _tableone_novars(
data, strata,
binvars::Nothing, catvars::Nothing, npvars::Nothing,
paramvars::Nothing, cramvars::Nothing;
data, strata, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Nothing;
kwargs...
)
alldfvars = Symbol.(names(data))
Expand Down Expand Up @@ -203,9 +201,9 @@ function _tableone_novars(
)
end

__tableone_novars_var(var::Nothing, S) = S[ ]
__tableone_novars_var(var, S) = [ var ]
__tableone_novars_var(var::AbstractVector, S) = var
__tableone_novars_var(::Nothing, S) = S[ ]
__tableone_novars_var(var, ::Any) = [ var ]
__tableone_novars_var(var::AbstractVector, ::Any) = var

# Variables can be listed as Symbols or Strings, but need to be consistent. Functions
# to check this consistency and make sure all lists are vectors
Expand Down Expand Up @@ -330,25 +328,21 @@ function ___catvariable(
ids = strataids[Symbol(sn)]
_catvariable!(_t, varvect, levels, ids, sn, pmatrix, i; kwargs...)
end

if addtotal
ids = strataids[:Total]
_catvariable!(_t, varvect, levels, ids, "Total", nothing, nothing; kwargs...)
end

if addnmissing _addnmissing!(_t, varvect, strataids) end

_addcatpvalue!(_t, pmatrix; kwargs...)
return _t
end

function _catvariable!(_t::DataFrame, varvect, levels, stratumids, sn, pmatrix, i; kwargs...)
estimates::Vector{String} = [ "" ]

for (j, lev) enumerate(levels)
__catvariable!(estimates, varvect, lev, stratumids, pmatrix, i, j; kwargs...)
end

insertcols!(_t, Symbol(sn) => estimates)
end

Expand Down Expand Up @@ -429,7 +423,7 @@ end

function _npvariable!(_t, varvect, ids, sn; digits=1, kwargs...)
med = median(skipmissing(varvect[ids]))
iqr = quantile(skipmissing(varvect[ids]), [.25, .75])
iqr = quantile(skipmissing(varvect[ids]), [ 0.25, 0.75 ])
_med = "$(sprint(show, round(med; digits)))"
_iqr = "$(sprint(show, round(iqr[1]; digits)))$(sprint(show, round(iqr[2]; digits)))"
estimates::Vector{String} = [ "$_med [$_iqr]" ]
Expand Down Expand Up @@ -542,7 +536,6 @@ function _contvariable(
else
pvectors = nothing
end

return _contvariable(
addfn, pfn, strata, stratanames, strataids, varvect, varname, pvectors;
kwargs...
Expand Down Expand Up @@ -570,7 +563,7 @@ function _contvariable(
return _t
end

_getvarname(var, varnames::Nothing) = String(var)
_getvarname(var, ::Nothing) = String(var)

function _getvarname(var, varnames::Dict)
if var keys(varnames)
Expand All @@ -594,7 +587,7 @@ function _countmissing(varvect, strataids, sn=:Total)
return n
end

_binvariabledisplay(v, varvect, binvardisplay::Nothing) = maximum(skipmissing(unique(varvect)))
_binvariabledisplay(::Any, varvect, ::Nothing) = maximum(skipmissing(unique(varvect)))

function _binvariabledisplay(v, varvect, binvardisplay::Dict)
if v keys(binvardisplay)
Expand All @@ -604,23 +597,18 @@ function _binvariabledisplay(v, varvect, binvardisplay::Dict)
end
end

_catvarpmatrix!(pmatrix::Nothing, n, i, j) = nothing
_catvarpmatrix!(::Nothing, ::Any, ::Any, ::Any) = nothing
_catvarpmatrix!(pmatrix::Matrix{<:Integer}, n::Int, i::Int, j::Int) = pmatrix[j, i] = n
_contpvectors!(pvectors::Nothing, newvect) = nothing
_contpvectors!(::Nothing, ::Any) = nothing
_contpvectors!(pvectors::Vector, newvect) = push!(pvectors, newvect)
_addcatpvalue!(_t, pmatrix::Nothing; kwargs...) = nothing
_addcatpvalue!(::Any, ::Nothing; kwargs...) = nothing

function _addcatpvalue!(_t, pmatrix::Matrix{<:Integer}; kwargs...)
@unpack p, testname = _catpvalue(pmatrix)
_addpvalue!(_t, p, testname; kwargs...)
end

function _addbinpvalue!(
_t, pmatrix::Nothing, stratanames, strataids, varvect, level;
kwargs...
)
return nothing
end
_addbinpvalue!(::Any, ::Nothing, ::Any, ::Any, ::Any, ::Any; kwargs...) = nothing

function _addbinpvalue!(
_t, pmatrix::Matrix{<:Integer}, stratanames, strataids, varvect, level;
Expand All @@ -631,12 +619,11 @@ function _addbinpvalue!(
@unpack n, denom = _catvalues(varvect, level, stratumids)
pmatrix[2, i] = denom - n
end

@unpack p, testname = _catpvalue(pmatrix)
_addpvalue!(_t, p, testname; kwargs...)
end

_addcontpvalue!(_t, func, pvectors::Nothing; kwargs...) = nothing
_addcontpvalue!(::Any, ::Any, ::Nothing; kwargs...) = nothing

function _addcontpvalue!(_t, func, pvectors; kwargs...)
p = pvalue(func(pvectors...))
Expand All @@ -647,7 +634,6 @@ function _addpvalue!(_t, p, testname; addtestname, pdigits, kwargs...)
pcol = [ "" for _ axes(_t, 1) ]
pcol[1] = "$(sprint(show, round(p; digits = pdigits)))"
insertcols!(_t, :p => pcol)

if addtestname _addtestname!(_t, testname) end
end

Expand All @@ -662,7 +648,6 @@ function _addtestname!(_t, testname::AbstractString)
else
tn = testname
end

tncol = [ "" for _ axes(_t, 1) ]
tncol[1] = tn
insertcols!(_t, :test => tncol)
Expand All @@ -676,7 +661,6 @@ function _catpvalue(pmatrix)
p = pvalue(ChisqTest(pmatrix))
testname = ChisqTest
end

return ( p=p, testname=testname )
end

Expand Down

2 comments on commit 54a843c

@markgpritchard
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 register

Release notes:
Allow default keyword argument to be changed if making table without stratifying variable.

@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/119242

Tagging

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.2.2 -m "<description of version>" 54a843c8d78678f00849ed4f2cec8b6922c66dbd
git push origin v0.2.2

Please sign in to comment.