I am trying to build a model that allows me to estimate the spread of disease in the population. The setup is similar to the SIR model, with the difference that this model includes the stochastic component.
I have written an algorithm to carry out the simulation, but the lead times are really high. Could you please help me optimize the code? Below is the code.
Population = 100000
Time = 365
r_0 = 0.02
start_positive = 100
min_positive_days = 5
max_positive_days = 15
Pop_over_time <- matrix("S", ncol = Time, nrow = Population, dimnames = list(1:Population, paste0("T",1:Time)))
I0 <- sample(Population, start_positive)
for (i in I0) {
Pop_over_time[i, 1] <- "I"
nr <- trunc(runif(1, min_positive_days, max_positive_days))
Pop_over_time[i, 1:nr] <- "I"
Pop_over_time[i, (nr):(Time)] <- "R"
}
for (i in 2:Time) {
r_s <- r_0 * sqrt(2 + sin(500 + i * 2 * pi / 365)) / 2
for (j in 1:Population) {
if (Pop_over_time[j, i - 1] == "I") {
for (z in (1:k[j])) {
if (Pop_over_time[Link[j, z], i - 1] == "S") {
Pop_over_time[Link[j, z], i] <- rbinom(1, 1, r_s)
if (Pop_over_time[Link[j, z], i] == 1) {
nr <- trunc(runif(1, min_positive_days, max_positive_days))
nr_min <- min((i + nr - 1), Time)
Pop_over_time[Link[j, z], i:nr_min] <-
rep(1, length(i:nr_min))
Pop_over_time[Link[j, z], (nr_min):(Time)] <- "R"
}
}
}
}
}
Pop_over_time[, i][Pop_over_time[, i] == 1] <- "I"
Pop_over_time[, i][Pop_over_time[, i] == 0] <- "S"
}
In a first part I create a matrix (Link) that defines all the links within the population. So first I define variables, then I create a matrix that saves, for each day, the situation of the population. Then I define the first 100 cases (with a sampling) and after that the heaviest part of the code starts. r_s (the probability of contagion) is defined in a seasonal way. So, for each member of the population that I infect I generate a binomial variable for each of his contacts: if the number generated is 1 then the infection has occurred, vice versa not.
Here you can download the .rdata file to run the code.
readRDS
? \$\endgroup\$