0

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.

hnguyen
  • 772
  • 6
  • 17
  • Try `for(t in seq.int(steps)[1:(steps - 4)])`. – Rui Barradas May 24 '22 at 02:49
  • I'm wondering if there was a problem before the error condition occurred. There were only 1 juvenile to start and then there were 20. Is that what you expected? – IRTFM May 24 '22 at 03:45
  • What you need is a single line such as `N[t+1,] <- A%*%N[t,]` but with a modulo-4 selector that determines which of the transition matrices to use. Something like `N[t+1,] <- ML[[ (step%/% 4)+1]] %*% N[t,]` where ML is a list of 4 seasonal matrices. You should NOT have 4 separate assignments at each of the "steps". You also need to define your model. Are juveniles supposed to get created by last years adults? Do juveniles persist across multiple years? What mortality is expected for adults?, for juveniles? – IRTFM May 24 '22 at 06:04
  • @IRTFM: Thank you for all your concerns. These matrices are just dummies, so we can work on the mechanism. And based on the transition matrix, 20 juveniles should be at step 2, and so on. – hnguyen May 26 '22 at 03:26

0 Answers0