Storing results of loop iterations in R
Asked Answered
F

3

7

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?

Thanks in advance!

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`
Fuqua answered 27/9, 2013 at 18:25 Comment(1)
as a general tip, avoid using c as a variable name in R since it is also the name of one of the most highly used functions, c( )Popeyed
P
4

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="_")]] <- ... 
Popeyed answered 27/9, 2013 at 18:40 Comment(2)
Hello Ricardo. Sorry to ask this after so long, but I'm writing a package that uses this function, and it takes to long for it to complete the loop when I include more parameters like c. I noticed that you said I could avoid looping for the result. Can you tell me how you think this can be done? Thanks a lot!Fuqua
hi Javier, it's best to open a new question for this and link bank to this onePopeyed
P
12

The easiest way to store model results by iterations is in a list:

List = list()
for(i in 1:100)
    {
       LM = lm(rnorm(10)~rnorm(10))
       List[[length(List)+1]] = LM
     }
Pinole answered 27/9, 2013 at 18:39 Comment(1)
Thank you all. I went with Roxxy.32's solution. Here is the final codeFuqua
P
4

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="_")]] <- ... 
Popeyed answered 27/9, 2013 at 18:40 Comment(2)
Hello Ricardo. Sorry to ask this after so long, but I'm writing a package that uses this function, and it takes to long for it to complete the loop when I include more parameters like c. I noticed that you said I could avoid looping for the result. Can you tell me how you think this can be done? Thanks a lot!Fuqua
hi Javier, it's best to open a new question for this and link bank to this onePopeyed
D
1

I'm pretty sure to save all iterations of the RSS's you could do something like this:

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


      }
     }
    }
Dombrowski answered 27/9, 2013 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.