我有这个功能
gillespieLO = function (N, n, ...)
{
tt = 0
x = N$M
S = t(N$Post - N$Pre)
u = nrow(S)
v = ncol(S)
tcrit = 2
tvec = vector("numeric", n)
xmat = matrix(ncol = u, nrow = n + 1)
xmat[1, ] = x
for (i in 1:n) {
h = N$h(x, tt, ...)
tt = tt + rexp(1, sum(h))
j = sample(v, 1, prob = h)
x = x + S[, j]
tvec[i] = tt
xmat[i + 1, ] = x
poo = min(which(tvec>tcrit))
if (i == poo){
x[4] = x[4]+(10000/10)
}
}
print(min(which(tvec>tcrit)))
return(list(t = tvec, x = xmat))
}
LO = list()
LO$M = c(i = 1, ri = 0, I = 50, Lac = 20, ILac = 0, o = 1, Io = 0, RNAP = 100, RNAPo = 0, r = 0, z = 0)
LO$Pre = matrix(c(1,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,0,
0,0,1,1,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,1,0,1,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,0,1,0,
0,0,0,1,0,0,0,0,0,0,1,
0,1,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,1), ncol=11, byrow=TRUE)
LO$Post = matrix(c(1,1,0,0,0,0,0,0,0,0,0,
0,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,
0,0,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,
0,0,1,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,1,0,1,0,0,0,
0,0,0,0,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0), ncol=11, byrow=TRUE)
LO$h = function(x,t,th=c(0.02,0.1,0.005,0.1,1,0.01,0.1,0.01,0.03,0.1,1e-05,0.01,0.002,0.01,0.001))
{
with(as.list(c(x, th)), {
return(c(th[1]*i, th[2]*ri, th[3]*I*Lac, th[4]*ILac, th[5]*I*o, th[6]*Io, th[7]*o*RNAP,
th[8]*RNAPo, th[9]*RNAPo, th[10]*r, th[11]*Lac*z, th[12]*ri, th[13]*I,
th[13]*ILac, th[14]*r, th[15]*z))
})
}
把
gillespieLO(LO,15)
给出
gillespieLO(LO,15)
[1] 11
$t
[1] 0.001399092 0.053674441 0.132884069 0.156973856 0.365723847 0.700767071 1.178798507
[8] 1.529528682 1.663988976 1.719413303 2.352115017 2.353169914 2.355999647 2.364615018
[15] 2.367255303
$x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 1 0 50 20 0 1 0 100 0 0 0
[2,] 1 0 49 20 0 0 1 100 0 0 0
[3,] 1 0 48 19 1 0 1 100 0 0 0
[4,] 1 0 47 18 2 0 1 100 0 0 0
[5,] 1 0 46 17 3 0 1 100 0 0 0
[6,] 1 0 47 18 2 0 1 100 0 0 0
[7,] 1 0 46 17 3 0 1 100 0 0 0
[8,] 1 0 45 16 4 0 1 100 0 0 0
[9,] 1 0 44 15 5 0 1 100 0 0 0
[10,] 1 0 43 14 6 0 1 100 0 0 0
[11,] 1 0 42 13 7 0 1 100 0 0 0
[12,] 1 0 41 12 8 0 1 100 0 0 0
[13,] 1 0 40 1011 9 0 1 100 0 0 0
[14,] 1 0 39 1010 10 0 1 100 0 0 0
[15,] 1 0 38 1009 11 0 1 100 0 0 0
[16,] 1 0 37 1008 12 0 1 100 0 0 0
但我得到错误,
1: In min(which(tvec > tcrit)) :
no non-missing arguments to min; returning Inf
And I want to adapt it so that I can find the minimum value of `i` in my loop such that `tt>tcrit`
所以我想做像min这样的事情(我这样(tt> tcrit))但我不知道如何在R中写这个。
很抱歉没有多大帮助,因为我一直在努力寻找这个,但我不知道如何正确解释。
答案 0 :(得分:0)
循环之前:
firstIalreadyFound <- FALSE
然后,将此行作为循环中的第一行:
if (tt > tcrit & !firstIalreadyFound){
firstIalreadyFound <- TRUE
# here, add the code you want to execute for this first i
}
只要满足条件,它就会打印出来。
答案 1 :(得分:0)
如果没有传递有效(非NA)值,min
函数将返回Inf
。
tcrit
的值为2
。值将在行tvec
的循环中分配给tvec[i] = tt
。假设在第一次迭代中,分配给tvec[i]
的值小于1(小于tcrit
)。在该迭代中,which(tvec>tcrit)
不会返回任何有效值,因为没有值大于tcrit
。 min
返回Inf
,因为没有有效值传递给它。您可以通过简单的测试来尝试这种情况:
a <- c(1,2,2,3,4)
> min(which(a>5))
[1] Inf
因此,为了避免警告,最好检查any
中是否存在tvec
匹配条件,然后仅申请min
,否则min
无效。
修改后的代码:
if(any(which(tvec>tcrit))){
poo = min(which(tvec>tcrit))
if (i == poo){
x[4] = x[4]+(10000/10)
}
print(min(which(tvec>tcrit)))
}