Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/initPortableStreams.R
The main argument is the collection of seeds that is in the proper format. It should be a "row" of a "portableSeeds" array object. The currentStream argument determines which of these is set into the R environment variable .Random.seed.
1 | setSeedVector(runSeeds, currentStream = 1L, verbose = FALSE)
|
runSeeds |
Required. Seeds for the L'Ecuyer-CMRG random generator |
currentStream |
Optional. Integer indicating which of the streams is to be used first. Default = 1. |
verbose |
Optional. Default = FALSE. |
This is different from setSeeds
because setSeets accepts
the whole parallelSeeds object as input, whereas this function
wants only the input for one run of a simulation.
In some specific use cases, particuarly the replication of individual runs, it may be easier to use this function rather than setSeeds, but both achieve the same purpose.
nothing is returned. This function is used for the side effect of setting three objects in the environment, the startStates (list), currentStates (list), and currentStream (an integer).
Paul E. Johnson <pauljohn@ku.edu>
seedCreator
, setSeeds
, and useStream
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | library(portableParallelSeeds)
projSeeds <- seedCreator(50, 3, seed = 123456)
A1 <- projSeeds[[27]]
A1 ## shows states of 3 generators for one run
##' Receive a run-specific seed vector and conduct a simulation
##'
##' Rather than giving this function the whole seed warehouse that is used for
##' all the simulation runs, this one just receives one vector of seeds. Compare
##' to \code{\link{setSeeds}}.
##'
##' This function does not need to know what the run number is, since it accepts
##' a stream vector as its first argument. Prehaps this is a simpler, more
##' elegant approach than in \code\link{{setSeeds}}.
##' @param streams A stream element selected from a "portableSeeds" object.
##' Created by \code\link{{seedCreator}}
##' @param N Number of rows in simulated data set
##' @param m mean of simulated data
##' @param sd standard deviation of simulated data
##' @param beta regression coefficients
##' @return A list of estimated regressions fitted to the simulated values,
##' including 3 linear models and one generalized linear model
##' @author Paul Johnson <pauljohn@@ku.edu>
runOneSimulation <- function(streams, N, m, sd, beta = c(0.3, 0.2, 0.1))
{
setSeedVector(streams)
datX <- rockchalk::mvrnorm(N,
mu = rep(m, 3),
Sigma = sd * diag(3))
colnames(datX) <- c("X1", "X2", "X3")
useStream(2)
eta <- datX %*% beta
SigmaE <- sqrt(mean(eta))
Y1 <- eta + SigmaE * rnorm(N)
datX <- data.frame(datX, Y1)
lm1 <- lm(Y1 ~ X1 + X2 + X3, data = datX)
## lets see a log link, just for fun
datX$Y2 <- rpois(N, lambda = exp(eta))
lm2 <- lm(Y2 ~ X1 + X2 + X3, data = datX)
lm3 <- lm(log(Y2+0.5) ~ X1 + X2 + X3, data = datX)
glm3 <- glm(Y2 ~ X1 + X2 + X3, data = datX, family = poisson(link = "log"))
list("lm1" = lm1, "lm2" = lm2, "lm3" = lm3, "glm3" = glm3)
}
serial1 <- lapply(projSeeds[1:30], runOneSimulation, N = 1000, m = 2,
sd = 10, beta = c(0.5, 0.6, 0.1))
## Take the 27th simulated model
lapply(serial1[[27]], summary)
## Recall A1, the 27th seed vector, was created above.
## Repeat that run, explicitly:
serial1.27 <- runOneSimulation(A1, N = 1000, m = 2, sd = 10, beta =
c(0.5, 0.6, 0.1))
## Note they are same
sapply(serial1[[27]], coef)
sapply(serial1.27, coef)
## Check Again
identical(coef(serial1.27[[1]]), coef(serial1[[27]][[1]]))
identical(coef(serial1.27[[2]]), coef(serial1[[27]][[2]]))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.