Search code examples
rfor-looplimitmatrix-multiplicationprojection

How to set a variable limit on a specific row of a matrix within a list within a forloop


I have a list of matrices which I am iteratively matrix multiplying by an initial vector of numbers. I have a forloop to accomplish this, and am able to set an upper limit at an example flat rate (7,000) but would like to replace this with a variable upper limit which is sequentially defined by a pre-specified vector of numbers. see example code below for my existing code which works. In sum, I want to replace the 7,000 limit in the following code line with a vector of numbers for the 10,000 iterations of 50 years (50 matrices) contained within the list

allYearslist[[t]][4,i2[j]] <- ifelse (allYearslist[[t]][4,i2[j]] > 7000, 7000, allYearslist[[t]][4,i2[j]]) # to add density dependence to nth row

# loading packages
library(plyr)
library(popbio)
library(ggplot2)
library(tidyr)
library(dplyr)
library(reshape2)
library(simpleboot)
library(boot)
library(reshape)
library(vctrs)

# setting seed for replication purposes and creating function to project. replicating with bootstrap
set.seed(123)

# currently with 10,000 replications 
# vector of egg survival morts
egg.to.fry.s <- vec_rep(c(seq(from = 0.50, to = 0.99,by=0.01)),10000)

# vector of fry survival morts
fry.to.one.s <- vec_rep(c(seq(from = 0.40, to = 0.89,by=0.01)),10000)

# one.to.two rates
one.to.two.s <- rbeta(500000,10,2)


  A <- lapply(1:500000, function(x)  # construct list of matrices
    matrix(c(0, 0, 10, 10,
             0, 0, 0, 0,
             0, 0, 0, 0,
             0, 0, 0, 0), nrow = 4,ncol=4, byrow = TRUE, ))
  
  Anew <- A
  
  for(t in 1:length(Anew)) {
    Anew[[t]][2,1] <- egg.to.fry.s[t]
    Anew[[t]][3,2] <- fry.to.one.s[t]
    Anew[[t]][4,3] <- one.to.two.s[t]
  }
  
  AnewSplit <- split(Anew, rep(1:10000, each = 50)) # split list into lists to represent each sim
  
  n <- c(500,100,200,3000)  # initial vector of abundances
   
  nYears = 50  # define the number of years to project over
  
  allYears <- matrix(0,nrow=4,ncol=nYears+1)  # build a storage array for all abundances
  
  allYears[,1] <- n  # set the year 0 abundance 
  
  allYearsarray<-replicate(10000,allYears)
  allYearslist <- alply(allYearsarray,3)
  

  i1 <- length(allYearslist)   # list items to loop over
  i2 <- 2:ncol(allYearslist[[1]])    # number of columns in list to loop over

  # matrix multiply each list of 50 sequentially in AnewSplit by each list in allYearslist and each column - 1
  
  for(t in 1:i1) {
    for(j in seq_along(i2)){
    allYearslist[[t]][,i2[j]] <-  AnewSplit[[t]][[j]]%*% allYearslist[[t]][,i2[j]-1]
    allYearslist[[t]][4,i2[j]] <- ifelse (allYearslist[[t]][4,i2[j]] > 7000, 7000, allYearslist[[t]][4,i2[j]])  # to add density dependence to nth row
    }
  }

Solution

  • I believe I have answered my own question, if someone wants to verify. I believe it caps the indexed row before the next set of matrix multiplication commences...

      # variable density dependence
      dd <- seq(from=4000, to =4049, by = 1)
    
      # matrix multiply each list of 50 sequentially in AnewSplit by each list in allYearslist and each column - 1
      
      for(t in 1:i1) {
        for(j in seq_along(i2)){
        allYearslist[[t]][,i2[j]] <-  AnewSplit[[t]][[j]]%*% allYearslist[[t]][,i2[j]-1]
        allYearslist[[t]][4,i2[j]] <- ifelse(allYearslist[[t]][4,i2[j]] > dd[j], dd[j], allYearslist[[t]][4,i2[j]]) # to add variable density dependence to nth row
        
        }
      }