Ok then, here's what I generate :
#last element of a list
last<-function(l)
{l[length(l)]}
traj<-function(N,Q0,T,p){
X=c(0)
Q=c(Q0)
Y=c(0)
for (t in 0:N){
y=rbinom(1,1,p)
Y=c(Y,y)
Q=c(Q,ifelse (last(Q)>0,last(Q)-y,0))
if (t-T+2>0 && Q[t-T+2]>0 ) {Z=Y[t-T+2]}
else {Z=0}
X=c(X,last(X)+y-Z)
}
return(list("X"=X,"Q"=Q,"Y"=Y, "total admitted" = sum(Y),
"total treated"= ifelse(last(Q)>0, Q0-last(Q),Q0),
"time shortage" = ifelse(last(Q)>0,N,min(which(Q==0))),
"health rate"= ifelse(last(Q)>0, Q0-last(Q),Q0)/sum(Y)))
}
Here's what I want but with ggplot package (and geom_line) in a more elegant way.
palette=distinctColorPalette(8)
N=100
t=c(-1:N+1)
Q0=10
T=3
p=seq(0.2,0.9,0.1)
plot(t,traj(N,Q0,T,0.1)$X,type="l", ylim=c(0,80),xlab="t", ylab="Xt")
title(main="Trajectory according to p")
for (pbis in p){
y=traj(N,Q0,T,pbis)$X
lines(t,y, type="l",col=palette[which(p==pbis)])}
Doing so, I get all my curves with a different color. I would need a legend if it's not more complicated...
Hoping you have enough information now...
Thanks.