-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitFunctions.R
53 lines (48 loc) · 1.34 KB
/
fitFunctions.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
# fitFunctions.R
# For fitting noise around epidemic curve
nll.nb <- function(log.c = 0, logodds = 0, data = cases$count){
# Transform values
c <- exp(log.c)
prob <- 1 / (1 + exp(logodds))
-sum(dnbinom(data,c,prob,log=T))
}
nll.pois <- function(log.l = 0, data = cases$count){
# Transform value
ll <- exp(log.l)
-sum(dpois(data,ll,log=T))
}
# For fitting incubation period distribution
nllGammaIcens <- function(logmean = NULL, lograte = NULL){
pp <- exp(c(logmean,lograte))
alpha <- pp[1]*pp[2]
lambda <- pp[2]
# Calculate probabilities of being in intervals
intervalProb <- (
pgamma(perInc_max
, shape = alpha, rate = lambda
, log = F) -
pgamma(perInc_min
, shape = alpha, rate = lambda
, log = F)
)
# Adjust for data with two exposures
firstExp <- (
pgamma(perInc_min[ipOr] + 0.5
, shape = alpha, rate = lambda
, log = F) -
pgamma(perInc_min[ipOr] - 0.5
, shape = alpha, rate = lambda
, log = F)
)
secondExp <- (
pgamma(perInc_max[ipOr] + 0.5
, shape = alpha, rate = lambda
, log = F) -
pgamma(perInc_max[ipOr] - 0.5
, shape = alpha, rate = lambda
, log = F)
)
intervalProb[ipOr] <- firstExp+secondExp
nll <- -sum(log(intervalProb))
return(nll)
}