-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
135 lines (117 loc) · 3.73 KB
/
functions.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
## ---------------------------
##
## Script name: functions.R
##
## Purpose of script: Hold a bunch of functions for coronaRisk app
##
## Author: Ben Phillips
##
## Date Created: 2020-03-12
##
## Email: [email protected]
##
## ---------------------------
##
## Notes:
##
##
## --------------------------
## load up the packages we will need
## ---------------------------
## function definitions
# calculates a nation aggregate and appends to dataframe
natAgg <-function(tsDF){
cAgg <- tsSub(tsDF, subset = tsDF$Country.Region==tsDF$Country.Region[1])
dim(cAgg)<-c(1, length(cAgg))
cAgg<-data.frame(tsI[1, !dCols], cAgg)
cAgg$Province.State <- "National aggregate"
colnames(cAgg)<-colnames(tsDF)
rbind(cAgg, tsDF)
}
#splits symptomatic into into critical, severe and mild
#accounts for detection, and
# moves them forward tth = time to hospitalisation
splitter <- function(active, time, tth = 6, fracSev = 0.15, fracCrit = 0.05, det = 0.36){
newCases <- c(0, diff(active))
nn <- length(newCases)
newCases[newCases<0]<-0 # catch negatives
newCases <- newCases/det # account for detection
hTime <- time + tth # expected hositalisation date
sev <- fracSev*newCases
sev <- cumsum(sev)
sev <- sev-c(rep(0, 6), sev[-((nn-5):nn)]) #discharge after 6 days
crit <- fracCrit*newCases
crit <- cumsum(crit)
crit <-crit - c(rep(0, 14), crit[-((nn-13):nn)])
list(date = hTime, severe = sev, critical = crit)
}
# calculates doubling time over the last inWindow days.
doubTime <- function(cases, time, inWindow = 10){
r <- projSimpleSlope(cases, time, inWindow = inWindow)[2]
log(2)/r
}
# growth rate
growthRate <- function(cases, inWindow=10){
nn <- length(cases)
ss <- (nn - inWindow + 1):nn
rate <- numeric(length(ss))
rate[ss] <- 100*(cases[ss] - cases[ss-1]) / cases[ss-1]
}
# calculates the curve flatenning index.
# it is the second derivative of logA wrt t (the change in growth rate) divided by first differential (the current growth rate).
cfi <- function(active){
lnact <-log(active)
cfiInd <- -diff(diff(lnact))/abs(diff(lnact)[-1])
cfiInd[abs(cfiInd)>10]<-NA # remove crazy values associated with changed test/diagnosis
cfiInd
}
# estimates detection rate based on assumptions about cfr, ttd
detRate<-function(infd, deaths, cfr = 0.033, ttd=17, window=5){
obs<-c(rep(NA, window), diff(infd, window)) # observed new cases
deathDiff<-diff(deaths, window) # observed new deaths
expd<-deathDiff/cfr #expected new cases given cfr
expd<-expd[-(1:(ttd-window))]
expd<-c(expd, rep(NA, ttd))
detRate<-obs/expd
detRate[detRate==0]<-NA
detRate[is.infinite(detRate)]<-NA
out<-mean(detRate, na.rm = TRUE)
if (is.nan(out)) return(NA)
if (out>1) out<-1
out
}
# Simple projection based on growth over last inWindow days
# returns extended plotting data
projSimple<-function(rawN, rawTime, inWindow=10, extWindow = 10){
nn <- length(rawN)
ss <- (nn-inWindow+1):nn
x <- c(rawTime[ss], rawTime[nn]+1:extWindow)
lnN <- log(rawN[ss])
lnN[is.infinite(lnN)]<-NA
tIn <- rawTime[ss]
mFit <- lm(lnN~tIn)
extFit <- predict(mFit, newdata = list(tIn = x), interval = "confidence")
y <- exp(extFit)
list(x=x, y=y)
}
# Simple projection based on growth over last inWindow days
# returns coefficients
projSimpleSlope<-function(rawN, rawTime, inWindow=10){
nn <- length(rawN)
ss <- (nn-inWindow+1):nn
x <- c(rawTime[ss], rawTime[nn]+1:inWindow)
lnN <- log(rawN[ss])
lnN[is.infinite(lnN)]<-NA
tIn <- rawTime[ss]
mFit <- lm(lnN~tIn)
coefficients(mFit)
}
# to identify the date columns in ts dataframes
dateCols<-function(x){
grepl(pattern = "\\d", x = colnames(x))
}
# To subset time series data and aggregate totals
tsSub <- function(x, subset){
xSub<-x[subset, dateCols(x)]
colSums(xSub)
}