Predictions from a model at new data values

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

# S3 method for merMod
predict(object, newdata = NULL, newparams = NULL,
	re.form = NULL, ReForm, REForm, REform,
        random.only=FALSE, terms = NULL,
	type = c("link", "response"), = FALSE,
	na.action = na.pass, …)

a fitted model object


data frame for which to evaluate predictions.


new parameters to use in evaluating predictions, specified as in the start parameter for lmer or glmer -- a list with components theta and/or (for GLMMs) beta.


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.


(logical) ignore fixed effects, making predictions only using random effects?


a terms object - unused at present.


character string - either "link", the default, or "response" indicating the type of prediction object returned.

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 data with previously unobserved levels (or NAs).


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.


  • If any random effects are included in re.form (see below), newdata must contain columns corresponding to all of the grouping variables and random effects used in the original model, even if not all are used in prediction; however, they can be safely set to NA in this case.

  • There is no option for computing standard errors of predictions because it is difficult to define an efficient method that incorporates uncertainty in the variance parameters; we recommend bootMer for this task.


a numeric vector of predicted values

  • predict.merMod
(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))
# }
<!-- %dont -->
# }
Documentation reproduced from package lme4, version 1.1-21, License: GPL (>= 2)

Community examples

ChrisHIV at Oct 27, 2019 lme4 v1.1-21

An example of making predictions from a linear mixed model, with confidence intervals. ``` set.seed(1) # Define a model y = mx + c, with a random effect of person on both m and c m <- 1 c <- 3 people <- c("alice", "bob", "charlie", "doris") df.true.params <- data.frame(person = people, m = m + rnorm(4), c = c + rnorm(4)) # Observe each person from x = 0 to x = 3; use that person's random effect, but # add an error term independently to every observation. obs.points <- 0:3 df.observed <- data.frame(person = rep(people, 4), x = sort(rep(obs.points, 4))) df.observed$y <- rep(df.true.params$m, 4) * df.observed$x + rep(df.true.params$c, 4) + rnorm(16) # 4 people * 4 observations each df.observed # Plot everyone's observations library(ggplot2) plot <- ggplot(data=df.observed, aes(x=x, y=y, colour=person)) + geom_point() plot # Fit an LMM to the observations library(lme4) lmm <- lmer(data = df.observed, y ~ x + (x | person)) summary(lmm) # Make predictions for y = mx + c for an unspecified person (i.e. the population # expectation). df.predicted <- data.frame(person = NA, x = obs.points) <- function(my.lmm) { predict(my.lmm, newdata = df.predicted, re.form = NA) # This is predict.merMod } df.predicted$ml.value <- # Make predictions in 100 bootstraps of the LMM. Use these to get confidence # intervals. <- bootMer(lmm,, nsim = 100) df.predicted <- cbind(df.predicted, confint( df.predicted # Plot the ML prediction and its confidence intervals plot + geom_line(data=df.predicted, aes(x=obs.points, y=df.predicted$ml.value)) + geom_ribbon(data=df.predicted, aes(x=obs.points, ymin=df.predicted$`2.5 %`, ymax=df.predicted$`97.5 %`), fill="gray", alpha=0.5, inherit.aes = FALSE) ```