This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Class: CompactLinearModel

Predict response of linear regression model


ypred = predict(mdl,Xnew)
[ypred,yci] = predict(mdl,Xnew)
[ypred,yci] = predict(mdl,Xnew,Name,Value)


ypred = predict(mdl,Xnew) returns the predicted response of the mdl linear regression model to the points in Xnew.

[ypred,yci] = predict(mdl,Xnew) returns confidence intervals for the true mean responses.

[ypred,yci] = predict(mdl,Xnew,Name,Value) predicts responses with additional options specified by one or more Name,Value pair arguments.


  • For predictions with added noise, use random.

Input Arguments

expand all

Linear model object, specified as a full LinearModel object constructed using fitlm or stepwiselm, or a compacted CompactLinearModel object constructed using compact.

New predictor input values, specified as a table, dataset array, or numeric matrix.

  • If Xnew is a table or dataset array, it must contain the predictor names in mdl.

  • If Xnew is a numeric matrix, it must have the same number of variables (columns) as was used to create mdl. Furthermore, all variables used in creating mdl must be numeric.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

expand all

Alpha value for confidence interval, specified as the comma-separated pair consisting of 'Alpha' and a numeric value in the range [0,1]. The confidence level of yci is equal to 100(1–alpha)%. For example, specifying Alpha as 0.05 corresponds to a 95% confidence interval.

Prediction type, specified as the comma-separated pair consisting of 'Prediction' and one of the following.

  • 'curve'predict predicts confidence bounds for the fitted mean values.

  • 'observation'predict predicts confidence bounds for the new observations. This results in wider bounds because the error in a new observation is equal to the error in the estimated mean value, plus the variability in the observation from the true mean.

For details, see polyconf.

Flag to compute simultaneous confidence bounds, specified as the comma-separated pair consisting of Simultaneous and either false or true. If you specify true, then the function computes confidence bounds are for all predictor values simultaneously. If you specify false, then the function calculates confidence bounds using a hold for each individual predictor value. Simultaneous bounds are wider than separate bounds, because it is more stringent to require that the entire curve be within the bounds than to require that the curve at a single predictor value be within the bounds.

For details, see polyconf.

Output Arguments

expand all

Predicted mean response values evaluated at Xnew, returned as a numeric vector. ypred is the same size as Xnew.

Confidence intervals for predicted responses, returned as a two-column matrix with each row providing one interval. The meaning of the confidence interval depends on the settings of the name-value pairs.


expand all

Create a model of car mileage as a function of weight, and predict the response.

Create a quadratic model of car mileage as a function of weight from the carsmall data.

load carsmall
X = Weight;
y = MPG;
mdl = fitlm(X,y,'quadratic');

Create predicted responses to the data.

Xnew = X;
ypred = predict(mdl,Xnew);

Plot the original responses and the predicted responses to see how they differ.


Related Examples


feval gives the same predictions, but uses multiple input arrays with one component in each input argument. feval can be simpler to use with a model created from a table or dataset array, although feval does not give confidence intervals on its predictions.

random predicts with added noise.

Was this topic helpful?