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: GeneralizedLinearModel

Simulate responses for generalized linear regression model


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


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

ysim = random(mdl,Xnew,Name,Value) simulates responses with additional options specified by one or more Name,Value pair arguments.

Input Arguments


Generalized linear model, as constructed by fitglm or stepwiseglm.


Points at which mdl predicts responses.

  • 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.


The value of the binomial n parameter for each row in the training data. BinomialSize can be a vector the same length as Xnew, or a scalar that applies to each row. The default value 1 produces ysim values that are predicted proportions. Use BinomialSize only if mdl is fit to a binomial distribution.

Default: 1


Value of the offset for each row in Xnew. Offset can be a vector the same length as Xnew, or a scalar that applies to each row. The offset is used as an additional predictor with a coefficient value fixed at 1. In other words, if b is the fitted coefficient vector, and link is the link function,

link(ysim) = Offset + Xnew * b.

Default: zeros(size(Xnew,1))

Output Arguments


Vector of simulated values at Xnew.

random generates ysim using random values with mean given by the fitted model, and with the distribution used in mdl. The values in ysim are independent conditional on the predictors. For binomial and Poisson fits, random generates ysim with the specified distribution with no adjustment for any estimated dispersion.


expand all

Create a generalized linear model, and simulate its response to new data.

Generate artificial data for the model, Poisson random numbers with one underlying predictors X.

rng('default') % reproducible
X = rand(20,1);
mu = exp(1 + 2*X);
y = poissrnd(mu);

Create a generalized linear regression model of Poisson data.

mdl = fitglm(X,y,'y ~ x1','distr','poisson');

Create points for prediction.

Xnew = (0:.05:1)';

Simulate responses at the new points.

ysim = random(mdl,Xnew);

Plot the simulated values along with the original values.

legend('Data','Simulated','Fitted Mean',...

Related Examples


For predictions without random noise, use predict or feval.

Was this topic helpful?