HOWTO · R
How to Use the Predict Function on a Linear Regression Model in R
Learn how to use R's predict() with a fitted linear regression model, format newdata, and match its feature column to the model formula.
On this page
To predict response values from a fitted linear regression in R, call predict() with the model and a data frame in newdata. The data frame must contain a feature column named exactly as it appears in the model formula. For example, a model fitted with lm(Response ~ Feature, data = training) expects newdata to contain the exact feature-column name Feature.
Use the predict() Function on a Linear Regression Model in R
This self-contained example was run successfully and returned three predictions. It creates a small training data frame, fits the model, and calls for predictions at three new feature values. The formula names Response as the quantity to estimate and Feature as its input; predict() returns one prediction for each row in newdata.
Feature <- 15:24
set.seed(654)
Response <- 2 * Feature + 5 + rnorm(10, mean = 0, sd = 3)
training <- data.frame(Response = Response, Feature = Feature)
model <- lm(Response ~ Feature, data = training)
newdata <- data.frame(Feature = c(20.5, 16.5, 22.5))
predict(object = model, newdata = newdata)
The call asks predict() to apply the fitted model to the rows of newdata; it returns one estimate per supplied row, in row order. The values in Feature are inputs to the fitted relationship, while Response is the quantity being estimated. Naming object and newdata makes each argument’s role clear, and lets a reader see which data frame is used for prediction rather than the training data used by lm().
Pass new values as a data frame, not as a bare vector: predict(model, newdata = c(20.5, 16.5, 22.5)) does not provide the tabular input expected by this model’s prediction method. Also match the formula’s feature name. A data frame made as data.frame(NewFeature = c(20.5, 16.5, 22.5)) lacks the Feature column the fitted formula requests. In that situation, R may look for Feature in the model’s original environment; this can lead to a row-count warning and predictions that do not use the three requested values. The exact behavior depends on which variables are available there, so do not treat a returned vector as proof that the new values were used. Construct newdata with data.frame(Feature = ...) to supply the intended input explicitly.
Keep the formula and prediction data aligned when adapting the example. If the model uses a different predictor name, use that same name as a column in newdata; for multiple predictors, include a column for each predictor used by the formula. Each row then represents one case for which the model estimates Response. The model object also retains information from fitting, so prediction data should describe the new cases rather than repeat or rename training variables by accident.
The output is a numeric prediction vector, not a new fitted model or a table containing the original inputs. If you need to inspect estimates alongside the values supplied, retain newdata and combine it with the returned vector in a data frame. For example, assign the call to a variable such as predicted and then create a result table with Feature = newdata$Feature and Response = predicted. This makes the association between each requested feature value and its estimate explicit. For uncertainty intervals, predict.lm() also supports interval options; choose those when the task requires intervals rather than point estimates.