在R

时间:2017-06-23 22:11:26

标签: r

我是新来的,总的来说是编程 - 希望得到一些帮助。 我有以下代码用于回溯扩展卡尔曼滤波器,它给出了特定参数的MSE。问题是当我运行代码时,最后,矩阵只存储最后一组值而不是所有值。 如果您需要在PC上运行代码,只需将文件名替换为您手头的任何数据集。它应该仍然有效。

start.time <- Sys.time()

library(invgamma)
w = read.csv("Reddy.csv")
q = ts(w[2])
num = length(q)

f = function(x){
  f1 = sqrt(x)
  return(f1)
}
h = function(x){
  h1 = x**3
  return(h1)
}


ae1 = seq(24,26)
ae2 = seq(24,26)

be1 = seq(1,3)
be2 = seq(1,3)

a = seq(1,3)
b = seq(1,3)

MSE = matrix(nrow = length(ae1)*length(ae2)*length(be1)*length(be2)*length(a)*length(b), ncol =7)

for (i in ae1){
  for (j in ae2){
    for (k in be1){
      for (l in be2){
        for (m in a){
          for (n in b){
            d = rep(0,num)
            for(o in 2:num){
              xt = rep(0,num)
              yt = rep(0,num)
              fx = rep(0,num)
              hx = rep(0,num)

              e = rinvgamma(num,i,k)
              g = rinvgamma(num,j,l)
              fx[o] = f(xt[o-1])
              xt[o] = m*fx[o] + e[o-1]
              hx[o] = h(xt[o])
              yt[o]= n*hx[o] +g[o]
              d[o] = (yt[o] - q[o])**2
            }
            MSE[,1] = mean(d)
            MSE[,2] = i
            MSE[,3] = j
            MSE[,4] = k
            MSE[,5] = l
            MSE[,6] = m
            MSE[,7] = n
            t = rbind(mean(d),i,j,k,l,m,n)
            print(t)
          }
        }
      }
    }
  }
}

end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

m = which.min(MSE[1])

理想情况下,我的矩阵将第一行作为MSE,第2列到第7列将分别具有相应的i,j,k,l,m,n值,并且每次迭代将被记录到新的行条目中。在这里,它似乎每次都重写整个矩阵。

1 个答案:

答案 0 :(得分:0)

使用时

  

MSE [,2] = i

您实际上调用了整个列,因此代码正在重写该列。 我已经使用了一个可以提供帮助的计数器更新了代码。

start.time <- Sys.time()

library(invgamma)
w = read.csv("Reddy.csv")
q = ts(w[2])
num = length(q)

f = function(x){
  f1 = sqrt(x)
  return(f1)
}
h = function(x){
  h1 = x**3
  return(h1)
}


ae1 = seq(24,26)
ae2 = seq(24,26)

be1 = seq(1,3)
be2 = seq(1,3)

a = seq(1,3)
b = seq(1,3)

count = 0

MSE = matrix(nrow = length(ae1)*length(ae2)*length(be1)*length(be2)*length(a)*length(b), ncol =7)

for (i in ae1){
  for (j in ae2){
    for (k in be1){
      for (l in be2){
        for (m in a){
          for (n in b){
            d = rep(0,num)
            for(o in 2:num){
              xt = rep(0,num)
              yt = rep(0,num)
              fx = rep(0,num)
              hx = rep(0,num)

              e = rinvgamma(num,i,k)
              g = rinvgamma(num,j,l)
              fx[o] = f(xt[o-1])
              xt[o] = m*fx[o] + e[o-1]
              hx[o] = h(xt[o])
              yt[o]= n*hx[o] +g[o]
              d[o] = (yt[o] - q[o])**2
            }
            count <- count + 1
            MSE[count,1] = mean(d)
            MSE[count,2] = i
            MSE[count,3] = j
            MSE[count,4] = k
            MSE[count,5] = l
            MSE[count,6] = m
            MSE[count,7] = n
            t = rbind(mean(d),i,j,k,l,m,n)
            print(t)
          }
        }
      }
    }
  }
}

end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

m = which.min(MSE[1])