[英]R Using loop to create 4 separate graphs of matrices (using lines function)
我目前尚未對Micahelis-Menten酶動力學模型進行任何測試和分析。 我的代碼嘗試做的是將速率參數th1更改為最大值的200%的50%。
我想做的是生成4個獨立的圖,這些圖顯示更改速率參數時MM1,MM2,MM3,MM4會發生什么。
我遇到的問題是關於“ q”循環以及在代碼末尾附近使用“ if”,“ else if”和“ else”函數的地方。
MM <- list(Pre = matrix(c(1,0,0,1,0,0,0,1,1,0,0,0), ncol=4), Post =
matrix(c(0,1,0,0,1,1,1,0,0,0,0,1),ncol=4), M= c("x1"=301,"x2"=120, "x3"=0,
"x4"=0), h = function (x, t, th = c(1.66e-3, 1e-4 , 0.1))
{
with(as.list(c(x, th)), {
return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
})
})
gillespied1 <- function (N, T = 100, dt = 1, ...)
{
tt = 0
n = T%/%dt
x = N$M
S = t(N$Post - N$Pre)
u = nrow(S)
v = ncol(S)
xmat = matrix(ncol = u, nrow = n)
i = 1
target = 0
repeat {
h = N$h(x, tt, ...)
h0 = sum(h)
if (h0 < 1e-10)
tt = 1e+99
else if (h0>3000){
tt=1e+99
xmat[i] <- xmat[i-1] ###
i = i + 1
if(i > n)
return(ts(xmat, start = 0, deltat = dt)) ###
}
else tt = tt + rexp(1, h0)
while (tt >= target) {
xmat[i, ] = x
i = i + 1
target = target + dt
if (i > n)
return(ts(xmat, start = 0, deltat = dt))
}
j = sample(v, 1, prob = h)
x = x + S[, j]
}
}
cl = rainbow(13)
for(q in 1:4){
plot(1, type="n", xlab="Time", ylab="Concentration of Substrate",xaxt='n',
xlim=c(0, 1200), ylim=c(0, 310), main="Micahaelis-Menten:Changing Substrate rate parameter")
for(i in seq(from=50, to=200, by=25)){
MM$h = function (x, t, th = c(1.66e-3*(i/100), 1e-4, 0.1))
{
with(as.list(c(x, th)), {
return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
})
}
out = gillespied1(MM,T=300,dt=0.1)
MM1 <- out[,1]
MM2 = out[,2]
MM3 = out[,3]
MM4 = out[,4]
for (j in 1:40) {
out = gillespied1(MM, T=300, dt=0.1)
MM1 = cbind(MM1,out[,1])
MM2 = cbind(MM2,out[,2])
MM3 = cbind(MM3,out[,3])
MM4 = cbind(MM4,out[,4])
}
a =matrix(rowMeans(MM1))
b = matrix(rowMeans(MM2))
c = matrix(rowMeans(MM3))
d = matrix(rowMeans(MM4))
if (q = 1) {
lines(a, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else if ( q=2) {
lines(b, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else if ( q=3) {
lines(c, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else
lines(d, lwd="1.5", col =cl[2*((i/25)-1)-1])
}
axis(side = 1, at = (0:300)*10 , labels = 0:300)
legend("topright", legend=c("50%","75%","100%","125%","150%","175%", "200%"), lty =c(rep(1)), lwd=c(rep(1)), title ="% of original substrate rate parameter", col=cl[seq(1,13,2)], cex=0.4)
}
我不斷收到這個錯誤
Error: unexpected '}' in "}"
但我不知道為什么。
如果我的代碼運行正常,我應該得到4張圖,每個圖包含7行。
任何幫助都將是驚人的。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.