dimanche 19 avril 2020

Why is this condition not logical?

first question here so please be indulgent on the form. I am trying to know the length of a vector full of decimals if I keep only the integers, what include erasing the integers when they already exist. I created a if function to keep certain lines within a "row to keep" vector if they fit the condition, which is a boolean. Unfortunately, after a certain while in the for loops where the if function stands, the condition is NA, so this message returns : error in if missing value where true/false needed. Any idead why? Thanks for your help and please give me advice if needed for an eventual question to be more clear

epidemies_time <-c(1:epidemies_nb)
time_code <- for (epid_idx in 1:epidemies_nb)
{
  time_trunc<-trunc(time)
row_to_keep<-rep(FALSE,taille)
row_to_keep[1] <- TRUE
dayafterday <- for (time_idx in 2:taille) {
if  (time_trunc[time_idx] != time_trunc[time_idx-1]) {`
  row_to_keep[time_idx-1] <- TRUE
  if (time_idx > 2) {
    time_trunc[time_idx-1] <- time_trunc[time_idx-1] + 1
  }
  time_trunc <- time_trunc[row_to_keep]
  duree<- length(time_trunc)
  epidemies_time[epid_idx]<-duree
} } }

PS : I skipped all the useless part of the script in that problem, but if you need it or if you are curious on how do I get the "time" vector, the entire code also lies down here, but I'm sure it works

suscept<-rep(0,taille)
infect<-rep(0,taille)
remov<-rep(0,taille)
time<-rep(0,taille)
nvcas<-rep(0,taille)
pop_susc <- 0.1
infect[1]<-5
suscept[1]<- pop_susc*taille - infect[1]
remov[1]<- taille - infect[1]+suscept[1]
beta<-0.0000524  
alpha<-0.4
epidemies_nb <- 60
epidemies_time <-c(1:epidemies_nb)
time_code <- for (epid_idx in 1:epidemies_nb) 
{
  modele_epid <- for(ind_idx in 1:(taille-1))
  {
    if(infect[ind_idx]!=0)
    {
      s<--log(runif(1))/(beta*suscept[ind_idx]*infect[ind_idx]+alpha*infect[ind_idx])
      time[ind_idx+1]<-time[ind_idx]+s
      rand<-runif(1)
      proba<-beta*suscept[ind_idx]*infect[ind_idx]/(beta*suscept[ind_idx]*infect[ind_idx]+alpha*infect[ind_idx])
      if (rand<=proba)
      {
        suscept[ind_idx+1]<-suscept[ind_idx]-1
        infect[ind_idx+1]<-infect[ind_idx]+1
        remov[ind_idx+1]<-remov[ind_idx]
        nvcas[ind_idx+1]<-1
      }
      if (rand>proba)
      {
        suscept[ind_idx+1]<-suscept[ind_idx]
        infect[ind_idx+1]<-infect[ind_idx]-1
        remov[ind_idx+1]<-remov[ind_idx]+1
        nvcas[ind_idx+1]<-0
      }
    }
  }

  time_trunc<-trunc(time)
  row_to_keep<-rep(FALSE,taille)
  row_to_keep[1] <- TRUE
  dayafterday <- for (time_idx in 2:taille) {
    if  (time_trunc[time_idx] != time_trunc[time_idx-1]) {
      row_to_keep[time_idx-1] <- TRUE
      if (time_idx > 2) {
        time_trunc[time_idx-1] <- time_trunc[time_idx-1] + 1
      }
      time_trunc <- time_trunc[row_to_keep]
      duree<- length(time_trunc)
      epidemies_time[epid_idx]<-duree
    }
  }
}
epidemies_time```


Aucun commentaire:

Enregistrer un commentaire