lme4 (version 1.1-9)

predict.merMod: Predictions from a model at new data values

Description

The predict method for merMod objects, i.e. results of lmer(), glmer(), etc.

Usage

## S3 method for class 'merMod':
predict(object, newdata = NULL, newparams = NULL,
	re.form = NULL, ReForm, REForm, REform,	terms = NULL,
	type = c("link", "response"), allow.new.levels = FALSE,
	na.action = na.pass, ...)

Arguments

object
a fitted model object
newdata
data frame of for which to evaluate predictions.
newparams
new parameters to use in evaluating predictions, specified as in the start parameter for lmer or glmer -- a list with components theta<
re.form
formula for random effects to condition on. If NULL, include all random effects; if NA or ~0, include no random effects
ReForm, REForm, REform
allowed for backward compatibility: re.form is now the preferred argument name.
terms
a terms object - unused at present.
type
character string - either "link", the default, or "response" indicating the type of prediction object returned.
allow.new.levels
logical if new levels (or NA values) in newdata are allowed. If FALSE (default), such new values in newdata will trigger an error; if TRUE, then the prediction will use the unconditional (population-level) values for
na.action
function determining what should be done with missing values for fixed effects in newdata. The default is to predict NA: see na.pass<
...
optional additional parameters. None are used at present.

Value

  • a numeric vector of predicted values

Examples

Run this code
(gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 |herd), cbpp, binomial))
str(p0 <- predict(gm1))            # fitted values
str(p1 <- predict(gm1,re.form=NA))  # fitted values, unconditional (level-0)
newdata <- with(cbpp, expand.grid(period=unique(period), herd=unique(herd)))
str(p2 <- predict(gm1,newdata))    # new data, all RE
str(p3 <- predict(gm1,newdata,re.form=NA)) # new data, level-0
str(p4 <- predict(gm1,newdata,re.form= ~(1|herd))) # explicitly specify RE
stopifnot(identical(p2, p4))
stopifnot(identical(p1, predict(gm1,ReForm=NA)))
stopifnot(identical(p3, predict(gm1,newdata,REForm=NA)))
stopifnot(identical(p4, predict(gm1,newdata,REform=~(1|herd))))

## predict() should work with variable names with spaces [as lm() does]:
dd <- expand.grid(y=1:3, "Animal ID" = 1:9)
fm <- lmer(y ~ 1 + (1 | `Animal ID`),  dd)
summary(fm)
isel <- c(7, 9, 11, 13:17, 20:22)
stopifnot(all.equal(vcov(fm)[1,1], 0.02564102564),
	  all.equal(unname(predict(fm, newdata = dd[isel,])),
		    unname( fitted(fm) [isel])))

Run the code above in your browser using DataCamp Workspace