 # Using multiple values in a for loop

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[] = mean(rat[])

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[]
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"))``````
``````for (i in seq_along(results)){
sim_i <- results[[i]]
if(i==1)
matplot(sim_i[,1],sim_i[,2:3], type="l", ylab="Numbers",
xlab="Time",col=c("black", "red"), lty=c(1,1),lwd=2)
else matlines(sim_i[,1],sim_i[,2:3], type="l", ylab="Numbers",
xlab="Time",col=c("black", "red"), lty=c(1,1),lwd=2)

}
title(main = "Vectors")
legend("center", c("Sv", "Iv"), lty=c(1,1),lwd=2,
col=c("black", "red"))``````

Hello @ nirgrahamuk,

Many thanks for this, but it seems the multiple plots came from the 20 iterations and not from the "betavh" values. The number of "betavh" values will determine the number of lines in the plot. So, if we have 7 "betavh" values, we should have 7 lines in the plot. In essence, I am interested in knowing the outputs when "betavh" is varied. Hope this helps. Thanks so much.

So where are the betavh values in the results you want to use . Do I have to study your tau function in depth. Or can you make objects with names so its easier ?

Sorry if it wasn't clear. For example "betavh" is given below:. Parameters, "paras" uses the "betavh" values to obtain the results "sim"

``````betavh = seq(1000, 5000, by=500)
paras = c(dh=0,betavh, gh = 0.0181)
inits = c(Sh = 685, Ih = 1)
``````

For instance,
when betavh = 1000, paras = c(dh=0, betavh=1000, gh=0.0181)
when betavh = 1500, paras = c(dh=0, betavh=1500, gh = 0.0181)
when betavh = 2000, paras = c(dh=0, betavh=2000, gh = 0.0181)
when betavh = 2500, paras = c(dh=0, betavh=2500, gh = 0.0181)
etc
I need to put this in a for loop so that there will be different "betavh" values in the parameters, "paras", but don't know how. Different values of "betavh" used will lead to multiple plots.