Skip to main content
v2026.11,986 entries · CC-BY 4.0

Prediction in R: predict() for lm and glm

How prediction in R works with predict(): newdata, lm intervals, glm link versus response, how to read each return shape, and common warnings.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

Prediction in R means calling predict() on a fitted model object to get estimated values for new data. The function is generic: the model class you pass decides which method runs, and the methods for lm and glm differ in the arguments they accept. The most common source of confusion is that only predict.lm has an interval argument, while only predict.glm has a type that switches between the link and the response scale.

Ask CASRAI · free to tryQuestion about this guide? Ask CASRAI answers with linked sources. 2 free questions a day, no account.

Ask your first question freeSubscribe

What prediction in R is used for in research

Researchers fit a model to observed data and then ask what the model implies for cases it has not seen, for example a new value of a predictor in a dose-response study. predict() answers that question. The R documentation states that the form of the value returned depends on the class of the argument, so the same call can return a vector, a matrix or a list. Searches such as “prediction r” and “r prediction” are about this function.

If you call predict() without new data on a linear model, you get the fitted values back; the separate guide on fitted values in R covers that case and how it differs from predicting.

Install or access

predict() is part of the stats package, which ships with R, so no extra package is needed. R itself is distributed through CRAN, the Comprehensive R Archive Network (cran.r-project.org); its binaries are listed at cran.r-project.org/bin. The reference pages for predict, predict.lm and predict.glm are listed under Sources.

Step-by-step: predictions from a linear model

  1. Fit the model. Using the built-in cars data set: fit <- lm(dist ~ speed, data = cars)
  2. Build a data frame of new cases. Its column names must match the predictors in the formula: new_cases <- data.frame(speed = c(10, 15, 20))
  3. Predict. predict(fit, newdata = new_cases) returns a vector with one prediction per row of newdata.
  4. Add an interval if you need one. predict(fit, newdata = new_cases, interval = "confidence") or interval = "prediction". The default level is 0.95; change it with level = 0.9.
  5. Ask for standard errors instead. predict(fit, newdata = new_cases, se.fit = TRUE) returns a list.

The documented usage of the method is predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95, type = c("response", "terms"), terms = NULL, na.action = na.pass, pred.var = res.var/weights, weights = 1, rankdeficient = ..., tol = 1e-6, verbose = FALSE, ...).

Confidence interval or prediction interval

Use interval = "confidence" for the uncertainty of the predicted mean at each new case. Use interval = "prediction" for a range that covers a future observation; because it also allows for the residual variation of individual observations, it is wider than the confidence interval. The manual notes that prediction variances and prediction intervals always refer to future observations. For background on the first kind, see the dictionary entry on confidence intervals.

Step-by-step: predictions from a logistic or other glm

  1. Fit the model. fit_glm <- glm(am ~ wt, data = mtcars, family = binomial)
  2. Predict on the response scale. predict(fit_glm, newdata = data.frame(wt = c(2.5, 3.5)), type = "response")

The documented usage is predict(object, newdata = NULL, type = c("link", "response", "terms"), se.fit = FALSE, dispersion = NULL, terms = NULL, na.action = na.pass, ...). The default type is "link", the scale of the linear predictors; for a binomial model with a logit link that is the log-odds. type = "response" returns probabilities. predict.glm has no interval argument.

predict.lm and predict.glm side by side

Argument predict.lm predict.glm
interval and level Yes: “none”, “confidence” or “prediction”; level 0.95 by default Not an argument
type “response” (default) or “terms” “link” (default), “response” or “terms”
se.fit Yes, default FALSE Yes, default FALSE
newdata Optional; if omitted, fitted values are used Default NULL
na.action na.pass na.pass

Output and how to read it

  • A plain vector when interval is “none” and se.fit is FALSE: one predicted value per row of newdata.
  • A matrix with columns fit, lwr and upr when interval is set on an lm: the prediction and the lower and upper limits at the chosen level.
  • A list when se.fit = TRUE: for lm the components are fit, se.fit, residual.scale and df; for glm they are fit, se.fit and residual.scale.
  • A matrix with one column per term when type = "terms".

For a glm, remember which scale you asked for. A value from the default type = "link" is not a probability; request type = "response" when you want one. To learn how the fitted model itself is summarised before you predict, see how to read a regression table and the assumptions of simple regression.

Troubleshooting

Predictions come back for the original data, not my new cases

For lm, if newdata is omitted the fitted values are used. Pass the data frame by name: newdata = new_cases.

A warning about variables of different lengths

The manual states that variables are looked up in newdata first and then in the environment of the formula, and that a warning is given if the variables found are not the same length as those in newdata. Check that the column names in newdata are identical to the predictor names in the formula.

NA in the prediction

Both methods default to na.action = na.pass, so a missing value in newdata gives an NA prediction for that row.

NA or a warning from a rank-deficient model

For lm the argument rankdeficient controls what happens when the model has non-estimable coefficients. The default "warnif" warns only when non-estimable cases are predicted; "NA" returns NA silently for them.

No interval for a glm

predict.glm does not take interval. Ask for se.fit = TRUE to get standard errors and build limits on the scale you need.

R and its stats package are free to download from CRAN. Within base R, the choice is made by the class of the fitted object:

Situation What the documentation says
Linear model (lm) predict.lm: newdata, interval, level, se.fit
Generalized linear model (glm) predict.glm: newdata, type (“link” or “response”), se.fit
Time-series models The generic predict page says time-series methods take n.ahead
Machine-learning models Packages define their own predict methods; see the guide to random forests in R

If you are comparing regression approaches before you predict, the comparison of ridge and lasso regression is a related read.

FAQ

What does predict() do in R?

It returns predictions from a fitted model object. What it returns, and which arguments it accepts, depends on the class of the object.

How do I predict on new data in R?

Fit the model, put the new predictor values in a data frame with the same column names as in the formula, and pass it as newdata.

What is the difference between a confidence interval and a prediction interval in predict()?

The confidence interval is for the predicted mean at each new case; the prediction interval is for a future observation and is wider because it also allows for residual variation. Only predict.lm takes the interval argument.

Why does my logistic regression predict() not give probabilities?

The default type for predict.glm is “link”, the scale of the linear predictors. Use type = "response".

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Prediction in R: predict() for lm and glm

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →