/
egf_single.R
63 lines (50 loc) · 1.18 KB
/
egf_single.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
library(tidyverse)
library(epigrowthfit)
library(shellpipes)
library(stringr)
loadEnvironments()
fakewin <- data.frame(start = -Inf, end=Inf)
print(windows)
egffun <- function(x){
## Drop the past-the-peak point for exponential
if(method=="Exponential"){
x <- (x
|> filter(offset < max(offset) | cases == max(cases))
)
}
x <- bind_rows(data.frame(offset = x$offset[[1]]-1
, cases = NA)
, x)
mod <- egf(model = egf_model(curve = method, family = "nbinom")
, data_ts = x
, formula_ts = cbind(offset, cases) ~ 1
, formula_parameters = ~ 1
, data_windows = fakewin
, formula_windows = cbind(start, end) ~ 1
, se = TRUE
)
return(mod)
}
r_ests <- function(x){
cf <- confint(x[[1]], level = 0.95, A = NULL, class = TRUE)
df <- data.frame(lwr = exp(cf[["ci"]][, 1L][[1]])
, est = exp(cf[["value"]][[1]])
, upr = exp(cf[["ci"]][, 2L][[1]])
)
return(df)
}
## Do we need map here? Why??
ff <- (selected
%>% group_by(loc,phase)
%>% nest()
%>% mutate(egf_fit = map(data,~ egffun(.)))
)
print(ff)
print(ff$egf_fit)
fulldat <-(ff
%>% group_by(loc,phase,egf_fit)
%>% reframe(r_ests(egf_fit))
%>% mutate(method=str_to_title(method))
)
print(fulldat)
rdsSave(fulldat)