This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Class: CompactLinearModel

Simulate responses for linear regression model


ysim = random(mdl)
ysim = random(mdl,Xnew)


ysim = random(mdl) simulates responses from the fitted linear model mdl at the original design points.

ysim = random(mdl,Xnew) simulates responses from the mdl linear model to the data in Xnew, adding random noise.

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.

Output Arguments

expand all

Predicted mean values at Xnew, perturbed by random noise, returned as a numeric vector. The noise is independent and normally distributed, with mean equal to zero, and variance equal to the estimated error variance of the model.


expand all

Create a model of car mileage as a function of weight, and simulate 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 simulated responses to the data.

Xnew = X;
ysim = random(mdl,Xnew);

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



For predictions without random noise, use predict or feval.

Extended Capabilities

Introduced in R2012a