Skip to content

Commit

Permalink
Merge pull request #43 from PharmCat/dev
Browse files Browse the repository at this point in the history
v0.2.7
  - setkelauto!
  - getkelauto
  - applyelimrange!
  - getval improove
  - Mean CI fix
  - code fix
  - experimental tests
  - DataSet{Descriptive} show changes
  - improove coverage
  - test update
  • Loading branch information
PharmCat authored Jun 1, 2020
2 parents e8b83e1 + b017c52 commit 36e4639
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors = ["Vladimir Arnautov ([email protected])"]
name = "ClinicalTrialUtilities"
uuid = "535c2557-d7d0-564d-8ff9-4ae146c18cfe"
version = "0.2.6"
version = "0.2.7"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ ci = meanci(30, 10, 30, alpha = 0.05, method=:norm)
#### NCA
```
using CSV, DataFrames, ClinicalTrialUtilities
pkdata2 = CSV.File("pkdata.csv") |> DataFrame
pkds = pkimport(pkdata2, [:Subject, :Formulation]; time = :Time, conc = :Concentration)
pkdatapath = joinpath(dirname(pathof(ClinicalTrialUtilities)))*"\\..\\test\\csv\\pkdata2.csv"
pkdata = CSV.File(pkdatapath) |> DataFrame
pkds = pkimport(pkdata, [:Subject, :Formulation]; time = :Time, conc = :Concentration)
pk = nca!(pkds)
ncadf = DataFrame(pk; unst = true)
ds = ClinicalTrialUtilities.descriptive(ncadf, stats = [:n, :mean, :sd], sort = [:Formulation])
dsdf = ClinicalTrialUtilities.DataFrame(ds; unst = true)
```

#### Randomization
```
using DataFrames, ClinicalTrialUtilities
Expand Down
12 changes: 12 additions & 0 deletions cange.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
v0.2.7
- setkelauto!
- getkelauto
- applyelimrange!
- getval improove
- Mean CI fix
- code fix
- experimental tests
- DataSet{Descriptive} show changes
- improove coverage
- test update

v0.2.6

- findfirst
Expand Down
9 changes: 7 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[compat]

Documenter = "0.23"

Documenter = "0.23, 0.24"
DataFrames = "0.19, 0.20"
CSV = "0.5, 0.6"
Plots = "1.2, 1.3"
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, ClinicalTrialUtilities
using Documenter, ClinicalTrialUtilities, DataFrames, CSV, Plots

makedocs(
modules = [ClinicalTrialUtilities],
Expand Down
4 changes: 3 additions & 1 deletion docs/src/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ ClinicalTrialUtilities.diffmeanci

Table cell map:

| | outcome 1 | outcome 2 |

| group | outcome 1 | outcome 2 |
|---------|-----------|-----------|
| group 1 | a | b |
| group 2 | c | d |


### diffcmhci
```@docs
ClinicalTrialUtilities.diffcmhci
Expand Down
26 changes: 26 additions & 0 deletions docs/src/pkplot.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,29 @@
```@docs
ClinicalTrialUtilities.pkplot
```

#### Examples


```@example 1
using ClinicalTrialUtilities, DataFrames, CSV, Plots
pkdatapath = joinpath(dirname(pathof(ClinicalTrialUtilities)))*"\\..\\test\\csv\\pkdata2.csv"
pkdata = CSV.File(pkdatapath) |> DataFrame
pkds = ClinicalTrialUtilities.pkimport(pkdata, [:Subject, :Formulation]; conc = :Concentration, time = :Time)
plot1 = pkplot(pkds[1], legend = false);
#savefig("pkplot1.svg"); # hide
plot2 = pkplot(pkds; pagesort = [:Formulation], typesort = [:Subject])[1];
#savefig("pkplot2.svg"); nothing # hide
```

Plot for subject:

```@example 1
plot1
```

First plot for DataSet:

```@example 1
plot2
```
3 changes: 3 additions & 0 deletions src/ClinicalTrialUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ ElimRange,
DoseTime,
LimitRule,
applyncarule!,
applyelimrange!,
keldata,
setkelauto!,
getkelauto,
# Simulation
ctsim,
#Plots
Expand Down
68 changes: 41 additions & 27 deletions src/ci.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
# DescTools https://CRAN.R-project.org/package=DescTools
# metafor by Wolfgang Viechtbauer https://cran.r-project.org/package=metafor

struct ConfInt{T <: Real}
lower::T
upper::T
estimate::T
alpha::T
function ConfInt(lower::T, upper::T, estimate::T) where T <: Real
new{T}(lower, upper, estimate, NaN)::ConfInt
struct ConfInt
lower::Real
upper::Real
estimate::Real
alpha::Real
function ConfInt(lower, upper, estimate)
new(lower, upper, estimate, NaN)::ConfInt
end
function ConfInt(lower::T, upper::T, estimate::T, alpha::T) where T <: Real
new{T}(lower, upper, estimate, alpha)::ConfInt
function ConfInt(lower, upper, estimate, alpha)
new(lower, upper, estimate, alpha)::ConfInt
end
end

