predict - Predict responses of generalized linear regression model - MATLAB (original) (raw)

Predict responses of generalized linear regression model

Syntax

Description

[ypred](#mw%5Fc9d3c562-a78b-4844-a36b-d822f9ab0812) = predict([mdl](#btbshg4%5Fsep%5Fshared-mdl),[Xnew](#btbshg4%5Fsep%5Fshared-Xnew)) returns the predicted response values of the generalized linear regression modelmdl to the points in Xnew.

example

[[ypred](#mw%5Fc9d3c562-a78b-4844-a36b-d822f9ab0812),[yci](#mw%5F20307ba0-ca31-4400-8b5f-6988d092ecf4)] = predict([mdl](#btbshg4%5Fsep%5Fshared-mdl),[Xnew](#btbshg4%5Fsep%5Fshared-Xnew)) also returns confidence intervals for the responses atXnew.

[[ypred](#mw%5Fc9d3c562-a78b-4844-a36b-d822f9ab0812),[yci](#mw%5F20307ba0-ca31-4400-8b5f-6988d092ecf4)] = predict([mdl](#btbshg4%5Fsep%5Fshared-mdl),[Xnew](#btbshg4%5Fsep%5Fshared-Xnew),[Name,Value](#namevaluepairarguments)) specifies additional options using one or more name-value pair arguments. For example, you can specify the confidence level of the confidence interval.

example

Examples

collapse all

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

Generate sample data 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','Distribution','poisson');

Create data points for prediction.

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

Predict responses at the data points.

ypred = predict(mdl,Xnew);

Plot the predictions.

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

Figure contains an axes object. The axes object contains an object of type surface.

Fit a generalized linear regression model, and then save the model by using saveLearnerForCoder. Define an entry-point function that loads the model by using loadLearnerForCoder and calls the predict function of the fitted model. Then use codegen (MATLAB Coder) to generate C/C++ code. Note that generating C/C++ code requires MATLAB® Coder™.

This example briefly explains the code generation workflow for the prediction of linear regression models at the command line. For more details, see Code Generation for Prediction of Machine Learning Model at Command Line. You can also generate code using the MATLAB Coder app. For details, see Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App.

Train Model

Generate sample data 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. Specify the Poisson distribution for the response.

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

Save Model

Save the fitted generalized linear regression model to the file GLMMdl.mat by using saveLearnerForCoder.

saveLearnerForCoder(mdl,'GLMMdl');

Define Entry-Point Function

In your current folder, define an entry-point function named mypredictGLM.m that does the following:

function [yhat,ci] = mypredictGLM(x,varargin) %#codegen %MYPREDICTGLM Predict responses using GLM model % MYPREDICTGLM predicts 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 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'); narginchk(1,Inf); [yhat,ci] = predict(CompactMdl,x,varargin{:}); end

Add the %#codegen compiler directive (or pragma) to the entry-point function after the function signature to indicate that you intend to generate code for the MATLAB algorithm. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in errors during code generation.

Generate Code

Generate code for the entry-point function using codegen (MATLAB Coder). Because C and C++ are statically typed languages, you must determine the properties of all variables in the entry-point function at compile time. To specify the data type and exact input array size, pass a MATLAB® expression that represents the set of values with a certain data type and array size. Use coder.Constant (MATLAB Coder) for the names of name-value pair arguments.

Create points for prediction.

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

Generate code and specify returning 90% simultaneous confidence intervals on the predictions.

codegen mypredictGLM -args {Xnew,coder.Constant('Alpha'),0.1,coder.Constant('Simultaneous'),true}

Code generation successful.

codegen generates the MEX function mypredictGLM_mex with a platform-dependent extension.

If the number of observations is unknown at compile time, you can also specify the input as variable-size by using coder.typeof (MATLAB Coder). For details, see Specify Variable-Size Arguments for Code Generation and Specify Types of Entry-Point Function Inputs (MATLAB Coder).

Verify Generated Code

Compare predictions and confidence intervals 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);

The returned values from mypredictGLM_mex might include round-off differences compared to the values from predict. In this case, compare the values allowing a small tolerance.

find(abs(yhat1-yhat2) > 1e-6)

ans =

0×1 empty double column vector

find(abs(ci1-ci2) > 1e-6)

ans =

0×1 empty double column vector

The comparison confirms that the returned values are equal within the tolerance 1e–6.

Input Arguments

collapse all

Data Types: single | double | table

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: [ypred,yci] = predict(Mdl,Xnew,'Alpha',0.01,'Simultaneous',true) returns the confidence interval yci with a 99% confidence level, computed simultaneously for all predictor values.

Data Types: single | double

Number of trials for the binomial distribution, specified as the comma-separated pair consisting of 'BinomialSize' and a scalar or vector of the same length as the response.predict expands the scalar input into a constant array of the same size as the response. The scalar input means that all observations have the same number of trials.

The meaning of the output values in ypred depends on the value of 'BinomialSize'.

Data Types: single | double

Data Types: single | double

Output Arguments

collapse all

Predicted response values at Xnew, returned as a numeric vector.

For a binomial model, the meaning of the output values inypred depends on the value of the'BinomialSize' name-value pair argument.

For a model with an offset, specify the offset value by using the'Offset' name-value pair argument. Otherwise,predict uses 0 as the offset value.

Confidence intervals for the responses, returned as a two-column matrix with each row providing one interval. The meaning of the confidence interval depends on the settings of the name-value pair arguments'Alpha' and'Simultaneous'.

Alternative Functionality

Extended Capabilities

expand all

Usage notes and limitations:

For more information, see Introduction to Code Generation.

Version History

Introduced in R2012a