Skip to content

Commit b8d93be

Browse files
author
Eduard Grebe
authored
Merge pull request #35 from eduardgrebe/master
Update frrcal and bump inctools version to 1.0.15 for release
2 parents b32bf86 + 633f6ed commit b8d93be

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

inctools-julia/Incidence.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,41 @@ import Distributions
77
import Statistics
88
import DataFrames
99

10-
function prevalence(pos, n, de = 1) #, f = 1
10+
function prevalence(pos, n, de = 1; ci = false, α = 0.05) #, f = 1
1111
if n == 0
1212
@warn "n = 0: prevalence undefined. n set to 1."
1313
n = 1
1414
end
15+
if pos < 5
16+
@warn "Too few successes for the normal approximation to be valid"
17+
end
18+
if n - pos < 5
19+
@warn "Sample size twoo small for the normal apprximation to be valid"
20+
end
1521
p = pos/n
1622
σ = sqrt( (p * (1 - p)) / n ) * de #* sqrt(1 - f)
17-
return p, σ
23+
if !ci
24+
return (p = p, σ = σ)
25+
elseif ci
26+
Fd =
27+
if pos == 0
28+
lb = 0
29+
ub = 1-/2)^(1/n)
30+
elseif pos == n
31+
lb =/2)^(1/n)
32+
ub = 1
33+
else
34+
lb = ( 1+(n-pos+1)/(pos * Distributions.quantile(Distributions.FDist(2*pos, 2*(n-pos+1)), α/2)) ) ^ (-1)
35+
ub = ( 1+(n-pos)/((pos + 1) * Distributions.quantile(Distributions.FDist(2*(pos + 1), 2*(n-pos)), 1-α/2)) ) ^ (-1)
36+
end
37+
38+
# Normal approximation
39+
# pprime = (pos + 2) / (n + 4)
40+
# sigmaprime = sqrt( (pprime * (1 - pprime)) / (n + 4) )
41+
#lb = max(pprime - 1.96 * sigmaprime,0)
42+
# ub = min(pprime + 1.96 * sigmaprime,1)
43+
return (p = p, σ = σ, ci = [lb, ub])
44+
end
1845
end
1946

2047
function kassanjee(prev::Float64, prevR::Float64, mdri::Float64, frr::Float64, T::Float64)

inctools/DESCRIPTION

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ Encoding: UTF-8
22
Package: inctools
33
Type: Package
44
Title: Incidence Estimation Tools
5-
Version: 1.0.14.9005
6-
Date: 2019-08-28
5+
Version: 1.0.15
6+
Date: 2019-11-05
77
Authors@R: c(
8-
person("Eduard","Grebe",email="[email protected]",role=c("cre","aut")),
8+
person("Eduard","Grebe",email="[email protected]",role=c("cre","aut")),
99
person("Alex","Welte", email="[email protected]", role="aut"),
1010
person("Avery","McIntosh",email="[email protected]",role="aut"),
1111
person("Petra","Bäumler",email="[email protected]",role="aut"),
@@ -43,7 +43,8 @@ Imports:
4343
pracma,
4444
foreach,
4545
parallel,
46-
doParallel
46+
doParallel,
47+
binom
4748
Suggests:
4849
survey,
4950
knitr,

inctools/R/calibration_frr.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#' @param recency_vars Variables to be used in determining recency outcomes
3030
#' @param recency_params Vector of numeric parameters (e.g. thresholds) for determining recency according to the relevant rule
3131
#' @param alpha Confidence level, default=0.05.
32+
#' @param method Method for computing confidence interval on binomial probability (passed to binom::binom.confint). Default is Clopper-Pearson 'exact' method. Accepted values: `c("exact", "ac", "asymptotic", "wilson", "prop.test", "bayes", "logit", "cloglog", "probit")`.
3233
#' @param debug Enable debugging mode (browser)
3334
#' @details The package contains long form documentation in the form of vignettes that cover the use of the main fucntions. Use browseVignettes(package="inctools") to access them.
3435
#'
@@ -51,6 +52,7 @@
5152
#' recency_rule = "independent_thresholds",
5253
#' recency_vars = c("Result","VL"),
5354
#' recency_params = c(10,0,1000,1),
55+
#' method = "exact",
5456
#' alpha = 0.05)
5557
#' @export
5658
frrcal <- function(data = NULL,
@@ -61,6 +63,7 @@ frrcal <- function(data = NULL,
6163
recency_vars = NULL,
6264
recency_params = NULL,
6365
alpha = 0.05,
66+
method = "exact",
6467
debug = FALSE) {
6568

6669
if (debug) {browser()}
@@ -101,6 +104,13 @@ frrcal <- function(data = NULL,
101104
if (is.null(time_var)) {stop("Error: No time variable provided.")}
102105
if (is.null(time_var)) {stop("Error: No recency variables provided variable provided.")}
103106

107+
if (is.null(method)) {stop("Error: Confidence interval method must be specified.")}
108+
109+
if (length(method) != 1) {stop("Error: Exactly one confidence interval method must be specified.")}
110+
111+
if ( !(method %in% c("exact", "ac", "asymptotic", "wilson", "prop.test", "bayes", "logit", "cloglog", "probit"))) {
112+
stop("Confidence interval method must be one of 'exact', 'ac', 'asymptotic', 'wilson', 'prop.test', 'bayes', 'logit', 'cloglog', 'probit'. See help of binom::binom.test() for further details.")
113+
}
104114

105115
# check that subject id, time and recency variables exist
106116
variables <- colnames(data)
@@ -143,13 +153,13 @@ frrcal <- function(data = NULL,
143153
}
144154
}
145155
subjectdata <- subjectdata[!is.na(subjectdata$sid),]
146-
nr <- ceiling(sum(subjectdata$recent))
156+
nr <- as.integer(ceiling(sum(subjectdata$recent)))
147157
n <- nrow(subjectdata)
148158
p <- nr / n
149159
sigma <- sqrt( (p * (1 - p)) / n )
150-
binom_test <- stats::binom.test(nr, n, p = 0, conf.level = 1 - alpha)
151-
FRR <- data.frame(p, sigma, binom_test$conf.int[1], binom_test$conf.int[2], alpha, nr, n, nrow(data))
152-
colnames(FRR) <- c("FRRest", "SE", "LB", "UB", "alpha", "n_recent", "n_subjects", "n_observations")
153-
rownames(FRR) <- ""
160+
binom_ci <- binom::binom.confint(nr, n, conf.level = 1 - alpha, methods = method)
161+
FRR <- tibble::tibble(FRRest = p, SE = sigma, LB = binom_ci$lower, UB = binom_ci$upper,
162+
alpha = alpha, n_recent = nr, n_subjects = n, n_observations = nrow(data),
163+
ci_method = method)
154164
return(FRR)
155165
}

inctools/man/frrcal.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ frr_untreated <- frrcal(data = untreated_all,
155155
recency_rule = "independent_thresholds",
156156
recency_vars = c("LAg_ODn","viral_load"),
157157
recency_params = c(1.5,0,75,1),
158+
method = "exact",
158159
debug = TRUE)
159160
print(frr_untreated)
160161
frr_treated <- frrcal(data = treated_all,

0 commit comments

Comments
 (0)