根据向量

时间:2017-03-23 01:17:35

标签: r probability

对不起,这有点乱,但正在寻找一些建议。

我试图通过环境随机性来模拟人口增长。我已经创建了一个概率向量,我正在使用的10个补丁大小中的任何一个将经历一场灾难性事件,这将使人口规模减少75%。我想在一个函数中应用它。因此,基本上我需要运行函数并确定下一步的填充大小,然后应用该补丁大小的概率来确定在新的填充大小存储在矩阵中之前是否会发生灾难性事件。 / p>

所以我基本上想要做一些像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) 
} 

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的模型:

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)

答案 1 :(得分:0)

如果我正在解释这个权利,你想要一个值向量,表示没有灾难性事件或灾难性事件,概率等于d0?

您可以使用基于d0的加权概率对向量c(1.0,0.25)进行采样,即第一个条目为50/50,最后一个条目仅为5%灾难性损失......等等。

对于每个循环,随机绘制您的猫矢量:

sns.palplot(sns.color_palette("Set2", 8))

并在运行Ricker模型后将其粘贴在循环中