predict

Class: GeneralizedLinearModel

Predict response of generalized linear regression model

Syntax

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

Description

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.

Tips

  • For predictions with added noise, use random.

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

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.

'alpha'

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

Default: 0.05, meaning a 95% confidence interval.

'BinomialSize'

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

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

Default: zeros(size(Xnew,1))

'Simultaneous'

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

ypred

Vector of predicted mean values at Xnew.

yci

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.

Examples

expand all

Generalized Linear Model Predictions

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.

surf(Xtest1,Xtest2,reshape(ypred,9,9))

Create confidence intervals on the predictions.

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

Alternatives

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

random predicts with added noise.

Was this topic helpful?