Documentation

predict

Class: CompactLinearModel

Predict response of linear regression model

Syntax

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

Description

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.

Tips

  • For predictions with added noise, use random.

Input Arguments

expand all

mdl — Linear model objectLinearModel object | CompactLinearModel object

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

Xnew — New predictor input valuestable | dataset array | numeric matrix

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.

'Alpha' — Alpha value for confidence interval0.05 (default) | numeric value in the range [0,1]

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' — Prediction type'curve' (default) | 'observation'

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.

'Simultaneous' — Flag to compute simultaneous confidence boundsfalse (default) | true

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

ypred — Predicted mean response valuesnumeric vector

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

yci — Confidence intervals for predicted responsestwo-column numeric matrix

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.

Examples

expand all

Predict Response Values Using Linear Model

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.

plot(X,y,'o',Xnew,ypred,'x')
legend('Data','Predictions')

Related Examples

Alternatives

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?