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.

random

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

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

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')

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.

saveCompactModel(mdl,'GLMMdl');

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 = loadCompactModel('GLMMdl');
narginchk(1,Inf);
y = random(CompactMdl,x,varargin{:});
end


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.

figure;
plot(x,y,'bo',x,ysim,'r*');
legend('Observed responses','Simulated responses');
xlabel('x');
ylabel('y');

The observed and simulated responses appear to be similarly distributed.

Alternatives

For predictions without random noise, use predict or feval.

Extended Capabilities

Introduced in R2012a