# Here is the R script template used to create data and save the results # The 'subSampleSize', 'subVariance', and 'subNum' are terms # that will be substituted later. script <- ' set.seed(1234) nrep <- 1000 n1 <- subSampleSize n2 <- 120 - n1 v1 <- 1 v2 <- subVariance result <- rep(NA, nrep) for(i in 1:nrep) { y1 <- rnorm(n1, 0, sqrt(v1)) y2 <- rnorm(n2, 0, sqrt(v2)) y <- c(y1, y2) g <- c(rep(1, n1), rep(2, n2)) dat <- data.frame(g = g, y = y) out <- t.test(y ~ g, data = dat, var.equal = TRUE) result[i] <- out[["p.value"]] } write.csv(result, file="outputsubNum.csv") ' # Create combination of the sample-size condition and the variance # condition. The 'expand.grid' function is used to create all # possible combination of all conditions. n1 <- c(20, 40, 60, 80, 100) v2 <- c(1, 3, 10) condition <- expand.grid(n1, v2) # The R script template will be substituted by the condition values # from each combination. Then, the substituted template will be # written in sim*.R for(i in 1:nrow(condition)) { temp <- gsub("subSampleSize", condition[i, 1], script) temp <- gsub("subVariance", condition[i, 2], temp) temp <- gsub("subNum", i, temp) write(temp, paste("sim", i, ".R", sep="")) } # Create folders called n100 and n200 by R n <- c(100, 200) for(i in 1:length(n)) { temp1 <- paste("n", n[i], sep = "") dir.create(temp1) } # Parallel processing library(parallel) set.seed(123321) nrep <- 1000 FUN <- function(temp) { n <- 200 b <- 0.5 x <- rnorm(n, 0, 1) e <- rnorm(n, 0, sqrt(1 - b^2)) y <- b*x + e dat <- data.frame(x = x, y = y) out <- lm(y ~ x, data = dat) summary(out)[["coefficients"]][2, c(1, 2, 4)] } result <- mclapply(1:nrep, FUN, mc.cores = 11) result <- do.call(rbind, result) save(result, file=paste("result.rda", sep="")) # Parallel processing with L'Ecuyer seed set.seed(123321) nrep <- 1000 library(parallel) RNGkind("L'Ecuyer-CMRG") # Use L'Ecuyer method seed.l <- list() # Create list of seed number to provide for each rep s <- .Random.seed # Extract the current seed number for (i in 1:nrep) { seed.l[[i]] <- s # Save seed number to the list s <- nextRNGStream(s) # Build independent seed number } FUN <- function(repseed) { RNGkind("L'Ecuyer-CMRG") # Assign the seed for each rep inside each node assign(".Random.seed", repseed, envir = .GlobalEnv) n <- 200 b <- 0.5 x <- rnorm(n, 0, 1) e <- rnorm(n, 0, sqrt(1 - b^2)) y <- b*x + e dat <- data.frame(x = x, y = y) out <- lm(y ~ x, data = dat) summary(out)[["coefficients"]][2, c(1, 2, 4)] } # Use mclapply to distribute seed number for each rep result <- mclapply(seed.l, FUN, mc.cores = 11) result <- do.call(rbind, result) save(result, file=paste("result2.rda", sep=""))