# random

Simulate responses with random noise for linear regression model

## Syntax

## Description

## Examples

### Simulate Response Data with Random Noise

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

data set.

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

Create simulated responses to the data with random noise.

ysim = random(mdl,X);

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

plot(X,y,'o',X,ysim,'x') legend('Data','Simulated')

## Input Arguments

`mdl`

— Linear regression model object

`LinearModel`

object | `CompactLinearModel`

object

Linear regression model object, specified as a `LinearModel`

object created by using `fitlm`

or `stepwiselm`

, or a `CompactLinearModel`

object created by using `compact`

.

`Xnew`

— New predictor input values

table | dataset array | matrix

New predictor input values, specified as a table, dataset array, or matrix. Each row of
`Xnew`

corresponds to one observation, and each column
corresponds to one variable.

If

`Xnew`

is a table or dataset array, it must contain predictors that have the same predictor names as in the`PredictorNames`

property of`mdl`

.If

`Xnew`

is a matrix, it must have the same number of variables (columns) in the same order as the predictor input used to create`mdl`

. Note that`Xnew`

must also contain any predictor variables that are not used as predictors in the fitted model. Also, all variables used in creating`mdl`

must be numeric. To treat numerical predictors as categorical, identify the predictors using the`'CategoricalVars'`

name-value pair argument when you create`mdl`

.

**Data Types: **`single`

| `double`

| `table`

## Output Arguments

`ysim`

— Simulated response values

numeric vector

Simulated response value, returned as a numeric vector. The simulated
value is the predicted response values at
`Xnew`

perturbed by random noise.
The noise is independent and normally distributed, with mean
equal to zero and variance equal to the estimated error variance
of the model.

## Alternative Functionality

For predictions without random noise, use `predict`

or `feval`

. These two functions give the same
predictions.

`predict`

accepts a single input argument containing all predictor variables, and gives confidence intervals on its predictions.`feval`

accepts multiple input arguments with one input for each predictor variable.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Use

`saveLearnerForCoder`

,`loadLearnerForCoder`

, and`codegen`

(MATLAB Coder) to generate code for the`random`

function. Save a trained model by using`saveLearnerForCoder`

. Define an entry-point function that loads the saved model by using`loadLearnerForCoder`

and calls the`random`

function. Then use`codegen`

to generate code for the entry-point function.`random`

can return a different sequence of numbers than MATLAB^{®}if either of the following is true:The output is nonscalar.

An input parameter is invalid for the distribution.

This table contains notes about the arguments of

`random`

. Arguments not included in this table are fully supported.Argument Notes and Limitations `mdl`

Suppose you train a linear model by using

`fitlm`

and specifying`'RobustOpts'`

as a structure with an anonymous function handle for the`RobustWgtFun`

field, use`saveLearnerForCoder`

to save the model, and then use`loadLearnerForCoder`

to load the model. In this case,`loadLearnerForCoder`

cannot restore the Robust property into the MATLAB Workspace. However,`loadLearnerForCoder`

can load the model at compile time within an entry-point function for code generation.For the usage notes and limitations of the model object, see Code Generation of the

`CompactLinearModel`

object.

`Xnew`

`Xnew`

must be a single-precision or double-precision matrix or a table containing numeric variables, categorical variables, or both.The number of rows, or observations, in

`Xnew`

can be a variable size, but the number of columns in`Xnew`

must be fixed.If you want to specify

`Xnew`

as a table, then your model must be trained using a table, and you must ensure that your entry-point function for prediction:Accepts data as arrays

Creates a table from the data input arguments and specifies the variable names in the table

Passes the table to

`predict`

For an example of this table workflow, see Generate Code to Classify Data in Table. For more information on using tables in code generation, see Code Generation for Tables (MATLAB Coder) and Table Limitations for Code Generation (MATLAB Coder).

For more information, see Introduction to Code Generation.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced in R2012a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)