random

Class: GeneralizedLinearModel

Simulate responses for generalized linear regression model

Syntax

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

Description

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

mdl

Generalized linear model, as constructed by fitglm or stepwiseglm.

Xnew

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.

'BinomialSize'

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

'Offset'

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

ysim

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.

Examples

expand all

Generalized Linear Model Simulation

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.

plot(X,y,'rx',Xnew,ysim,'bo',...
    Xnew,feval(mdl,Xnew),'g-')
legend('Data','Simulated','Fitted Mean',...
    'Location','best')

Alternatives

For predictions without random noise, use predict or feval.

Was this topic helpful?