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, specified as a full GeneralizedLinearModel object constructed using fitglm or stepwiseglm, or a compacted CompactGeneralizedLinearModel object constructed using compact.


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 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',... 

Train a generalized linear model, and then generate code from a function that draws random responses from the model given new predictor data.

Generate artificial data, specifically:

  • .

  • and .

  • .

rng('default') % For reproducibility
x = 1 + randn(100,1)*0.5;
beta = -2;
p = exp(1 + x*beta)./(1 + exp(1 + x*beta)); % Inverse logit
n = 10;
y = binornd(n,p,100,1);

Create a generalized linear regression model of binomial data. Specify a binomial sample size of 10.

mdl = fitglm(x,y,'y ~ x1','Distribution','Binomial','BinomialSize',n);

Save the fitted generalized linear model to the file GLMMdl.mat.


In your current folder, define a function called myrandomGLM.m that:

  • Accepts measurements with columns corresponding to those in X and valid name-value pair arguments

  • Loads the fitted generalized linear model in GLMMdl.mat

  • Simulates responses from the loaded GLM model

function y = myrandomGLM(x,varargin) %#codegen
%MYRANDOMGLM Simulate response using GLM model 
%   MYRANDOMGLM simulates responses for the n observations in the n-by-1
%   vector x using the GLM model stored in the MAT-file GLMMdl.mat, and
%   then returns the simulations in the n-by-1 vector y.
CompactMdl = loadLearnerForCoder('GLMMdl');
y = random(CompactMdl,x,varargin{:});

Generate a MEX function from myrandomGLM.m. Specify the in-sample predictor data X and 10 for the binomial parameter n. Because C uses static typing, codegen must determine the properties of all variables in MATLAB® files at compile time. To designate arguments as compile-time constants, use coder.Constant.

codegen -config:mex myrandomGLM -args {x,coder.Constant('BinomialSize'),coder.Constant(n)}

codegen generates the MEX file myrandomGLM_mex.mexw64 in your current folder. The file extension depends on your platform.

Simulate responses using the MEX function.

ysim = myrandomGLM_mex(x,'BinomialSize',n);

Plot the simulated values and the data in the same figure.

legend('Observed responses','Simulated responses');

The observed and simulated responses appear to be similarly distributed.


For predictions without random noise, use predict or feval.

Extended Capabilities

Introduced in R2012a