/
ExpFit.R
148 lines (136 loc) · 6.57 KB
/
ExpFit.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
## Code adapted from https://github.com/ICI3D/ebola-forecast/ (Carl Pearson)
## Steve Bellan 2015
library(boot); library(ggplot2); require(data.table)
load(file='data/cleanSLData.Rdata')
param.xform <- function(x) c(x['rate_0'], decay_rate = as.numeric(exp(x['logdecay_rate'])), nbsize = as.numeric(exp(x['lognbsize'])))
ref.guess <- c(rate_0=30, logdecay_rate=log(0.01), lognbsize=log(1.2))
## analysis functions
llgenerator <- function(ratefun, logprobfun) return(
function(params, cases, times) {
ps <- param.xform(params)
ll <- sum(logprobfun(ps, ratefun(ps, times), cases))
return(ll)
}
)
exp_decay <- function(params, times) params["rate_0"]*exp(-params["decay_rate"]*times)
pois_cases <- function(params, rates, cases) {
out <- dpois(cases, lambda=rates, log=TRUE)
## trates <<- rates
return(out)
}
nbinom_cases <- function(params, rates, cases) {
out <- dnbinom(cases, size=params["nbsize"], mu=rates, log=TRUE)
## trates <<- rates
## print(out)
return(out)
}
exp_pois_ll <- llgenerator(exp_decay, pois_cases)
exp_nbinom_ll <- llgenerator(exp_decay, nbinom_cases)
mod_exp_decay <- function(params, times) params["rate_0"]*exp(- (params["decay_rate"]*times)^params["shape_parameter"])
mod_exp_pois_ll <- llgenerator(mod_exp_decay, pois_cases)
flat_model <- function(params, times) {
rep(params["rate_0"],length(times))
}
flat_pois_ll <- llgenerator(flat_model, pois_cases)
expcurve <- function(optimresults, date_zero, params.xform = function(x) x) with(optimresults, {
ps <- params.xform(par)
return(function(d) {
t <- d - date_zero
ps["rate_0"]*exp(-ps["decay_rate"]*t)
})
})
params <- function(src, start_date, end_date,
param.guess = ref.guess, parscale = c(1, rep(1/10, length(param.guess)-1)),
ll = 'exp_nbinom_ll') {
slice <- src[(start_date <= src$Date) & (src$Date <= end_date),] ## data.table doesn't work with shiny for some reason?
slice$t <- as.numeric(slice$Date - min(slice$Date))
if(sum(slice$int>1)>0) print('not always daily incidence')
llfxn <- get(ll)
res <- optim(param.guess, llfxn, gr=NULL, cases = slice$cases, times = slice$t, control=list(fnscale=-1, parscale=parscale), hessian=TRUE)
vcmat <- solve(-res$hessian)
res$par <- param.xform(res$par)
cis <- with(res,{
offset <- 1.96*sqrt(vcmat[2,2])
par["decay_rate"] + c(low = offset, est=0, hi = -offset) # hi b = hi decay = low time
})
list(t0 = start_date, par=res$par, cis=cis)
}
doProj <- function(src, forecast_date = as.Date('2015-12-31'), max_censor_interval = 14,
minDecayRate = .01, ll = 'exp_nbinom_ll', model = expcurve) {
endDate <- max(src$Date)
maxcases <- src[Date <= (endDate - max_censor_interval), max(cases)]
startDate <- src[cases == maxcases, max(Date)]
fit_ref <- params(src, startDate, endDate, ll = ll)
fit_ref$par['decay_rate'] <- max(fit_ref$par['decay_rate'], minDecayRate)
## fill in dates from the beginning of data to end of forecast
src <- merge(src, data.table(Date = seq(min(src$Date), forecast_date, by='day')), by = 'Date', all=T)
src[Date < endDate & is.na(cases), cases:=0] ## fill in dates with missing values for plotting
src[, reg := reg[1]]
src$days <- as.numeric(src$Date)
src$fit <- model(fit_ref, date_zero = src[Date==startDate, days])(src$days)
return(list(
fit = fit_ref, src = src[,list(Date,reg, cases,fit)],
startDate = startDate, endDate = endDate, include_interval=endDate-startDate,
model = model
))
}
forecast <- function(fit, main=NULL, nbsize = NULL, doPlot = T, xticks = T, ylim = NULL, xlim = NULL, moreDays=90) with(fit, {
if(!is.null(xlim)) {
startX <- xlim[1]
endX <- xlim[2]
}else{
startX <- startDate - beforeDays
endX <- endDate + moreDays
xlim <- c(startX, endX)
}
if(is.null(nbsize)) nbsize <- fit$par$nbsize ## if not specified in arguments
src$proj <- src[, rnbinom(length(fit), mu = fit, size = nbsize)]
if(doPlot) {
src[Date < endDate & Date > startX, plot(Date, cases, type = 'h', bty = 'n', las = 2, xaxt = 'n',
xlim=xlim, ylim=ylim, xlab = '', lwd = 3)]
rgDates <- seq.Date(startX, endX, by = 1)
seqDates <- rgDates[format(rgDates, '%d') %in% c('01')]
seqDatesMid <- rgDates[format(rgDates, '%d') %in% c('15')]
axis.Date(1, at = seqDatesMid, labels = NA)
if(xticks) axis.Date(1, at = seqDates, format = '%b', las = 2, tck=0)
rect(startDate, 0, endDate, par('usr')[4], col = rgb(0,.5,0,.3), border=NA)
title(main)
src[Date > endDate, lines(Date, proj, type = 'h', lwd = 3, col = 'red')]
src[Date > startDate, lines(Date, fit, lty = 1, col = 'dodger blue', lwd = 2)]
rect(endDate, 0, endDate + moreDays, par('usr')[4], col = rgb(0.5,0,0,.3), border=NA)
text(startDate+include_interval/2, par('usr')[4], 'fitting', pos=1)
text(endDate+moreDays/2, par('usr')[4], 'forecasting', pos=1)
}
return(src)
})
createHazTrajFromSLProjection <- function(fits, nbsize = 1.2, trialStartDate = as.Date('2015-02-01'), xlim = as.Date(c('2014-09-15','2015-12-01')),
propInTrial = .03, numClus = 20, clusSize = 300, weeks = T, verbose=0) {
hazTList <- NULL
if(verbose>20) browser()
for(cc in 1:numClus) {
fit <- fits[[sample(regs, 1)]]
src <- forecast(fit, doPlot = F, nbsize = nbsize, xlim = xlim)
src$day <- src[, as.numeric(Date - trialStartDate)]
lastDataDay <- src[max(which(!is.na(src$cases))), day]
src[day < lastDataDay & is.na(cases), cases := 0] ## fill in over interval without reporting
src$haz <- src$cases
src[day > lastDataDay, haz := proj]
src[, haz := haz * propInTrial / clusSize]
src[day > -60, list(day, haz)]
if(weeks) {
src$week <- src[, floor(day/7)]
src <- src[, list(haz = mean(haz, na.rm=T), Date = min(Date)), week] ## get mean daily hazard by week
}
src$cluster <- cc
hazTList[[cc]] <- as.data.frame(src)
}
hazT <- rbindlist(hazTList)
setnames(hazT, 'haz', 'clusHaz')
hazT$day <- hazT[, week*7]
setkey(hazT, day, cluster)
return(hazT[, list(cluster, day, Date, clusHaz)])
}
makeTransparent<-function(someColor, alpha=150) {
newColor<-col2rgb(someColor)
apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],
blue=curcoldata[3],alpha=alpha, maxColorValue=255)})}