R使用循环函数编辑列表 - 错误

时间:2017-12-24 16:10:51

标签: r loops

我创建此列表代表Michaelis Menten酶动力学模型

MM
MM= list()
MM$M = c(x1 = 301,x2 = 120,x3 = 100,x4 = 0)
MM$Pre = matrix(c(1,1,0,0,0,0,1,0,0,0,1,0), nrow=3, ncol=4, byrow = TRUE)
MM$Post = matrix(c(0,0,1,0,1,1,0,0,0,1,0,1), ncol=4, nrow=3, byrow=TRUE)
MM$h = function (x, t, th = c(0.00166, 1e-04, 0.1)) 
{
  with(as.list(c(x, th)), {
    return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
  })
}

使用下面的gillespie算法

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(k in 1:3){
  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)){
      if (k == 1) {
        MM$h = function (x, t, th = c(1.66e-3*(i/100), 1e-4, 0.1))
      } else if ( k==2) {
        MM$h = function (x, t, th = c(1.66e-3, 1e-4*(i/100), 0.1))
      } else
        MM$h = function (x, t, th = c(1.66e-3, 1e-4, 0.1*(i/100)))
      {
        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:

    "        MM$h = function (x, t, th = c(1.66e-3*(i/100), 1e-4, 0.1))
          }"

所以问题出在我的k循环中,我改变了我的列表的MM $ h部分。但我不知道为什么我会收到这些错误。任何提示都会很棒。

如果这个问题不够明确,我会尝试改写它,但我基本上都在问我为什么会收到这些错误

编辑部分:

cl = rainbow(13)
for(k in 1:3){
  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)){
      if (k == 1) {
        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))
          })
        }

      } else if ( k==2) {
        MM$h = function (x, t, th = c(1.66e-3, 1e-4*(i/100), 0.1))
        {
          with(as.list(c(x, th)), {
            return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
          })
        }
      } else
        MM$h = function (x, t, th = c(1.66e-3, 1e-4, 0.1*(i/100)))
      {
        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)
  }
}

0 个答案:

没有答案