Calculate population size with multiple sub-annual projection matrices

47 Views Asked by At

I have a population vector with juveniles and adults that I would like to record new population size after each sub-annual transition. The expected output would have the original population vector on the first row, and population at each following time step at the following row. I've modified the code presented at section 4 here but haven't arrived at what I need https://hankstevens.github.io/Primer-of-Ecology/DID.html The original algorithm use an annual projection matrix and project populations for 8 years.

A <- matrix(c(0, .3, 2, .7), nrow=2) # spring transition matrix 
B <- matrix(c(0.5, .3, 3, .7), nrow = 2) # summer transition matrix
C <- matrix(c(0, .3, 4, .7), nrow=2) # fall transition matrix 
D <- matrix(c(0.1, .1, 6, .7), nrow = 2) # winter transition matrix

N0 <- c(Juveniles=1,Adults=10) # initial population
steps <- 12 # number of time steps; each chain of 4 time step represent a year

My rough idea is to record population size at the end of each season on every row of the blank matrix N.

# with a column for each stage and a row for each time step
N <- rbind(N0, matrix(0, ncol=2, nrow=steps) )
# use a for-loop to project the population each season and store it.
for(t in 1:steps) {
  N[t+1,] <- A%*%N[t,]
  N[t+2,] <- B%*%A%*%N[t,]
  N[t+3,] <- C%*%B%*%A%*%N[t,]
  N[t+4,] <- D%*%C%*%B%*%A%*%N[t,]
N[t+5,] <- A%*%D%*%C%*%B%*%A%*%N[t,]

}

To continue, at N[t+6,], the population should be B%*%A%*%D%*%C%*%B%*%A%*%N[t,], and so on.

At this point, I got an error Error in D %*% C : requires numeric/complex matrix/vector arguments, which I don't understand what it means, and why my N[t+4,] and N[t+5,] were not calculated despite the supplied formulae.

Here is an incomplete table of N[t+i]

N
   Juveniles Adults
N0      1.00 10.000
       20.00  7.300
       31.90 11.110
       44.44 17.347
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000
        0.00  0.000

How do I change my code so that I don't have to spell out every multiplication chain? Thanks for stopping by my question.

0

There are 0 best solutions below