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.
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
- Fit the model. Using the built-in
carsdata set:fit <- lm(dist ~ speed, data = cars) - 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)) - Predict.
predict(fit, newdata = new_cases)returns a vector with one prediction per row ofnewdata. - Add an interval if you need one.
predict(fit, newdata = new_cases, interval = "confidence")orinterval = "prediction". The defaultlevelis 0.95; change it withlevel = 0.9. - 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
- Fit the model.
fit_glm <- glm(am ~ wt, data = mtcars, family = binomial) - 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
intervalis “none” andse.fitis FALSE: one predicted value per row ofnewdata. - A matrix with columns
fit,lwranduprwhenintervalis set on anlm: the prediction and the lower and upper limits at the chosenlevel. - A list when
se.fit = TRUE: forlmthe components arefit,se.fit,residual.scaleanddf; forglmthey arefit,se.fitandresidual.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.
Free alternatives and related prediction methods
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
- R documentation, predict: Model Predictions (R-devel manual, checked 2026-10-29).
- R documentation, predict.lm: Predict method for Linear Model Fits (R-devel manual, checked 2026-10-29).
- R documentation, predict.glm: Predict method for GLM Fits (R-devel manual, checked 2026-10-29).
- The Comprehensive R Archive Network, cran.r-project.org (checked 2026-10-29).







