predict( )

Use a fitted model to calculate predicted values. predict() is a generic function, and for models made with lm(), it uses the predict.lm() method behind the scenes.

Required Library

# predict() is part of base R (stats), loaded by default — no package installation needed

Syntax

predict(object)
predict(object, newdata = df)

Once you have fitted a linear regression model with lm(), predict(object) gives you the model’s predicted values for the data you used to fit it. If you instead want predictions for new x-values, predict(object, newdata = df) lets you supply a data frame of new predictor values and returns the model’s predicted response for each one.

predict() is the natural next step after fitting a linear regression model with lm(). Once you have a model that describes the relationship between your x and y variables, predict() lets you use that relationship to estimate y for any x-value, including ones not in your original data.

You will use predict() the same way regardless of what kind of model you have fitted. Behind the scenes, R picks the right calculation to use based on the model’s class, so for an lm() model, R automatically uses predict.lm() without you needing to specify this.

Examples

Predict new values based on model

This model describes first-order drug degradation, where the natural log of concentration decreases linearly with time. Because the model was fitted on log-transformed concentration, predict() returns predicted log-concentration values. To get the predicted concentration itself, back-transform with exp().

Rather than calculating the predictions separately and joining them back on afterwards, you can do it in one step with mutate(). Inside the pipe, . refers to new_time itself as it flows through — so newdata = . tells predict() to use these new time points, and wrapping the whole thing in exp() back-transforms the result before mutate() adds it as a new column.

Predict a single new value and extract the result

If you only need a prediction for one new time point, predict() still returns a vector — here, a vector of length 1. Since the model was fitted on log-transformed concentration, the result needs to be back-transformed with exp(). Because there’s only one value, you can pull it out directly as a plain number with [[1]], rather than keeping it inside a data frame.

Compare predicted and actual values

This model describes first-order drug degradation, where log-concentration decreases linearly with time. Adding the predicted values as a new column lets you see, row by row, how close the model’s estimates are to the concentrations you actually measured. Since the model works on the log scale, the predicted values are back-transformed with exp() before comparing.

Argument Overview

Required arguments must be included when using a function while optional arguments can be included on demand.

object — fitted model object model object Required

The fitted model to use for prediction. For this site, the common example is an lm() model.

predict(model)

Data Types: model object, usually from lm()

newdata — new predictor values data frame | tibble Optional

Data frame containing new predictor values for prediction. The column names must match the predictor names used in the model.

predict(model, newdata = new_conc)

Data Types: data.frame | tibble · Default: original model data if omitted

— method-specific prediction options various Optional

Additional arguments passed to the method used by predict(). For linear models, this includes options such as confidence intervals or standard errors.

Data Types: depends on the model class