90 lines
2.6 KiB
R

## Set Working Directory to file directory - RStudio approach
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
seirds.f <- function(t, y, k) {
S <- y[1]
E <- y[2]
I <- y[3]
R <- y[4]
D <- y[5]
beta = k[1]
delta = k[2]
gamma_r = k[3]
gamma_d = k[4]
mu = k[5]
epsilon = k[6]
waning = k[7]
# Saying infec rate of S in contact with E same as contact with I
dS <- epsilon - (beta*E + beta*I)*S + waning*R - mu*S
dE <- beta*S*E + beta*S*I - (delta+mu)*E
dI <- delta*E - mu*I - gamma_r*I
dR <- gamma_r*I - (waning+mu)*R
dD <- gamma_d*I
return(as.matrix(c(dS, dE, dI, dR, dD)))
}
library(reticulate)
#conda_install(envname = "r-reticulate", packages="networkx")
#conda_install(envname = "r-reticulate", packages="matplotlib")
#conda_install(envname = "r-reticulate", packages="pydot")
#conda_install(envname = "r-reticulate", packages="pygraphviz")
source_python('prep_model.py')
# a) a=0.5, b=1, S(0)=0.9, I(0)=0.1, R(0)=0
seirds.params <- c(0.7316455696202532, # beta
0.020253164556962026, # delta
0.09113924050632911, # gamma_r
0.002531645569620253, # gamma_d
0.0034602076124567475, # mu
0.0, # epsilon
0.0) # waning
S <- 62
E <- 8
I <- 288
R <- 36
D <- 1
N <- S+E+I+R+D
S <- S/N
E <- E/N
I <- I/N
R <- R/N
D <- D/N
tmin <- 0
tmax <- 7
library(pracma)
seirds.ode.sol <- ode45(f = function(t,y){seirds.f(t,y,k=seirds.params)},
y = c(S, E, I, R, D),
t0 = tmin, tfinal = tmax)
# plot
library(reshape2)
library(ggplot2)
plot.seirds <- function(sol, method){
# - pred/prey columns melted to 1 column called "value"
sol.melted <- melt(data.frame(time=sol$t,
S=sol$y[,1],
E=sol$y[,2],
I=sol$y[,3],
R=sol$y[,4],
D=sol$y[,5]),
id = "time") # melt based on time
# New column created with variable names, called “variable”
colnames(sol.melted)[2] <- "Group" # used in legend
g <- ggplot(data = sol.melted,
aes(x = time, y = value, color = Group))
g <- g + geom_point(size=2)
g <- g + xlab("time") + ylab("Population")
g <- g + ggtitle(paste("SEIRDS Model Using", method))
show(g)
}
plot.seirds(seirds.ode.sol, "ODE45")
print(seirds.ode.sol$y[tmax,1]+seirds.ode.sol$y[tmax,2]+seirds.ode.sol$y[tmax,3]+seirds.ode.sol$y[tmax,4]+seirds.ode.sol$y[tmax,5])