Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中存储循环迭代的结果_R_Loops_Iteration_Storing Information - Fatal编程技术网

在R中存储循环迭代的结果

在R中存储循环迭代的结果,r,loops,iteration,storing-information,R,Loops,Iteration,Storing Information,我试图存储下面代码的结果,但我只能想出一个解决方案,用最小的残差平方和保存模型的结果。这是有用的,直到结果在c和gamma的范围内,因此我需要评估其他点的特征。为此,我需要存储每次迭代的结果。有人知道在这种情况下怎么做吗 提前谢谢 dlpib1 <- info$dlpib1 scale <- sqrt(var(dlpib1)) RSS.m <- 10 for (c in seq(-0.03,0.05,0.001)){ for (gamma in seq(1,100,0.2

我试图存储下面代码的结果,但我只能想出一个解决方案,用最小的残差平方和保存模型的结果。这是有用的,直到结果在c和gamma的范围内,因此我需要评估其他点的特征。为此,我需要存储每次迭代的结果。有人知道在这种情况下怎么做吗

提前谢谢

dlpib1 <- info$dlpib1
scale <- sqrt(var(dlpib1))
RSS.m <- 10

for (c in seq(-0.03,0.05,0.001)){
  for (gamma in seq(1,100,0.2))
    {
    trans <- (1+exp(-(gamma/scale)*(dlpib1-c)))^-1
    grid.regre <-lm(dlpib ~ dlpib1 + dlpib8 + trans + trans*dlpib1 + 
                  + I(trans*dlpib4) ,data=info) 
coef <- grid.regre$coefficients
RSS <- sum(grid.regre$residuals^2)

if (RSS < RSS.m){
  RSS.m <- RSS
  gamma.m <- gamma
  c.m <- c
  coef.m <- coef
  }
 }
}
grid <- c(RSS=RSS.m,gamma=gamma.m,c=c.m,coef.m)
grid`

dlpib1通过迭代存储模型结果的最简单方法是在
列表中

List = list()
for(i in 1:100)
    {
       LM = lm(rnorm(10)~rnorm(10))
       List[[length(List)+1]] = LM
     }

您可能完全可以避免
for
循环。然而,对于如何完成任务,您只需要为存储值的对象编制索引。比如说,

# outside the for loop
trans <- list()

# inside the for loop
trans[[paste(gamma, c, sep="_")]] <- ... 
#for循环外部

trans我很确定要保存RSS的所有迭代,您可以这样做:

dlpib1 <- info$dlpib1
    scale <- sqrt(var(dlpib1))
    RSS.m <- rep(0,N)
    coef <- rep(0,N)
    i <- 0

    for (c in seq(-0.03,0.05,0.001)){
      for (gamma in seq(1,100,0.2))
        {
        trans <- (1+exp(-(gamma/scale)*(dlpib1-c)))^-1
        grid.regre <-lm(dlpib ~ dlpib1 + dlpib8 + trans + trans*dlpib1 + 
                      + I(trans*dlpib4) ,data=info) 
    coef <- grid.regre$coefficients
    RSS.m[i] <- sum(grid.regre$residuals^2)
    i=i+1


      }
     }
    }

dlpib1作为一般提示,避免在
R
中使用
c
作为变量名,因为它也是使用率最高的函数之一,
c()
谢谢大家。我选择了Roxy.32的解决方案。这是最后的代码你好,里卡多。很抱歉在这么长时间后问这个问题,但我正在编写一个使用此函数的包,当我包含更多参数(如c)时,它需要很长时间才能完成循环。我注意到你说我可以避免结果循环。你能告诉我你认为这是怎么做到的吗?谢谢!嗨,哈维尔,最好为这个问题提出一个新问题,并将银行与这个问题联系起来