如何在 forloop 中的列表中的矩阵的特定行上设置变量限制
How to set a variable limit on a specific row of a matrix within a list within a forloop
我有一个矩阵列表,我用它迭代矩阵乘以一个初始数字向量。我有一个 forloop 来完成此操作,并且能够以示例固定费率 (7,000) 设置上限,但想用一个可变上限替换它,该上限由预先指定的数字向量顺序定义。请参阅下面的示例代码,了解我现有的有效代码。总之,我想用列表中包含的 50 年(50 个矩阵)的 10,000 次迭代的数字向量替换以下代码行中的 7,000 限制
allYearslist[[t]][4,i2[j]] <- ifelse (allYearslist[[t]][4,i2[j]] > 7000, 7000, allYearslist[[t]][4 ,i2[j]]) # 将密度依赖添加到第 n 行
# 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
}
}
我相信我已经回答了我自己的问题,如果有人想验证的话。我相信它会在下一组矩阵乘法开始之前限制索引行...
# 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
}
}
我有一个矩阵列表,我用它迭代矩阵乘以一个初始数字向量。我有一个 forloop 来完成此操作,并且能够以示例固定费率 (7,000) 设置上限,但想用一个可变上限替换它,该上限由预先指定的数字向量顺序定义。请参阅下面的示例代码,了解我现有的有效代码。总之,我想用列表中包含的 50 年(50 个矩阵)的 10,000 次迭代的数字向量替换以下代码行中的 7,000 限制
allYearslist[[t]][4,i2[j]] <- ifelse (allYearslist[[t]][4,i2[j]] > 7000, 7000, allYearslist[[t]][4 ,i2[j]]) # 将密度依赖添加到第 n 行
# 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
}
}
我相信我已经回答了我自己的问题,如果有人想验证的话。我相信它会在下一组矩阵乘法开始之前限制索引行...
# 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
}
}