samedi 9 janvier 2016

How to do a checker in R that will have the step if a stock value is negative?

I am trying to create an SIR model with R. I have the SIR model working and a sensory function that is doing 100 runs of the model with different parameter values. I want to have a checker in model that will check if any of my stock values is negative and if they are the step in my simtime will be halved. I tried doing a checker like this:

o<-data.frame(ode(y=init, simtime, func = sir, 
                  parms=auxs, method="euler"))

STEP <- if(o$S<0 || o$I<0 || o$R<0) STEP*0.5

This didn't work but I presume it will be something like this. The problem is I am not sure how to adjust simtime after it is created. I am new to working with R so any help would be appreciated.

Here's the code I have for my SIR function, I was thinking the checker may need to added in here but I do not know how to implement it.

sir <- function(time, state, auxs) {
  with(as.list(c(state, auxs)), {
    dS <- -(Ce/population) * S *I
    dI <- (Ce/population) * S * I - (I/Rdelay)
    dR <- (I/Rdelay)



    return(list(c(dS,dI,dR)))
  })
}

Aucun commentaire:

Enregistrer un commentaire