Tracking infected individuals in a population - matrix model
<p>I've developed a simple age-structured population model to predict future population size in the event a disease is introduced. The disease causes individuals to die 1-3 years after being infected so I'm struggling with how to keep track of infected animals for up to 3 years within the single-stage matrix (year-to-year) model. In other words, how could my existing codes be modified to make sure that the infected animals that are still alive during their third year of infection are removed from the population? Any tips would be greatly appreciated. </p>

<pre><code>sum_matd &lt;- matrix(rep(0,5*5),nrow=5)  #matrix for 4 age classes

#model specs
fecundity&lt;- c(0.5) #fecundity
natsurv=0.7 #natural survival rate
prevalence&lt;-c(0.01) #percent of population infected
mort=c(0.33,0.5,1) #mortality rate for infected animals

risk1=0.1   #distribution of infection by age class
risk2=0.2
risk3=0.3
risk4=0.4

nyears &lt;- 15

results&lt;-list()

for (i in 1:nyears)
{
  if(i==1){

    baby=0              #number of animals in population          
    female1=1362                        
    female2=888
    female3=754
    female4=599

  }else{
    baby=0                    #number of animals in population for year +1 
    female1=endpop[1]               
    female2=endpop[6]
    female3=endpop[11]
    female4=endpop[16]+endpop[21] 
}

  initialpop= c(baby,baby,baby,baby,baby, #vector of population age structure at the beginning
             female1,female1,female1,female1,female1,
             female2,female2,female2,female2,female2,
             female3,female3,female3,female3,female3,
             female4,female4,female4,female4,female4)

  #leslie matrix 
  sum_matd[1,]=c(0,natsurv*fecundity,natsurv*fecundity,natsurv*fecundity,natsurv*fecundity) #offspring produced for each age-class
  sum_matd[2,]=c(0,natsurv,0,0,0) 
  sum_matd[3,]=c(0,0,natsurv,0,0)
  sum_matd[4,]=c(0,0,0,natsurv,0)
  sum_matd[5,]=c(0,0,0,0,natsurv)
  popA=initialpop*sum_matd #updated population after mortality and fecundity                    

  sickpoplevel&lt;-prevalence * (sum(popA)) #number of sick animals

  sickagelevel&lt;-c(0,sickpoplevel*risk1,sickpoplevel*risk2,sickpoplevel*risk3,sickpoplevel*risk4) #assign sick age class per risk

  died1&lt;- sickagelevel[2] * (sample (mort,1)) #sick that died based on randomly selected mortality
  died2&lt;- sickagelevel[3] * (sample (mort,1))
  died3&lt;- sickagelevel[4] * (sample (mort,1))
  died4&lt;- sickagelevel[5] * (sample (mort,1))

  infected1&lt;- sickagelevel[2] - died1  # sick but not dead
  infected2&lt;- sickagelevel[3] - died2  # I need to keep track of the number of infected animals
  infected3&lt;- sickagelevel[4] - died3  # because they die 1-3 years of being infected
  infected4&lt;- sickagelevel[5] - died4

  healthybaby   =sum(popA[1,])    
  healthyfemale1=sum(popA[2,])  - died1 #remove mortalties from population
  healthyfemale2=sum(popA[3,])  - died2
  healthyfemale3=sum(popA[4,])  - died3
  healthyfemale4=sum(popA[5,])  - died4

  endpop=c(rep(healthybaby,5),rep(healthyfemale1,5),rep(healthyfemale2,5),rep(healthyfemale3,5),rep(healthyfemale4,5)) 

}
</code></pre>