Eve Chanatasig
Eve Chanatasig

Reputation: 397

how to write out multiple files in R?

I am a newbie R user. Now, I have a question related to write out multiple files with different names. Lets says that my data has the following structure:

IV_HAR_m1<-matrix(rnorm(1:100), ncol=30, nrow = 2000)
DV_HAR_m1<-matrix(rnorm(1:100), ncol=10, nrow = 2000)

I am trying to estimate multiple LASSO regressions. At the beginning, I was storing the iterations in one object called Dinamic_beta. This object was stored in only one file, and it saves the required information each time that my code iterate.

For doing this I was using stew which belongs to pomp package, but the total process takes 5 or 6 days and I am worried about a power outage or a fail in my computer.

Now, I want to save each environment (iterations) in a .Rnd file. I do not know how can I do that? but the code that I am using is the following:

library(glmnet)
library(Matrix)
library(pomp)


  space <-  7 #THE NUMBER OF FILES THAT I would WANT TO CREATE

  Dinamic_betas<-array(NA, c(10, 31, (nrow(IV_HAR_m1)-space)))
  dimnames(Dinamic_betas) <- list(NULL, NULL)

  set.seed(12345)  
  stew( #stew save the enviroment in a .Rnd file
file = "Dinamic_LASSO_RD",{ # The name required by stew for creating one file with all information
  for (i in 1:dim(Dinamic_betas)[3]) {
    tryCatch( #print messsages
      expr = {
        cv_dinamic <- cv.glmnet(IV_HAR_m1[i:(space+i-1),],
                                DV_HAR_m1[i:(space+i-1),], alpha = 1, family = "mgaussian", thresh=1e-08, maxit=10^9)
        LASSO_estimation_dinamic<- glmnet(IV_HAR_m1[i:(space+i-1),], DV_HAR_m1[i:(space+i-1),],
                                          alpha = 1, lambda = cv_dinamic$lambda.min, family = "mgaussian")
        coefs <- as.matrix(do.call(cbind, coef(LASSO_estimation_dinamic)))
        Dinamic_betas[,,i] <- t(coefs)
      },
      error = function(e){
        message("Caught an error!")
        print(e)
      },
      warning = function(w){
        message("Caught an warning!")
        print(w)
      },
      finally = {
        message("All done, quitting.")
      }
    )
    if (i%%400==0) {print(i)}
  }
}
  )

If someone can suggest another package that stores the outputs in different files I will grateful.

Upvotes: 1

Views: 126

Answers (1)

JustGettinStarted
JustGettinStarted

Reputation: 834

Try adding this just before the close of your loop

save.image(paste0("Results_iteration_",i,".RData"))

This should save your entire workspace to disk for every iteration. You can then use load() to load the workspace of every environment. Let me know if this works.

Upvotes: 1

Related Questions