谁能向我解释为什么此代码的分布:
InfectionHistory <- rep(1,100)
for(x in 1:99)
{
r <- runif(1)
print(r)
if(InfectionHistory[x]==1)
{
if(r < 0.04)
{
InfectionHistory[x+1] <- 2
}
else
{
InfectionHistory[x+1] <- InfectionHistory[x]
}
}
if(InfectionHistory[x]==2)
{
if(r < 0.11)
{
InfectionHistory[x+1] <- 1
}
else
{
InfectionHistory[x+1] <- InfectionHistory[x]
}
}
}
plot(InfectionHistory, xlab = "Day", ylab = "State", main = "Patient Status per Day", type = "o")
与此代码不同:
InfectionHistory <- rep(1,100)
for(x in 1:99)
{
r <- runif(1)
print(r)
if((InfectionHistory[x]==1)&&(r < 0.04))
{
InfectionHistory[x+1] <- 2
}
if((InfectionHistory[x]==2)&&(r < 0.11))
{
InfectionHistory[x+1] <- 1
}
}
plot(InfectionHistory, xlab = "Day", ylab = "State", main = "Patient Status per Day", type = "o")
我觉得这与 if-else 语句的逻辑有关。代码的目标是模拟一个沿着马尔可夫链的感染模型