Hello,
I generated series of values called "betavh" which are to be used in a parameter set "paras" to give an output "sim". The output "sim" should be based on the number of "betavh" generated. Hence, I should have multiple plots for each value of "betavh". I've tried to use for loop for this, but I'm getting only one plot at the moment which is based on just one "betavh". Please kindly help with this. Thank you.
rlist=c(quote(dh*(Sh+Ih)),
quote(dh*Sh),
quote(betavh*Sh*G1*(Iv/(Sv+Iv))),
quote(dh * Ih),
quote(gh*Ih))
emat=matrix(c(1,0,
-1,0,
-1,1,
0,-1,
1,-1),
ncol=2, byrow=TRUE)
set.seed(2021)
library(philentropy)
x <- runif(4, 0, 50)
y <- runif(4, 0, 50)
testdata1 = cbind(x,y)
usdist = distance(testdata1, method = "euclidean")
library(geosphere)
theta = 0.000000002
pop = c(45151, 26741, 51612, 51565)
gravity = function(a,b, c, pop, distance) {
gravity = outer(pop^a, pop^b)/distance^c
diag(gravity) = 0
gravity
}
G1 = theta*gravity(1, 1, 2, pop, usdist)
tau=function(rateqs, eventmatrix, parameters,
initialvals, deltaT, endT){
time=seq(0, endT, by=deltaT)
res=data.frame(matrix(NA, ncol=length(initialvals)+1,
nrow=length(time)))
res[,1]=time
names(res)=c("time", names(inits))
res[1,]=c(0, inits)
for(i in 1:(length(time)-1)){
rat=sapply(rateqs, eval, as.list(c(parameters,
res[i,])))
rat[[3]] = mean(rat[[3]])
rat=unlist(rat)
evts=rpois(1, sum(rat)*deltaT)
if(evts>0){
whichevent=sample(1:nrow(eventmatrix), evts,
replace=TRUE, prob=c(rat))
mt=rbind(eventmatrix[whichevent,],
t(matrix(res[i,-1])))
mt=matrix(as.numeric(mt), ncol=ncol(mt))
res[i+1,-1]=apply(mt,2,sum)
res[i+1,][res[i+1,]<0]=0
}
else{
res[i+1,-1]=res[i,-1]
}}
return(res)
}
Sv = 150
Iv = 1
Sh = 685
Ih = 1
Sa = 254
Ia = 1
a=seq(1000, 5000, by=500)
b = t(a)
tp = t(b)
for(i in 1:ncol(tp)) {
betavh<- tp[,i]
paras = c(dh=0,betavh, gh = 0.0181)
inits = c(Sh = 685, Ih = 1)
}
number_of_iterations = 20
results = list()
for(i in 1:number_of_iterations){
#print('--------------------------------')
print(i)
set.seed(i)
st = Sys.time()
sim = tau(rlist, emat, paras, inits, 10/365,20)
results[[i]] = sim
#print(Sys.time() - st)
}
sim_i = results[[20]]
matplot(sim_i[,1],sim_i[,2:3], type="l", ylab="Numbers",
xlab="Time",col=c("black", "red"), lty=c(1,1),lwd=2)#, xlim=c(0,4000))
title(main = "Vectors")
legend("center", c("Sv", "Iv"), lty=c(1,1),lwd=2,
col=c("black", "red"))