私は R の学習を始めたばかりの数学の大学院生です。
確率論的進化の振る舞いを (マルコフ過程として) モデル化する光沢のあるアプリを構築しています。次の場所で (ショーケース モードで) 見ることができます。
問題:アプリのシミュレーション部分で、私が理解していない 2 つのエラーがスローされます。それらは次のとおりです。
人口サイズ N が小さい (N<10)場合、多くの場合 (常にではありません) スローします。
Error: in sample.int(length(x), size, replace, prob) :
incorrect number of probabilities
人口サイズ N が大きい場合 (N>100)、頻繁に (常にではありませんが) スローします
Error: in sample.int(length(x), size, replace, prob) :
non-positive probability
このエラーを再現するには、母集団スライダーを最大 (200) または最小 (0) に設定し、[Simulate Single Population] (左側のサイドバーにある) をエラーが発生するまで繰り返しクリックします。エラーが発生する前に、何度もクリックする必要がある場合があります。
この問題は、コードの次の部分に起因する可能性があります。
MPM.sim <- function(t, MPM, πNought) {
# The simulation function takes as arguments: duration t, the MPM matrix of transition probabilities, and some initial condition πNought
sim <- as.numeric(t)
if (missing(πNought)) { # If the initial state is not specified
sim[1] <- (sample(1:(N+1),1) - 1) # Then take a random initial state
}else{sim[1]<-πNought} # If the initial state is specified, start from that state.
for(i in 2:t){ # For each step of the simulation after the initial state,
newstate <- sample(1:(N+1),1,prob=MPM[sim[i-1],]) # The transition to the next state is given by the transition matrix MPM.
sim[i] <- newstate
}
sim
}
これを修正する方法についての助けや提案をいただければ幸いです。