在 R 中存储循环迭代的结果 [英] Storing results of loop iterations in R

查看:38
本文介绍了在 R 中存储循环迭代的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试存储下面代码的结果,但是我只能想出一个解决方案来保存具有最小残差平方和的模型的结果.这在结果处于 c 和 gamma 范围内之前很有用,因此我需要评估其他点的特征.为此,我需要存储每次迭代的结果.有谁知道在这种情况下该怎么做?

I am trying to store the results of the the code below, however I could only come up with a solution to save the results of the model with the smallest sum of squared residuals. This was useful until the results were in the limits of the range of both c and gamma, therefore I need to assess the characteristics of other points. For this I need to store the results of every iteration. Does anyone know how to do this in this case?

提前致谢!

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`

推荐答案

你可以完全避免 for 循环.然而,至于如何完成你的任务,你只需要索引你存储值的任何对象.例如,

You can probably avoid the for loop altogether. However, as for how to accomplish your task, you simply need to index whatever object you are storing the value in. For example,

# outside the for loop
trans <- list()

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

这篇关于在 R 中存储循环迭代的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