| simulate.merMod {lme4} | R Documentation |
merMod objectSimulate responses from the model represented by a "merMod" fitted
model object.
## S3 method for class 'merMod'
simulate(object, nsim = 1, seed = NULL,
use.u = FALSE, re.form=NA, ReForm, REForm, REform,
newdata=NULL, newparams=NULL,
family=NULL,
allow.new.levels=FALSE, na.action=na.pass, ...)
## S3 method for class 'formula'
simulate(object, nsim = 1 , seed = NULL, family, weights=NULL, offset=NULL, ...)
.simulateFun(object, nsim = 1, seed = NULL, use.u = FALSE,
re.form=NA, ReForm, REForm, REform,
newdata=NULL, newparams=NULL,
formula=NULL,family=NULL,
weights=NULL, offset=NULL,
allow.new.levels=FALSE, na.action=na.pass, ...)
object |
(for |
formula |
a (one-sided) mixed model
formula, as described for |
nsim |
positive integer scalar - the number of responses to simulate |
seed |
an optional seed to be used in
|
use.u |
(logical) if |
re.form |
formula for random effects to condition on. If |
ReForm |
allowed for backward compatibility: |
REForm |
allowed for backward compatibility: |
REform |
allowed for backward compatibility: |
newdata |
data frame for which to evaluate predictions |
newparams |
new parameters to use in evaluating predictions,
specified as in the |
family |
a GLM family, as in |
weights |
|
offset |
offset, as in |
allow.new.levels |
(logical) if FALSE (default),
then any new levels (or NA values) detected in
|
na.action |
what to do with |
... |
optional additional arguments: none are used at present |
ordinarily simulate is used to generate new
values from an existing, fitted model (merMod object):
however, if formula, newdata, and newparams are
specified, simulate generates the appropriate model
structure to simulate from
bootMer for “simulestimate”, i.e., where each
simulation is followed by refitting the model.
## test whether fitted models are consistent with the
## observed number of zeros in CBPP data set:
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
gg <- simulate(gm1,1000)
zeros <- sapply(gg,function(x) sum(x[,"incidence"]==0))
plot(table(zeros))
abline(v=sum(cbpp$incidence==0),col=2)
##
## simulate from a non-fitted model; in this case we are just
## replicating the previous model, but
params <- list(theta=0.5,beta=c(2,-1,-2,-3))
simdat <- with(cbpp,expand.grid(herd=levels(herd),period=factor(1:4)))
simdat$size <- 15
simdat$incidence <- sample(0:1,size=nrow(simdat),replace=TRUE)
form <- formula(gm1)[-2]
simulate(form,newdata=simdat,family=binomial,
newparams=params)