-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeSolve.R
133 lines (111 loc) · 4.56 KB
/
deSolve.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
library(deSolve)
#S – proportion of susceptible individuals in total population
#I – proportion of infected individuals in total population
#R – proportion of recovered individuals in total population
# the initial state
#The initial conditions are set to have the proportion of the populationg being in the Susceptible group at >99.9% (1-1E-6 to be exact), the Infected group to be close to 0 (1E-6) and no one in the Recovered group.
SIR.model <- function(t, b, g) {
require(deSolve)
init <- c(S=1-1e-6,I=1e-6,R=0)
parameters <- c(bet=b,gamm=g)
time <- seq(0,t,by=t/(2*length(1:t)))
eqn <- function(time,state,parameters){
with(as.list(c(state,parameters)),{
dS <- -bet*S*I
dI <- bet*S*I-gamm*I
dR <- gamm*I
return(list(c(dS,dI,dR)))})
}
#Then we run the ode function based on the parameters we set above and save coerce the output as a data frame class.
out<-ode(y=init,times=time,eqn,parms=parameters)
#out.df<-as.data.frame(out)
SIR.model <- function(t, b, g){
require(deSolve)
init <- c(S=1-1e-6,I=1e-6,R=0)
parameters <- c(bet=b,gamm=g)
#time <- seq(0,t,by=t/(2*length(1:t)))
time <- seq(0,t,by=t/(length(1:t)))
eqn <- function(time,state,parameters){
with(as.list(c(state,parameters)),{
dS <- -bet*S*I
dI <- bet*S*I-gamm*I
dR <- gamm*I
return(list(c(dS,dI,dR)))})}
out<-ode(y=init,times=time,eqn,parms=parameters)
out.df<-as.data.frame(out)
require(ggplot2)
mytheme4 <- theme_bw() +
theme(text=element_text(colour="black")) +
theme(panel.grid = element_line(colour = "white")) +
theme(panel.background = element_rect(fill = "#B2B2B2"))
theme_set(mytheme4)
title <- bquote("SIR Model: Basic")
subtit <- bquote(list(beta==.(parameters[1]),~gamma==.(parameters[2])))
res<-ggplot(out.df,aes(x=time))+
theme_bw()+
ggtitle(bquote(atop(bold(.(title)),atop(bold(.(subtit))))))+
geom_line(aes(y=S,colour="Susceptible"))+
geom_line(aes(y=I,colour="Infected"))+
geom_line(aes(y=R,colour="Recovered"))+
ylab(label="Proportion")+
xlab(label="Time (days)")+
theme(legend.justification=c(1,0), legend.position=c(1,0.5))+
theme(legend.title=element_text(size=12,face="bold"),
legend.background = element_rect(fill='#FFFFFF',
size=0.5,linetype="solid"),
legend.text=element_text(size=10),
legend.key=element_rect(colour="#FFFFFF",
fill='#C2C2C2',
size=0.25,
linetype="solid"))+
scale_colour_manual("Compartments",
breaks=c("Susceptible","Infected","Recovered"),
values=c("blue","red","darkgreen"))
print(res)
ggsave(plot=res,
filename=paste0("SIRplot_","time",t,"beta",b,"gamma",g,".png"),
width=8,height=6,dpi=180)
pdf("SIR_plot.pdf")
print(res)
dev.off()
return(out.df)
}
coVS <- SIR.model(t=100, b=2.68/6.1, g=1/6.1)
coVS$number_of_infection <- coVS$I*58500000
head(prov_data)
out.df <- coVS
title <- bquote("SIR Model: Basic")
subtit <- bquote(list(beta==.(parameters[1]),~gamma==.(parameters[2])))
res<-ggplot(out.df,aes(x=time))+
ggtitle(bquote(atop(bold(.(title)),atop(bold(.(subtit))))))+
geom_line(aes(y=S,colour="Susceptible"))+
geom_line(aes(y=I,colour="Infected"))+
geom_line(aes(y=R,colour="Recovered"))+
ylab(label="Proportion")+
xlab(label="Time (days)")+
theme(legend.justification=c(1,0), legend.position=c(1,0.5))+
theme(legend.title=element_text(size=12,face="bold"),
legend.background = element_rect(fill='#FFFFFF',
size=0.5,linetype="solid"),
legend.text=element_text(size=10),
legend.key=element_rect(colour="#FFFFFF",
fill='#C2C2C2',
size=0.25,
linetype="solid"))+
scale_colour_manual("Compartments",
breaks=c("Susceptible","Infected","Recovered"),
values=c("blue","red","darkgreen"))
print(res)
ggsave(plot=res,
filename=paste0("SIRplot_","time",t,"beta",b,"gamma",g,".png"),
width=8,height=6,dpi=180)
pred_data <- coVS[, c("time","number_of_infection")]
#prov_data$cum_confirm <- as.numeric(prov_data$cum_confirm)
#colnames(prov_data)[2] <- "confirmed_patient_number"
pdf("Hubei_prediction.pdf")
ggplot(pred_data, aes(time, number_of_infection)) +
geom_col() +
theme_bw()+
theme(axis.text.x = element_text(angle = 90)) +
ggtitle(paste0("Hubei predicted patient number"))
dev.off()