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 record. A high score suggests that a 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.

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.

data("simulation_data")
head(simulation_data)
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.

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.

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)
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.

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.

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.

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))
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:

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:

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:

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:

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))
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



mcavallaro/rancovr documentation built on April 17, 2025, 7:21 p.m.