-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
193 lines (148 loc) · 7.13 KB
/
README.Rmd
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
title: "rancovr. Cluster detection with Random Neighbourhood Covering"
author: Massimo Cavallaro
output:
md_document:
variant: markdown_github
---
```{r eval=TRUE, include=FALSE}
knitr::opts_knit$set(root.dir = "~/Documents/rancovr")
devtools::load_all(".")
```
# Rancovr: Cluster detection in R with Random Neighbourhood Covering
`rancovr` is a statistical software package written in R for disease cluster
and anomaly detection.
It implements the Random Neighbourhood Covering (RaNCover) approach of reference [1].
RaNCover assigns a score $w \in [0,1]$ to each records.
A high score suggests that the record is likely to be part of a cluster (e.g., it is an infection case caused by a local outbreak),
while a low score suggests that the record is consistent with a baseline of sporadic cases.
```{r eval=FALSE, include=TRUE}
install.packages("devtools")
devtools::install_github("mcavallaro/rancovr")
```
As a demonstration, we consider the spatio-temporal coordinates in `Data/synthetic_dataset.csv`.
The entries of this data set represent (simulated) locations and detection dates of infected patients generated by an endemic disease (`end.`) and
cases due to a local outbreak (`epi.`) in England.
See also reference [1] for the simulation details.
```{r}
data("simulation_data")
head(simulation_data)
```
```{r}
data("GB_region_boundaries")
plotBaseMap(add=F, xlim=range(simulation_data$longitude), ylim=range(simulation_data$latitude))
points(simulation_data$longitude, simulation_data$latitude,
col=ifelse(simulation_data$sim=='epi.', tab.red, tab.blue),
pch=ifelse(simulation_data$sim=='epi.', 1, 20),
cex=ifelse(simulation_data$sim=='epi.', 0.6, 0.2))
legend('topright',c('end.','epi.'), pch=c(20,1), col=c(tab.blue, tab.red))
```
For convenience, all observations are arranged in a `sparseMatrix` object named `observation.matrix` and saved on disk.
```{r}
CreateObservationMatrices(simulation_data)
```
Observations must be compared with an appropriate baseline model. If the numbers
of these observations significantly exceeded the model prediction, an outbreak might be occurring. Estimating the baseline involves finding a temporal trend (using the function `TimeFactor`) and a spatial trend based on the spatial population distribution.
```{r}
load(file.path(getwd(), "observation_matrix.RData"), verbose=1)
time.factor = TimeFactor(simulation_data, n.iterations=5)
baseline.matrix = CreateBaselineMatrix(simulation_data, save.on.dir = T)
```
```{r}
load(file.path(getwd(), "observation_matrix.RData"), verbose=1)
plot(time.factor, xlab = 'Week', ylab='Number of cases', xaxt='n')
# lines(colSums(baseline.matrix))
points(Matrix::colSums(observation.matrix), pch='+')
axis(side=1, at=1:length(time.factor), labels = names(time.factor))
legend('bottomright',legend=c('Baseline', 'Observations'), pch=c('o', '+'))
```
Create 100,000 cylinders to cover the observed cases using the estimated baseline.
```{r}
cylinders = CreateCylinders(observation.matrix, baseline.matrix, week.range = c(0,99), n.cylinders = 100000)
head(cylinders)
```
Some cylinders contain much more cases than the baseline predicts. These cylinders cover epidemic (outbreak) events.
```{r}
plotCylindersCI(cylinders, confidence.level = 0.95)
```
The "true" baseline matrix used to generate the endemic events is available as `data()`.
Let's use it in place of the estimated baseline matrix.
Notice that the true baseline matrix has higher dimensionality than the estimated baseline matrix (it includes entries for more postcodes and times) and requires a matching observation matrix.
```{r}
print(dim(baseline.matrix))
print(dim(observation.matrix))
data(baseline_for_sim)
print(dim(baseline_for_sim))
CreateObservationMatrices(simulation_data,
more.postcodes=rownames(baseline_for_sim),
more.weeks=colnames(baseline_for_sim))
load("/home/massimo/Documents/rancovr/observation_matrix.RData", verbose=1)
print(dim(observation.matrix))
```
```{r}
cylinders.2 = CreateCylinders(observation.matrix, baseline_for_sim, week.range = c(0,99), n.cylinders = 100000)
head(cylinders.2)
plotCylindersCI(cylinders.2, confidence.level = 0.95)
```
Compute the warning scores for each case:
```{r}
simulation_data[,'warning.score'] = apply(simulation_data, 1, FUN=warning.score, cylinders)
simulation_data[,'warning.score.2'] = apply(simulation_data, 1, FUN=warning.score, cylinders.2)
head(simulation_data)
```
Assess concordance with ROC-AUC:
```{r}
library(pROC)
ROC = roc(ifelse(simulation_data$sim == 'end.', FALSE, TRUE), simulation_data$warning.score)
plot(ROC)
print(ROC$auc)
ROC = roc(ifelse(simulation_data$sim == 'end.', FALSE, TRUE), simulation_data$warning.score.2)
plot(ROC, add=T, col='red')
print(ROC$auc)
legend('bottomright', legend = c('Using estimated baseline', 'Using true baseline'), lty=1, col=c('black','red'))
```
With mean squared error:
```{r}
simulation_data$Y = ifelse(simulation_data$sim == 'epi.',1,0)
simulation_data$sqerr = (simulation_data$Y - simulation_data$warning.score)^2
cat("MSE using estimated baseline:", mean(simulation_data$sqerr), '\n')
simulation_data$sqerr.2 = (simulation_data$Y - simulation_data$warning.score.2)^2
cat("MSE using true baseline:", mean(simulation_data$sqerr.2), '\n')
```
And with a map:
```{r}
data("GB_region_boundaries")
#plotBaseMap(add=F, xlim=c(-0.6,0.6), ylim=c(51.648,51.65))
plotBaseMap(add=F, xlim=c(-1,1), ylim=c(50.648,52.65))
points(simulation_data$longitude, simulation_data$latitude,
col=ifelse(simulation_data$sim=='epi.', tab.red, tab.blue),
pch=ifelse(simulation_data$sim=='epi.', 4, 20),
cex=ifelse(simulation_data$sim=='epi.', 1, 0.5))
points(simulation_data[simulation_data$warning.score>0.95,]$longitude,
simulation_data[simulation_data$warning.score>0.95,]$latitude,
col=tab.orange,
pch=1,
cex=1)
# case.df$color = rgb(colorRamp(c("blue", "red"))(case.df$warning.score) / 255)
# plot(case.df$longitude, case.df$latitude, col=case.df$color)
legend('topright',c('end.','true epi.', 'w>0.95'), pch=c(20,4,1), col=c(tab.blue, tab.red, tab.orange))
```
```{r}
plotBaseMap(add=F, xlim=c(-0.6,0.6), ylim=c(51.648,51.65))
#plotBaseMap(add=F, xlim=c(-1,1), ylim=c(50.648,52.65))
points(simulation_data$longitude, simulation_data$latitude,
col=ifelse(simulation_data$sim=='epi.', tab.red, tab.blue),
pch=ifelse(simulation_data$sim=='epi.', 4, 20),
cex=ifelse(simulation_data$sim=='epi.', 1, 0.5))
points(simulation_data[simulation_data$warning.score.2>0.95,]$longitude,
simulation_data[simulation_data$warning.score.2>0.95,]$latitude,
col=tab.orange,
pch=1,
cex=1)
# case.df$color = rgb(colorRamp(c("blue", "red"))(case.df$warning.score) / 255)
# plot(case.df$longitude, case.df$latitude, col=case.df$color)
legend('topright',c('end.','true epi.', 'w>0.95'), pch=c(20,4,1), col=c(tab.blue, tab.red, tab.orange))
```
[1] M. Cavallaro, J. Coelho, D. Ready, V. Decraene, T. Lamagni, N. D. McCarthy, D. Todkill, M. J. Keeling (2022)
Cluster detection with random neighbourhood covering: Application to invasive Group A Streptococcal disease. PLoS Comput Biol 18(11): e1010726.
https://doi.org/10.1371/journal.pcbi.1010726