predict.merMod

0th

Percentile

Predictions from a model at new data values

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

Usage
# 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"), allow.new.levels = FALSE,
na.action = na.pass, …)
Arguments
object

a fitted model object

newdata

data frame 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 and/or (for GLMMs) beta.

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.

random.only

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

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

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.

Details

• 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.

Value

a numeric vector of predicted values

Aliases
• predict.merMod
Examples
# NOT RUN {
(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))
# }
# NOT RUN {
<!-- %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) predict.fun <- function(my.lmm) { predict(my.lmm, newdata = df.predicted, re.form = NA) # This is predict.merMod } df.predicted$ml.value <- predict.fun(lmm) # Make predictions in 100 bootstraps of the LMM. Use these to get confidence # intervals. lmm.boots <- bootMer(lmm, predict.fun, nsim = 100) df.predicted <- cbind(df.predicted, confint(lmm.boots)) 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)