| simulate.merMod {lme4} | R Documentation | 
Simulate Responses From merMod Object
Description
Simulate responses from a "merMod" fitted model object, i.e.,
from the model represented by it.
Usage
## S3 method for class 'merMod'
simulate(object, nsim = 1, seed = NULL,
	 use.u = FALSE, re.form = NA,
	 newdata=NULL, newparams=NULL, family=NULL,
	 allow.new.levels = FALSE, na.action = na.pass, ...)
.simulateFun(object, nsim = 1, seed = NULL, use.u = FALSE,
             re.form = NA,
             newdata=NULL, newparams=NULL,
             formula=NULL, family=NULL, weights=NULL, offset=NULL,
             allow.new.levels = FALSE, na.action = na.pass,
             cond.sim = TRUE, ...)
Arguments
| object | (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
 | 
| newdata | data frame for which to evaluate predictions. | 
| newparams | new parameters to use in evaluating predictions,
specified as in the  | 
| formula | a (one-sided) mixed model formula, as described for
 | 
| family | a GLM family, as in  | 
| weights | |
| offset | offset, as in  | 
| allow.new.levels | (logical) if FALSE (default), then any new
levels (or  | 
| na.action | what to do with  | 
| cond.sim | (experimental) simulate the conditional
distribution?  if  | 
| ... | optional additional arguments (none are used in
 | 
Details
- ordinarily - simulateis used to generate new values from an existing, fitted model (- merModobject): however, if- formula,- newdata, and- newparamsare specified,- simulategenerates the appropriate model structure to simulate from.- formulamust be a one-sided formula (i.e. with an empty left-hand side); in general, if- fis a two-sided formula,- f[-2]can be used to drop the LHS.
- The - re.formargument allows the user to specify how the random effects are incorporated in the simulation. All of the random effects terms included in- re.formwill be conditioned on - that is, the conditional modes of those random effects will be included in the deterministic part of the simulation. (If new levels are used (and- allow.new.levelsis- TRUE), the conditional modes for these levels will be set to the population mode, i.e. values of zero will be used for the random effects.) Conversely, the random effect terms that are not included in- re.formwill be simulated from - that is, new values will be chosen for each group based on the estimated random-effects variances.- The default behaviour (using - re.form=NA) is to condition on none of the random effects, simulating new values for all of the random effects.
- For Gaussian fits, - sigmaspecifies the residual standard deviation; for Gamma fits, it specifies the shape parameter (the rate parameter for each observation i is calculated as shape/mean(i)). For negative binomial fits, the overdispersion parameter is specified via the family, e.g.- simulate(..., family=negative.binomial(theta=1.5)).
- For binomial models, - simulate.formulalooks for the binomial size first in the- weightsargument (if it's supplied), second from the left-hand side of the formula (if the formula has been specified in success/failure form), and defaults to 1 if neither of those have been supplied. Simulated responses will be given as proportions, unless the supplied formula has a matrix-valued left-hand side, in which case they will be given in matrix form. If a left-hand side is given, variables in that expression must be available in- newdata.
- For negative binomial models, use the - negative.binomialfamily (from the MASS package) and specify the overdispersion parameter via the- theta(sic) parameter of the family function, e.g.- simulate(...,family=negative.binomial(theta=1))to simulate from a geometric distribution (negative binomial with overdispersion parameter 1).
See Also
bootMer for “simulestimate”, i.e., where each
simulation is followed by refitting the model.
Examples
## 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 starting from scratch
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]  ## RHS of equation only
simulate(form,newdata=simdat,family=binomial,
    newparams=params)
## simulate from negative binomial distribution instead
simulate(form,newdata=simdat,family=negative.binomial(theta=2.5),
    newparams=params)