Write results to a file inside foreach loop R -
i using foreach package %dopar% implement set of simulations. structure of simulation illustrated simple version:
library(foreach) library(domc) registerdomc(4) id.list <- c(1:3000) results <- foreach(i=1:1000,.combine=rbind) %dopar% { ## randomly draw 100 donor pool units , 1 treated unit v1 <- sample(id.list, 1, replace = false) v2 <- sample(id.list, 1, replace = false) v3 <- sample(id.list, 1, replace = false) v4 <- sample(id.list, 1, replace = false) c(i,v1,v2,v3,v4) }
i write results loop .csv file or similar while loop still running. instance, when loop hits iteration 500, i'd write first 500 rows .csv.
alternatively, open breaking loop when hits iteration 500, while extracting first 500 rows of results.
is possible within foreach?
say, instead of 1000 trys, going 10000, break this, using part_num differentiate blocks of size 500
library(foreach) library(domc) registerdomc(4) id.list <- c(1:3000) for(part_num in 1:20){ results <- foreach(i=1:500,.combine=rbind) %dopar% { ## randomly draw 100 donor pool units , 1 treated unit v1 <- sample(id.list, 1, replace = false) v2 <- sample(id.list, 1, replace = false) v3 <- sample(id.list, 1, replace = false) v4 <- sample(id.list, 1, replace = false) c(i,v1,v2,v3,v4) } write.csv(results, file = paste('part', part_num, '.csv', sep = '')) }
Comments
Post a Comment