Skip to content

Commit

Permalink
test update
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Dec 8, 2019
1 parent cfa2828 commit 37eed39
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 216 deletions.
19 changes: 11 additions & 8 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,24 @@ Pooled CV from multiple sources.
- false - return var
"""
function pooledcv(data::DataFrame; cv=:cv, df=:df, alpha=0.05, returncv=true)::ConfInt
function pooledcv(data::DataFrame; cv = :cv, df = :df, alpha::Real = 0.05, returncv::Bool = true)::ConfInt
if isa(cv, String) cv = Symbol(cv) end
if isa(df, String) df = Symbol(df) end
tdf = sum(data[:, df])
result = sum(varfromcv.(data[:, cv]) .* data[:, df])/tdf
CHSQ = Chisq(tdf)
if returncv return ConfInt(cvfromvar(result*tdf/quantile(CHSQ, 1-alpha/2)), cvfromvar(result*tdf/quantile(CHSQ, alpha/2)), cvfromvar(result))
else ConfInt(result*tdf/quantile(CHSQ, 1-alpha/2), result*tdf/quantile(CHSQ, alpha/2), result)
end
return pooledcv(data[!, cv], data[!, df]; alpha = alpha, returncv = returncv)
end
function pooledcv(cv, df; alpha=0.05, returncv = true)::ConfInt
function pooledcv(cv::Vector, df::Vector; alpha=0.05, returncv = true)::ConfInt
tdf = sum(df)
result = sum(varfromcv.(cv) .* df)/tdf
CHSQ = Chisq(tdf)
if returncv return ConfInt(cvfromvar(result*tdf/quantile(CHSQ, 1-alpha/2)), cvfromvar(result*tdf/quantile(CHSQ, alpha/2)), cvfromvar(result))
else ConfInt(result*tdf/quantile(CHSQ, 1-alpha/2), result*tdf/quantile(CHSQ, alpha/2), result)
end
end
function pooledcv(cv::Vector, n::Vector, design::Vector; alpha=0.05, returncv = true)::ConfInt
d = Design.(design)
df = Array{Int, 1}(undef, length(d))
for i = 1:length(d)
df[i] = d[i].df(n[i])
end
return pooledcv(cv, df; alpha = alpha, returncv = returncv)
end
52 changes: 26 additions & 26 deletions test/citest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,142 +44,142 @@ println(" ---------------------------------- ")
#---- twoProp
#-- mn

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:or, method=:mn)
ci = ClinicalTrialUtilities.orpropci(30, 100, 40, 90; alpha=0.05, method=:mn)
@test ci.lower 0.29537414 atol=1E-6
@test ci.upper 0.97166697 atol=1E-6
@test ci.estimate 0.53571428 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(100, 100, 90, 90; alpha=0.05, type=:or, method=:mn)
ci = ClinicalTrialUtilities.orpropci(100, 100, 90, 90; alpha=0.05, method=:mn)
@test ci.lower 0.0
@test ci.upper Inf
#@test ci.estimate == NaN

ci = ClinicalTrialUtilities.twoprop(0, 100, 90, 90; alpha=0.05, type=:or, method=:mn)
ci = ClinicalTrialUtilities.orpropci(0, 100, 90, 90; alpha=0.05, method=:mn)
@test ci.lower 0.0
@test ci.upper 0.00041425995552740226 atol=1E-6
@test ci.estimate 0.0

ci = ClinicalTrialUtilities.twoprop(100, 100, 0, 90; alpha=0.05, type=:or, method=:mn)
ci = ClinicalTrialUtilities.orpropci(100, 100, 0, 90; alpha=0.05, method=:mn)
@test ci.lower 2413.9431770847045 atol=1E-6
@test ci.upper Inf
@test ci.estimate Inf

#-- mn2

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:or, method=:mn2)
ci = ClinicalTrialUtilities.orpropci(30, 100, 40, 90; alpha=0.05, method=:mn2)
@test ci.lower 0.2951669 atol=1E-6
@test ci.upper 0.9722965 atol=1E-6
@test ci.estimate 0.5357142 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(100, 100, 90, 90; alpha=0.05, type=:or, method=:mn2)
ci = ClinicalTrialUtilities.orpropci(100, 100, 90, 90; alpha=0.05, method=:mn2)
@test ci.lower 0.0
@test ci.upper Inf
#@test ci.estimate == NaN

ci = ClinicalTrialUtilities.twoprop(0, 100, 90, 90; alpha=0.05, type=:or, method=:mn2)
ci = ClinicalTrialUtilities.orpropci(0, 100, 90, 90; alpha=0.05, method=:mn2)
@test ci.lower 0.0
@test ci.upper 0.0004144169697670039 atol=1E-6
@test ci.estimate 0.0

ci = ClinicalTrialUtilities.twoprop(100, 100, 0, 90; alpha=0.05, type=:or, method=:mn2)
ci = ClinicalTrialUtilities.orpropci(100, 100, 0, 90; alpha=0.05, method=:mn2)
@test ci.lower 2411.6137253788347 atol=1E-6
@test ci.upper Inf
@test ci.estimate Inf

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:or, method=:awoolf)
ci = ClinicalTrialUtilities.orpropci(30, 100, 40, 90; alpha=0.05, method=:awoolf)
@test ci.lower 0.2982066 atol=1E-6
@test ci.upper 0.9758363 atol=1E-6
@test ci.estimate 0.5394449 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:or, method=:woolf)
ci = ClinicalTrialUtilities.orpropci(30, 100, 40, 90; alpha=0.05, method=:woolf)
@test ci.lower 0.29504200273798975 atol=1E-6
@test ci.upper 0.9727082695179062 atol=1E-6
@test ci.estimate 0.5357142857142857 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:or, method=:mover)
ci = ClinicalTrialUtilities.orpropci(30, 100, 40, 90; alpha=0.05, method=:mover)
@test ci.lower 0.2963748435372293 atol=1E-6
@test ci.upper 0.9689058534780502 atol=1E-6
@test ci.estimate 0.5357142857142857 atol=1E-6

#----

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:nhs)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:nhs)
@test ci.lower -0.275381800 atol=1E-6
@test ci.upper -0.007158419 atol=1E-6
@test ci.estimate -0.1444444 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:ac)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:ac)
@test ci.lower -0.276944506 atol=1E-6
@test ci.upper -0.006516705 atol=1E-6
@test ci.estimate -0.1444444 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:mn)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:mn)
@test ci.lower -0.2781290897168457 atol=1E-6
@test ci.upper -0.006708341755865329 atol=1E-6
@test ci.estimate -0.1444444 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:mee)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:mee)
@test ci.lower -0.27778775409226936 atol=1E-6
@test ci.upper -0.007071205228197489 atol=1E-6
@test ci.estimate -0.14444444444 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:mee2)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:mee2)
#Roots 0.7.4 CTU 0.1.7
@test ci.lower -0.27778778455 atol=1E-5 #Chang atol for validation
@test ci.upper -0.00707120778 atol=1E-5
@test ci.estimate -0.14444444444 atol=1E-7

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:wald)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:wald)
@test ci.lower -0.28084842238 atol=1E-6
@test ci.upper -0.00804046650 atol=1E-6
@test ci.estimate -0.14444444444 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:diff, method=:waldcc)
ci = ClinicalTrialUtilities.diffpropci(30, 100, 40, 90; alpha=0.05, method=:waldcc)
@test ci.lower -0.29140397794 atol=1E-6
@test ci.upper 0.00251508905 atol=1E-6
@test ci.estimate -0.14444444444 atol=1E-6

#---- RR
#-- mn

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:rr, method=:mn)
ci = ClinicalTrialUtilities.rrpropci(30, 100, 40, 90; alpha=0.05, method=:mn)
@test ci.lower 0.46636099123297575 atol=1E-6
@test ci.upper 0.9799258384796817 atol=1E-6
@test ci.estimate 0.675 atol=1E-6

ci = ClinicalTrialUtilities.twoprop(100, 100, 90, 90; alpha=0.05, type=:rr, method=:mn)
ci = ClinicalTrialUtilities.rrpropci(100, 100, 90, 90; alpha=0.05, method=:mn)
@test ci.lower 0.0
@test ci.upper Inf
#@test ci.estimate == NaN

ci = ClinicalTrialUtilities.twoprop(0, 100, 90, 90; alpha=0.05, type=:rr, method=:mn)
ci = ClinicalTrialUtilities.rrpropci(0, 100, 90, 90; alpha=0.05, method=:mn)
@test ci.lower 0.0
@test ci.upper 0.018137090385952483 atol=1E-6
@test ci.estimate 0.0

ci = ClinicalTrialUtilities.twoprop(100, 100, 0, 90; alpha=0.05, type=:rr, method=:mn)
ci = ClinicalTrialUtilities.rrpropci(100, 100, 0, 90; alpha=0.05, method=:mn)
@test ci.lower 44.8498645475395 atol=1E-6
@test ci.upper Inf
@test ci.estimate Inf

#-- cli

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:rr, method=:cli)
ci = ClinicalTrialUtilities.rrpropci(30, 100, 40, 90; alpha=0.05, method=:cli)
@test ci.lower 0.4663950 atol=1E-6
@test ci.upper 0.9860541 atol=1E-6
@test ci.estimate 0.675 atol=1E-4

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:rr, method=:mover)
ci = ClinicalTrialUtilities.rrpropci(30, 100, 40, 90; alpha=0.05, method=:mover)
@test ci.lower 0.4634443 atol=1E-6
@test ci.upper 0.9808807 atol=1E-6
@test ci.estimate 0.675 atol=1E-4

ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:rr, method=:mover)
ci = ClinicalTrialUtilities.rrpropci(30, 100, 40, 90; alpha=0.05, method=:mover)
@test ci.lower 0.4634443 atol=1E-6
@test ci.upper 0.9808807 atol=1E-6
@test ci.estimate 0.675 atol=1E-4
#katz
ci = ClinicalTrialUtilities.twoprop(30, 100, 40, 90; alpha=0.05, type=:rr, method=:katz)
ci = ClinicalTrialUtilities.rrpropci(30, 100, 40, 90; alpha=0.05, method=:katz)
@test ci.lower 0.4624671 atol=1E-6
@test ci.upper 0.9852050 atol=1E-6
@test ci.estimate 0.675 atol=1E-4
Expand Down
152 changes: 152 additions & 0 deletions test/ctsamplen.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
@testset "#1 ctsamplen Test " begin
#Sample Size Calculations in Clinical Research, Second Edition, Shein-Chung Chow, Ph.D., 2008, International Standard Book Number‑13: 978‑1‑58488‑982‑3
#1
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ea, group=:one, alpha=0.05, beta=0.2, sd=1, a=2, b=1.5)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 32
Base.show(io, t)
Base.show(io, t.task)
#2
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ei, group=:one, alpha=0.05, beta=0.2, sd=0.1, diff=0.05, a=2, b=2)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 35
Base.show(io, t)
#3
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ns, group=:one, alpha=0.05, beta=0.2, sd=1, diff=-0.5, a=2, b=1.5)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 7
Base.show(io, t)
#4
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ea, group=:two, alpha=0.05, beta=0.2, sd=10, a=5, b=10, k=1)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 63
Base.show(io, t)
#5
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ei, group=:two, alpha=0.05, beta=0.2, sd=10, diff=5, a=5, b=4, k=1)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 108
Base.show(io, t)
#6
t = ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ns, group=:two, alpha=0.05, beta=0.2, sd=10, diff=5, a=5, b=5, k=1)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 50
Base.show(io, t)
#7
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ea, group=:one, alpha=0.05, beta=0.2, a=0.5, b=0.3)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 50
Base.show(io, t)
#8
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ei, group=:one, alpha=0.05, beta=0.2, diff=0.2, a=0.6, b=0.6)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 52
Base.show(io, t)
#9
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ns, group=:one, alpha=0.05, beta=0.2, diff=-0.1, a=0.5, b=0.3)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 18
Base.show(io, t)
#10 p.92
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ea, group=:two, alpha=0.05, beta=0.2, a=0.65, b=0.85)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 70
Base.show(io, t)
#11 p.93
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ei, group=:two, alpha=0.05, beta=0.2, diff=0.2, a=0.75, b=0.80)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 133
Base.show(io, t)
#12 p.92
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ns, group=:two, alpha=0.05, beta=0.2, diff=-0.1, a=0.85, b=0.65)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 25
Base.show(io, t)
#13 p.108
t = ClinicalTrialUtilities.ctsamplen(param=:or, type=:ea, alpha=0.05, beta=0.2, a=0.4, b=0.25)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 156
Base.show(io, t)
#14.1 p.109
t = ClinicalTrialUtilities.ctsamplen(param=:or, type=:ei, alpha=0.05, beta=0.2, diff=0.5, a=0.25, b=0.25)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 366
Base.show(io, t)
#15.1 p.108
t = ClinicalTrialUtilities.ctsamplen(param=:or, type=:ns, alpha=0.05, beta=0.2, diff=0.2, a=0.4, b=0.25)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 242
Base.show(io, t)
#14.2
t = ClinicalTrialUtilities.ctsamplen(param=:or, type=:ei, alpha=0.05, beta=0.2, diff=exp(0.5), a=0.25, b=0.25, logscale = false)
@test ceil(t.result) == 366
Base.show(io, t)
#15.2
t = ClinicalTrialUtilities.ctsamplen(param=:or, type=:ns, alpha=0.05, beta=0.2, diff=exp(0.2), a=0.4, b=0.25, logscale = false)
@test ceil(t.result) == 242
Base.show(io, t)
#16
t = ClinicalTrialUtilities.ctsamplen(param=:prop, type=:mcnm, a=0.45, b=0.05)
@test ceil(t.result) == ceil(ClinicalTrialUtilities.ctsamplen(t.task).result) == 23
Base.show(io, t)

#Additional
#Different type input
@test ClinicalTrialUtilities.ctsamplen(param=:mean, type=:ns, group=:two, alpha=0.05, beta=0.2, diff=1, sd=20.0, a=1, b=2).result 1236.511446403953 atol=1E-12

@test ceil(ClinicalTrialUtilities.ctsamplen(param=:prop, type=:ns, group=:two, alpha=0.05, beta=0.2, diff=0.05, a=0.85, b=0.65).result) == 98
end

println(" ---------------------------------- ")
@testset "#2 ctpower Test " begin

#1
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ea, group=:one, a=2, b=1.5, sd=1,n=32, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8074304194325561 atol=1E-7
Base.show(io, t)
Base.show(io, t.task)
#2
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ei, group=:one, a=2, b=2, sd=0.1, diff=0.05, n=35, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8108839754376387 atol=1E-7
Base.show(io, t)
#3
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ns, group=:one, a=2, b=1.5, sd=1, diff=-0.5, n=7, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8415707712023641 atol=1E-7
Base.show(io, t)
#4
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ea, group=:two, a=5, b=10, sd=10, n=63, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8013023941055788 atol=1E-7
Base.show(io, t)
#5
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ei, group=:two, a=5, b=4, sd=10, diff=5, n=108, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.80452354556060 atol=1E-7
Base.show(io, t)
#6
t = ClinicalTrialUtilities.ctpower(param=:mean, type=:ns, group=:two, a=5, b=5, sd=10, diff=5, n=50, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8037819415575257 atol=1E-7
Base.show(io, t)
#7
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ea, group=:one, a=0.5, b=0.3, n=50, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8074304194325561 atol=1E-7
Base.show(io, t)
#8
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ei, group=:one, a=0.6, b=0.6, diff=0.2, n=52, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8060834056011101 atol=1E-7
Base.show(io, t)
#9
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ns, group=:one, a=0.5, b=0.3, diff=-0.1, n=18, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8161481827204281 atol=1E-7
Base.show(io, t)
#10
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ea, group=:two, a=0.65, b=0.85, n=70, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8019139068528 atol=1E-7
Base.show(io, t)
#11
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ei, group=:two, a=0.65, b=0.85, diff=0.05, n=136, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8033294052407269 atol=1E-7
Base.show(io, t)
#12
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:ns, group=:two, a=0.85, b=0.65, diff=-0.1, n=25, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.808599833380679 atol=1E-7
Base.show(io, t)
#13
t = ClinicalTrialUtilities.ctpower(param=:or, type=:ea, a=0.4, b=0.25, n=156, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8020239054864792 atol=1E-7
Base.show(io, t)
#14
t = ClinicalTrialUtilities.ctpower(param=:or, type=:ei, a=0.25, b=0.25, diff=0.5, n=366, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8008593380478983 atol=1E-7
Base.show(io, t)
#15
t = ClinicalTrialUtilities.ctpower(param=:or, type=:ns, a=0.4, b=0.25, diff=0.2, n=242, alpha=0.05)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.8007200876001626 atol=1E-7
Base.show(io, t)
#16
t = ClinicalTrialUtilities.ctpower(param=:prop, type=:mcnm, a=0.45, b=0.05, n=23, alpha=0.1)
@test ClinicalTrialUtilities.ctpower(t.task).result 0.9023805 atol=1E-7
Base.show(io, t)
end
Loading

0 comments on commit 37eed39

Please sign in to comment.