元の質問を間違って行ったので、こちらがより良いバージョンです。
R を使用してモデルを生成したいと思います。モデルの要点 --> ポリマーはさまざまな速度で成長または収縮できます。時折、成長率が一定に保たれているにもかかわらず、縮小率が 20 倍に増加します。これは「壊滅的な状態」と見なされます。状態は、特定の割合で「壊滅的な状態」との間で切り替わります。問題は、ポリマーの長さが時間に対してどのように変化するかということです。これが私が考えていることです:
初期化:
長さ 0 の 5 つのポリマー (列インデックスで示されます)
rnumbers <- data.frame(replicate(5,runif(20000, 0, 1)))
rstate <- rnumbers # copy the structure
rstate[] <- NA # preserve structure with NA's
# Init:
rstate[1, ] <- c(0)
> head(rstate)
X1 X2 X3 X4 X5
1 0 0 0 0 0
2 NA NA NA NA NA
3 NA NA NA NA NA
4 NA NA NA NA NA
5 NA NA NA NA NA
6 NA NA NA NA NA
シミュレーションを 200 秒間実行したい
レートの設定:
dt <- c(.01) # how time is being divided
A <- dt*1 # probability growth will occur under normal conditions
B <- dt*.25 # probability shrinking will occur under normal conditions
C <- dt*.03 # probability "catastrophic state" will occur
D <- dt*.3 # probability normal state will occur once in "catastrophic state"
E <- dt*5 # probability shrinking will occur "catastrophic state"
通常の状態では成長の確率が収縮の確率を上回っていますが、「壊滅的な状態」では収縮が優勢です。また、データ フレームの 20000 行の dt = .01 は、最大 200 になります
壊滅的な状態への切り替えを考慮しないと、コードは次のようになります。
library("Rcell")
rnumbers <- data.frame(replicate(5,runif(20000, 0, 1)))
dt <- c(.01)
A <- dt*1
B <- dt*.25
C <- dt*.03
D <- dt*.3
E <- dt*5
rstate <- rnumbers # copy the structure
rstate[] <- NA # preserve structure with NA's
# Init:
rstate[1, ] <- c(0)
step_generator <- function(col, rnum){
for (i in 2:length(col) ){
if( rnum[i] < B) {
col[i] <- -1
}
else {
if (rnum[i] < A) {
col[i] <- 1
}
else {
col[i] <- 0
}
}
}
return(col)
}
# Run for each column index:
for(cl in 1:5){ rstate[ , cl] <-
step_generator(rstate[,cl], rnumbers[,cl]) }
rstate1 <- transform(rstate, time = rep(dt))
rstate2 <- transform(rstate1, cumtime = cumsum(time))
cum_sum <- apply(rstate2, 2, cumsum)
cum_sum <- as.data.frame(cum_sum)
dev.new(width=5, height=5)
cplot(cum_sum, X2 ~ time, geom = "line")
このコードを実行すると、正の勾配を持つギザギザの線が 200 単位の時間にわたってプロットされます。私が使用したプロットを使用するには、パッケージ「Rcell」が必要です。
壊滅的な状態を取り入れようとすると、私の困難が生じます。このコードに壊滅的な状態を組み込むにはどうすればよいですか? 私はこのようなものを想像しますが、構文をどのように翻訳するかわかりません:
step_generator <- function(col, rnum)
for (i in 2:length(col)
start in normal growth conditions (growth prob = A ; shrinkage prob = B)
if rnum[i] < C switch to catastrophic state (
growth prob = A ;
shrinkage prob = E),
else stay in normal growth conditions (i.e. if rnum[i] >= C)
stay in catastrophic state until rnum[i] < D, when this happens switch back to normal growth conditions.
repeat through the entire 20000 rows
すべての助けに感謝します!