Expand All @@ -29,8 +29,10 @@ function getindex(a::ConfInt, b::Int)
return a.upper
elseif b == 3
return a.estimate
elseif b == 4
return a.alpha
else
throw(ArgumentError("Index should be in 1:3"))
throw(ArgumentError("Index should be in 1:4"))
end
end

Expand Down Expand Up @@ -61,10 +63,10 @@ function StatsBase.confint(param::RiskRatio{P}; level = 0.95, method = :default)
rrpropci(param.a.x, param.a.n, param.b.x, param.b.n; alpha = 1 - level, method = method)
end
function StatsBase.confint(param::Mean; level = 0.95, method = :default)::ConfInt
meanci(param.m, param.s, param.n; alpha = 1 - level, method = method)
meanci(param.m, param.sd^2, param.n; alpha = 1 - level, method = method)
end
function StatsBase.confint(param::DiffMean{true}; level = 0.95, method = :default)::ConfInt where T <: Mean
diffmeanci(param.a.m, param.a.s, param.a.n, param.b.m, param.b.s, param.b.n; alpha = 1 - level, method = method)
diffmeanci(param.a.m, param.a.sd^2, param.a.n, param.b.m, param.b.sd^2, param.b.n; alpha = 1 - level, method = method)
end
"""
propci(x::Int, n::Int; alpha=0.05, method = :default)::ConfInt
Expand Down Expand Up @@ -205,30 +207,40 @@ function orpropci(x1::Int, n1::Int, x2::Int, n2::Int; alpha::Real = 0.05, method
end
end
"""
meanci(m::Real, s::Real, n::Int; alpha::Real = 0.05,
meanci(m::Real, σ²::Real, n::Int; alpha::Real = 0.05,
method=:default)::ConfInt
Confidence interval for mean.
Confidence interval for mean, where:
m - mean;
σ² - variance;
n - observation number.
# Computation methods:
- :norm - Normal distribution (default);
- :tdist - T Distribution.
"""
function meanci(m::Real, s::Real, n::Int; alpha::Real = 0.05, method=:default)::ConfInt
function meanci(m::Real, σ²::Real, n::Int; alpha::Real = 0.05, method=:default)::ConfInt
if method==:norm || method == :default
return meannormci(m, s, n, alpha)
return meannormci(m, σ², n, alpha)
elseif method == :tdist
return meantdistci(m, s, n, alpha)
return meantdistci(m, σ², n, alpha)
end
end
"""
diffmeanci(m1::Real, s1::Real, n1::Real, m2::Real, s2::Real, n2::Real;
diffmeanci(m1::Real, σ²1::Real, n1::Real, m2::Real, σ²2::Real, n2::Real;
alpha::Real = 0.05, method::Symbol = :default)::ConfInt
Confidence interval for mead difference.
m1, m2 - mean;
σ²1, σ²2 - variance;
n1, n2 - observation number.
# Computation methods:
- :ev - equal variance (default);
- :uv - unequal variance with Welch-Satterthwaite df correction.
"""
Expand Down Expand Up @@ -675,19 +687,19 @@ end
#-------------------------------MEANS---------------------------------------

#Normal
function meannormci(m::Real, s::Real, n::Real, alpha::Real)::ConfInt
e = quantile(ZDIST, 1-alpha/2)*sqrt(s/n)
function meannormci(m::Real, σ²::Real, n::Real, alpha::Real)::ConfInt
e = quantile(ZDIST, 1-alpha/2)*sqrt(σ²/n)
return ConfInt(m-e, m+e, float(m), alpha)
end
#T Distribution
function meantdistci(m::Real, s::Real, n::Real, alpha::Real)::ConfInt
e = quantile(TDist(n-1), 1-alpha/2)*sqrt(s/n)
function meantdistci(m::Real, σ²::Real, n::Real, alpha::Real)::ConfInt
e = quantile(TDist(n-1), 1-alpha/2)*sqrt(σ²/n)
return ConfInt(m-e, m+e, float(m), alpha)
end
#mean diff equal var
function meandiffev(m1::Real, s1::Real, n1::Real, m2::Real, s2::Real, n2::Real, alpha::Real)::ConfInt
function meandiffev(m1::Real, σ²1::Real, n1::Real, m2::Real, σ²2::Real, n2::Real, alpha::Real)::ConfInt
diff = float(m1 - m2)
stddev = sqrt(((n1 - 1) * s1 + (n2 - 1) * s2) / (n1 + n2 - 2))
stddev = sqrt(((n1 - 1) * σ²1 + (n2 - 1) * σ²2) / (n1 + n2 - 2))
stderr = stddev * sqrt(1/n1 + 1/n2)
d = stderr*quantile(TDist(n1+n2-2), 1-alpha/2)
return ConfInt(diff-d, diff+d, diff, alpha)
Expand All @@ -698,10 +710,10 @@ end
#mean diff unequal var
#Two sample t-test (unequal variance)
#Welch-Satterthwaite df
function meandiffuv(m1::Real, s1::Real, n1::Real, m2::Real, s2::Real, n2::Real, alpha::Real)::ConfInt
function meandiffuv(m1::Real, σ²1::Real, n1::Real, m2::Real, σ²2::Real, n2::Real, alpha::Real)::ConfInt
diff = float(m1 - m2)
v = (s1/n1+s2/n2)^2/(s1^2/n1^2/(n1-1)+s2^2/n2^2/(n2-1))
stderr = sqrt(s1/n1 + s2/n2)
v = (σ²1 / n1 + σ²2 / n2)^2 /( σ²1^2 / n1^2 / (n1 - 1) + σ²2^2 / n2^2 / (n2 - 1))
stderr = sqrt(σ²1 / n1 + σ²2 / n2)
d = stderr*quantile(TDist(v), 1-alpha/2)
return ConfInt(diff-d, diff+d, diff, alpha)
end
Expand All @@ -725,8 +737,10 @@ function meanratiot(m1::Real, s1::Real, n1::Real, m2::Real, s2::Real, n2::Real,
d = sqrt((cov^2 - s1 * s2) * t0^4 + (m1^2 * s2 - 2 * m1 * m2 * cov + m2^2 * s1) * t0^2)
return ((a - d)/b, (a + d)/b, m2/m1, alpha)
end
#=
function meanratiof(m1::Real, s1::Real, n1::Real, m2::Real, s2::Real, n2::Real, cov::Real, alpha::Real)
end
=#

#Cochran–Mantel–Haenszel confidence intervals
#p275, Rothman, K. J., Greenland, S., & Lash, T. L. (2008). Modern epidemiology (3rd ed.). Philadelphia: Lippincott Williams & Wilkins.
Expand Down
80 changes: 77 additions & 3 deletions src/descriptives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,89 @@ function Base.show(io::IO, obj::Descriptive)
println(io, String(spv), "___", "____________")
end
end


function Base.getindex(a::Descriptive, s::Symbol)::Real
return a.result[s]
end

function getkeylist(obj::DataSet)
keylist = Vector{Any}(undef, 0)
for i = 1:length(obj)
if length(obj[i].sort) > 0
for k in keys(obj[i].sort)
if k keylist push!(keylist, k) end
end
end
end
keylist
end

function Base.show(io::IO, obj::DataSet{Descriptive})
println(io, "Descriptive statistics set")
if length(obj) == 0
println(io, "Empty")
return
end
keylist = getkeylist(obj)
strkeylist = string.(keylist)
maxkeyvars = Vector{Int}(undef, length(keylist))

strvars = Vector{String}(undef, length(obj))
keyvalmx = Matrix{String}(undef, length(obj), length(keylist))

for i = 1:length(obj)
strvars[i] = string(obj[i].var)
for kl = 1:length(keylist)
if keylist[kl] in keys(obj[i].sort)
keyvalmx[i, kl] = string(obj[i].sort[keylist[kl]])*" "
else
keyvalmx[i, kl] = "."
end
end
end

maxstrvars = max(10, maximum(length.(strvars)))

for i = 1:length(keylist)
maxkeyvars[i] = max(length(strkeylist[i]), maximum(length.(keyvalmx[:, i]))) + 2
if length(strkeylist[i]) < maxkeyvars[i]
for ps = 1: length(strkeylist[i]) - maxkeyvars[i]
strkeylist[i] = strkeylist[i]*" "
end
end
end

print(io, "N ")
#=
print(io, "Variabels ")
=#
for i = 1:length(strkeylist)
print(io, strkeylist[i])
if length(strkeylist[i]) < maxkeyvars[i]
for s = 1:(maxkeyvars[i] - length(strkeylist[i])) print(io, " ") end
end
end

println(io, " ")


for i = 1:length(obj.data)
print(io, i, " | ", string(obj.data[i].var))
if obj.data[i].sort !== nothing println(io, " | ", string(obj.data[i].sort)) end
println(io, "____________________")
print(io, "$i ")
#=
print(io, strvars[i])
if length(strvars[i]) < maxstrvars
for s = 1:(maxstrvars - length(strvars[i])) print(io, " ") end
end
=#
for c = 1:length(keylist)
print(io, keyvalmx[i, c])
if length(keyvalmx[i, c]) < maxkeyvars[c]
for s = 1:(maxkeyvars[c] - length(keyvalmx[i, c])) print(io, " ") end
end
end

println(io, " ")
end
end
"""
Expand Down
Loading

3 comments on commit 36e4639

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

Release notes:

v0.2.7

  • setkelauto!
  • getkelauto
  • applyelimrange!
  • getval improove
  • Mean CI fix
  • code fix
  • experimental tests
  • DataSet{Descriptive} show changes
  • improove coverage
  • test update

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

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.7 -m "<description of version>" 36e4639540efefa63e2829a3b53486171ed9e8cf
git push origin v0.2.7

@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.

@nanosoldier runtests(ALL)

Please sign in to comment.