基于向量中的概率应用条件
Apply condition based on probability in vector
抱歉,这有点乱,但正在寻求一些建议。
我正在尝试模拟具有环境随机性的人口增长。我创建了一个概率向量,我使用的 10 个补丁大小中的任何一个都会经历灾难性事件,这将使人口规模减少 75%。我想在一个函数中应用它。所以基本上我需要有函数 运行 并确定下一步的人口规模,然后应用该补丁大小的概率来确定在新的人口规模存储之前灾难性事件是否会发生矩阵。
所以我基本上想做类似 If/Then 的事情,但我不想定义 "If" 参数,而是想应用存储的概率。我四处搜寻,但运气不佳,但我认为这不会那么困难。谢谢!
d0 <- c(0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05 ) # chance that a catastrophic
# disturbance will reduce population for each patch size, assuming that rates of disturbance
# are much higher in small patches.
cat <- .25 # disturbance factor, assume that during catastrophic event 2/3 of animals are
#removed, I want to multiply this by the population size within each time step at the frequency
#defined in d0. I could probably make this into a function but I still need to know
# how to use the probability to decide when to apply it.
# Ricker model (N_t+1=N_t*exp(r*(1-N_t/K)))
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
nt1 = ((nt1*cat)) ### Here I would apply the probability, and when necessary the
# disturbance factor. I.E. Breeding season happens then there is a very harsh winter
# and many individuals die.
return(nt1) #return the value of (Nt+1)
}
for(t in 1:(tf-1)) { #loop through time
n[t+1,] = Ricker(n[t,],r,k0) #step through Ricker
}
我最终做了一些类似于@Marius 建议的事情,它似乎工作得很好,感谢大家的投入!
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
for(d in 1:(length(d0))) { # Create loop to test each patch for disturbance probability
dice_rolls = runif(length(d0)) # Generate random uniforms for each element in d0.
nt1 = ifelse(dice_rolls < d0, nt1 * cat, nt1) # If the'dice roll' is less than the corresponding element of d0 # the patch experiences the disturbance
}
return(nt1) #return the value of (Nt+1)
}
如果我对你的模型的理解正确,你想要:
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
# Generate random uniforms for each element in d0. If the
# 'dice roll' is less than the corresponding element of d0,
# the patch experiences the disturbance
dice_rolls = runif(length(d0))
nt1 = ifelse(dice_rolls < d0, nt1 * cat, nt1)
return(nt1) #return the value of (Nt+1)
}
要了解这是如何工作的,请查看 dice_rolls < d0
如何运行的简单模拟:您可以看到它分别处理 d0
的每个元素,并且
生成近似所需概率的长期 运行 平均值:
d0 <- c(0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05)
n_catastrophes = numeric(length = length(d0))
for (sim_num in 1:1000) {
dice_rolls = runif(length(d0))
n_catastrophes = n_catastrophes + (dice_rolls < d0)
}
# Number of times each patch had a catastrophe in the simulation
print(n_catastrophes)
# Simulated probabilities
print(n_catastrophes / 1000)
如果我的解释是正确的,您需要一个值向量,表示没有灾难性事件,或者表示灾难性事件的概率等于 d0?
您可以使用基于 d0 的加权概率对向量 c(1.0, 0.25) 进行采样,即第一个条目是 50/50,最后一个条目只有 5% 的灾难性损失...等等
对于每个循环,随机绘制你的猫向量:
cat <- unlist(lapply(d0, function(x) sample(c(1.0, 0.25), size=1, prob=c(1-x, x))))
并在 运行 Ricker 模型
之后将其粘贴在循环中
抱歉,这有点乱,但正在寻求一些建议。
我正在尝试模拟具有环境随机性的人口增长。我创建了一个概率向量,我使用的 10 个补丁大小中的任何一个都会经历灾难性事件,这将使人口规模减少 75%。我想在一个函数中应用它。所以基本上我需要有函数 运行 并确定下一步的人口规模,然后应用该补丁大小的概率来确定在新的人口规模存储之前灾难性事件是否会发生矩阵。
所以我基本上想做类似 If/Then 的事情,但我不想定义 "If" 参数,而是想应用存储的概率。我四处搜寻,但运气不佳,但我认为这不会那么困难。谢谢!
d0 <- c(0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05 ) # chance that a catastrophic
# disturbance will reduce population for each patch size, assuming that rates of disturbance
# are much higher in small patches.
cat <- .25 # disturbance factor, assume that during catastrophic event 2/3 of animals are
#removed, I want to multiply this by the population size within each time step at the frequency
#defined in d0. I could probably make this into a function but I still need to know
# how to use the probability to decide when to apply it.
# Ricker model (N_t+1=N_t*exp(r*(1-N_t/K)))
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
nt1 = ((nt1*cat)) ### Here I would apply the probability, and when necessary the
# disturbance factor. I.E. Breeding season happens then there is a very harsh winter
# and many individuals die.
return(nt1) #return the value of (Nt+1)
}
for(t in 1:(tf-1)) { #loop through time
n[t+1,] = Ricker(n[t,],r,k0) #step through Ricker
}
我最终做了一些类似于@Marius 建议的事情,它似乎工作得很好,感谢大家的投入!
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
for(d in 1:(length(d0))) { # Create loop to test each patch for disturbance probability
dice_rolls = runif(length(d0)) # Generate random uniforms for each element in d0.
nt1 = ifelse(dice_rolls < d0, nt1 * cat, nt1) # If the'dice roll' is less than the corresponding element of d0 # the patch experiences the disturbance
}
return(nt1) #return the value of (Nt+1)
}
如果我对你的模型的理解正确,你想要:
Ricker = function(nt, r, k0, d0) { #setup the Ricker function
nt1 = (nt*exp(r*(1-nt/k0))) # Run Ricker model
# Generate random uniforms for each element in d0. If the
# 'dice roll' is less than the corresponding element of d0,
# the patch experiences the disturbance
dice_rolls = runif(length(d0))
nt1 = ifelse(dice_rolls < d0, nt1 * cat, nt1)
return(nt1) #return the value of (Nt+1)
}
要了解这是如何工作的,请查看 dice_rolls < d0
如何运行的简单模拟:您可以看到它分别处理 d0
的每个元素,并且
生成近似所需概率的长期 运行 平均值:
d0 <- c(0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05)
n_catastrophes = numeric(length = length(d0))
for (sim_num in 1:1000) {
dice_rolls = runif(length(d0))
n_catastrophes = n_catastrophes + (dice_rolls < d0)
}
# Number of times each patch had a catastrophe in the simulation
print(n_catastrophes)
# Simulated probabilities
print(n_catastrophes / 1000)
如果我的解释是正确的,您需要一个值向量,表示没有灾难性事件,或者表示灾难性事件的概率等于 d0?
您可以使用基于 d0 的加权概率对向量 c(1.0, 0.25) 进行采样,即第一个条目是 50/50,最后一个条目只有 5% 的灾难性损失...等等
对于每个循环,随机绘制你的猫向量:
cat <- unlist(lapply(d0, function(x) sample(c(1.0, 0.25), size=1, prob=c(1-x, x))))
并在 运行 Ricker 模型
之后将其粘贴在循环中