Examples for 'stats::simulate'


Simulate Responses

Aliases: simulate

Keywords: models datagen

### ** Examples

x <- 1:5
mod1 <- lm(c(1:3, 7, 6) ~ x)
S1 <- simulate(mod1, nsim = 4)
## repeat the simulation:
.Random.seed <- attr(S1, "seed")
identical(S1, simulate(mod1, nsim = 4))
[1] FALSE
S2 <- simulate(mod1, nsim = 200, seed = 101)
rowMeans(S2) # should be about the same as
        1         2         3         4         5 
0.6885691 2.2329771 3.7790352 5.2775859 6.8131451 
fitted(mod1)
  1   2   3   4   5 
0.8 2.3 3.8 5.3 6.8 
## repeat identically:
(sseed <- attr(S2, "seed")) # seed; RNGkind as attribute
[1] 101
attr(,"kind")
attr(,"kind")[[1]]
[1] "Mersenne-Twister"

attr(,"kind")[[2]]
[1] "Inversion"

attr(,"kind")[[3]]
[1] "Rejection"
stopifnot(identical(S2, simulate(mod1, nsim = 200, seed = sseed)))

## To be sure about the proper RNGkind, e.g., after
RNGversion("2.7.0")
Warning in RNGkind("Mersenne-Twister", "Inversion", "Rounding"): non-uniform
'Rounding' sampler used
## first set the RNG kind, then simulate
do.call(RNGkind, attr(sseed, "kind"))
identical(S2, simulate(mod1, nsim = 200, seed = sseed))
[1] TRUE
## Binomial GLM examples
yb1 <- matrix(c(4, 4, 5, 7, 8, 6, 6, 5, 3, 2), ncol = 2)
modb1 <- glm(yb1 ~ x, family = binomial)
S3 <- simulate(modb1, nsim = 4)
# each column of S3 is a two-column matrix.

x2 <- sort(runif(100))
yb2 <- rbinom(100, prob = plogis(2*(x2-1)), size = 1)
yb2 <- factor(1 + yb2, labels = c("failure", "success"))
modb2 <- glm(yb2 ~ x2, family = binomial)
S4 <- simulate(modb2, nsim = 4)
# each column of S4 is a factor

[Package stats version 4.2.3 Index]