Predict response of generalized linear regression model


ypred = predict(mdl,Xnew)
[ypred,yci] = predict(mdl,Xnew)
[ypred,yci] = predict(mdl,Xnew,Name,Value)


ypred = predict(mdl,Xnew) returns the predicted response of the mdl generalized linear regression model to the points in Xnew.

[ypred,yci] = predict(mdl,Xnew) returns confidence intervals for the true mean responses.

[ypred,yci] = predict(mdl,Xnew,Name,Value) predicts 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.


Positive scalar from 0 to 1. Confidence level of yci is 100(1 – alpha)%.

Default: 0.05, meaning a 95% confidence interval.


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 ypred 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(ypred) = Offset + Xnew * b.

Default: zeros(size(Xnew,1))


Logical value specifying whether the confidence bounds are for all predictor values simultaneously (true), or hold for each individual predictor value (false). Simultaneous bounds are wider than separate bounds, because it is more stringent to require that the entire curve be within the bounds than to require that the curve at a single predictor value be within the bounds.

For details, see polyconf.

Default: false

Output Arguments


Vector of predicted mean values at Xnew.


Confidence intervals, a two-column matrix with each row providing one interval. The meaning of the confidence interval depends on the settings of the name-value pairs.


expand all

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

Generate artificial data for the model using Poisson random numbers with two underlying predictors X(1) and X(2).

rng('default') % For reproducibility
rndvars = randn(100,2);
X = [2+rndvars(:,1),rndvars(:,2)];
mu = exp(1 + X*[1;2]);
y = poissrnd(mu);

Create a generalized linear regression model of Poisson data.

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

Create points for prediction.

[Xtest1 Xtest2] = meshgrid(-1:.5:3,-2:.5:2);
Xnew = [Xtest1(:),Xtest2(:)];

Predict responses at the new points.

ypred = predict(mdl,Xnew);

Plot the predictions.


Create confidence intervals on the predictions.

[ypred yci] = predict(mdl,Xnew);

Train a generalized linear model, and then generate code from a function that classifies new observations based on the model. This example is based on the Generalized Linear Model Predictions example.

Generate artificial data.

rng('default') % For reproducibility
rndvars = randn(100,2);
X = [2+rndvars(:,1),rndvars(:,2)];
mu = exp(1 + X*[1;2]);
y = poissrnd(mu);

Create a generalized linear regression model. Specify that the distribution of the response is Poisson.

mdl = fitglm(X,y,'y ~ x1 + x2','Distribution','poisson');

Create points for prediction.

[Xtest1 Xtest2] = meshgrid(-1:.5:3,-2:.5:2);
Xnew = [Xtest1(:),Xtest2(:)];

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


Define a function in your working folder called mypredictGLM.m that:

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

  • Loads the fitted generalized linear model in GLMMdl.mat

  • Returns predictions and confidence interval bounds

function [yhat,ci] = mypredictGLM(x,varargin) %#codegen
%MYPREDICTGLM Predict response using GLM model 
%   MYPREDICTGLM predicts responses for the n observations in the n-by-1
%   vector x using using the GLM model stored in the MAT-file GLMMdl.mat,
%   and then returns the predictions in the n-by-1 vector yhat.
%   MYPREDICTGLM also returns confidence interval bounds for the
%   predictions in the n-by-2 vector ci.
CompactMdl = loadLearnerForCoder('GLMMdl');
[yhat,ci] = predict(CompactMdl,x,varargin{:});

Generate a MEX function from mypredictGLM.m. Specify returning 90% simultaneous confidence intervals on the predictions. 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 mypredictGLM -args {Xnew,coder.Constant('Alpha'),0.1,coder.Constant('Simultaneous'),true}

The MEX file mypredictGLM_mex.mexw64 generates to your working directory. The file extension depends on your system platform.

Compare predictions by using predict and mypredictGLM_mex. Specify name-value pair arguments in the same order as in the -args argument in the call to codegen.

[yhat1,ci1] = predict(mdl,Xnew,'Alpha',0.1,'Simultaneous',true);
[yhat2,ci2] = mypredictGLM_mex(Xnew,'Alpha',0.1,'Simultaneous',true);

comp1 = (yhat1 - yhat2)'*(yhat1 - yhat2);
comp2 = diag((ci1 - ci2)'*(ci1 - ci2));
agree1 = comp1 < eps
agree2 = comp2 < eps
agree1 =



agree2 =

  2x1 logical array


The generated MEX file produces the same predictions as predict.


  • For predictions with added noise, use random.

  • For a syntax that can be easier to use with models created from dataset arrays, try feval.


feval returns the same predictions, but uses separate input arrays for each predictor, instead of one input array containing all predictors.

random returns predictions with added noise.

Extended Capabilities

Introduced in R2012a