forked from eliminaterabies/egfR0
/
egf_fit.R
59 lines (45 loc) · 1.14 KB
/
egf_fit.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
library(shellpipes)
library(dplyr)
library(epigrowthfit)
loadEnvironments()
## First fit egf
egfing <- function(l, p){
dat <- df %>% filter(loc == l & phase == p)
wins <- windows %>% filter(loc == l & phase == p)
return(egf(model = egf_model(curve = "logistic", family = "nbinom"),
data_ts = dat,
formula_ts = cbind(offset, cases) ~ 1,
formula_parameters = ~ 1,
data_windows = wins,
formula_windows = cbind(start, end) ~ 1,
se = TRUE
))
}
## r0 samples
rsamps <- function(x,n=100){
mm <-coef(x)[1]
vv <- diag(vcov(x))[1]
exp(rnorm(n=n,mean=mm,sd=sqrt(vv)))
}
rsamples <- sapply(keep,function(x)rsamps(egfing(x)))
## Note, the units here is 1/month
print(rsamples)
rsamplong <- (rsamples
|> as.data.frame()
|> gather(value="rsamp",key="loc")
# |> pivot_longer(values_to="rsamp",names_to="loc")
|> group_by(loc)
|> summarise(NULL
, mid = quantile(rsamp,probs=0.5)
, lwr = quantile(rsamp,probs=0.025)
, upr = quantile(rsamp,probs=0.975)
)
|> arrange(loc,desc=FALSE)
)
gg <- (ggplot(rsamplong, aes(x=loc))
+ geom_pointrange(aes(ymin=lwr,ymax=upr,y=mid))
+ coord_flip()
+ ylab("r (1/month)")
)
print(gg)
print(summary(rsamples))