-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrowth_rate.R
79 lines (66 loc) · 2.24 KB
/
growth_rate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#' Calculate growth/decay rate
#'
#' @author Tim Taylor
#'
#' @param x The output of `fit_curve()`.
#' @param alpha Value of alpha used to calculate confidence intervals; defaults
#' to 0.05 which corresponds to a 95% confidence interval.
#' @param growth_decay_time Should a doubling/halving time and corresponding
#' confidence intervals be added to the output. Default TRUE.
#' @param include_warnings Include models in output that triggered warnings but
#' not errors. Defaults to `FALSE`.
#' @param ... Not currently used.
#'
#' @export
growth_rate <- function(x, ...) {
UseMethod("growth_rate")
}
#' @rdname growth_rate
#' @aliases growth_rate.default
#' @export
growth_rate.default <- function(x, ...) {
not_implemented(x)
}
#' @rdname growth_rate
#' @aliases growth_rate.incidence2_fit
#' @export
growth_rate.incidence2_fit <- function(x, alpha = 0.05,
growth_decay_time = TRUE,
include_warnings = FALSE, ...) {
dat <- is_ok(x, include_warnings = include_warnings)
model_var <- attr(dat, "model")
r <- vapply(
dat[[model_var]],
function(x) x$coefficients[2],
double(1)
)
r_lower <- vapply(
dat[[model_var]],
function(x) suppressMessages(confint(x, 2, 1 - alpha)[1]),
double(1)
)
r_upper <- vapply(
dat[[model_var]],
function(x) suppressMessages(confint(x, 2, 1 - alpha)[2]),
double(1)
)
res <- tibble(
model = dat[[model_var]],
r,
r_lower,
r_upper
)
groups <- attr(dat, "groups")
if (!is.null(groups)) res <- bind_cols(dat[groups], res)
count_variable <- attr(dat, "count_variable")
res <- bind_cols(dat[count_variable], res)
if (growth_decay_time) add_two_time(res) else res
}
add_two_time <- function(dat, r = "r", r_lower = "r_lower", r_upper = "r_upper") {
condition <- dat[[r]] < 0
dat$growth_or_decay <- ifelse(condition, "halving", "doubling")
dat$time <- ifelse(condition, log(0.5)/dat[[r]], log(2)/dat[[r]])
dat$time_lower <- ifelse(condition, log(0.5)/dat[[r_lower]], log(2)/dat[[r_upper]])
dat$time_upper <- ifelse(condition, log(0.5)/dat[[r_upper]], log(2)/dat[[r_lower]])
dat
}