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 Use Custom-Defined Link Function example.
Enter the sample data.
Suppose that the inverse normal pdf is an appropriate link function for the problem.
Define a function named myInvNorm.m
that accepts values of
and returns corresponding values of the inverse of the standard normal cdf.
function in = myInvNorm(mu) %#codegen
%myInvNorm Inverse of standard normal cdf for code generation
% myInvNorm is a GLM link function that accepts a numeric vector mu, and
% returns in, which is a numeric vector of corresponding values of the
% inverse of the standard normal cdf.
%
in = norminv(mu);
end
Define another function named myDInvNorm.m
that accepts values of
and returns corresponding values of the derivative of the link function.
function din = myDInvNorm(mu) %#codegen
%myDInvNorm Derivative of inverse of standard normal cdf for code
%generation
% myDInvNorm corresponds to the derivative of the GLM link function
% myInvNorm. myDInvNorm accepts a numeric vector mu, and returns din,
% which is a numeric vector of corresponding derivatives of the inverse
% of the standard normal cdf.
%
din = 1./normpdf(norminv(mu));
end
Define another function named myInvInvNorm.m
that accepts values of
and returns corresponding values of the inverse of the link function.
function iin = myInvInvNorm(mu) %#codegen
%myInvInvNorm Standard normal cdf for code generation
% myInvInvNorm is the inverse of the GLM link function myInvNorm.
% myInvInvNorm accepts a numeric vector mu, and returns iin, which is a
% numeric vector of corresponding values of the standard normal cdf.
%
iin = normcdf(mu);
end
Create a structure array that specifies each of the link functions. Specifically, the structure array contains fields named 'Link'
, 'Derivative'
, and 'Inverse'
. The corresponding values are the names of the functions.
link =
struct with fields:
Link: 'myInvNorm'
Derivative: 'myDInvNorm'
Inverse: 'myInvInvNorm'
Fit a GLM for y
on x
using the link function link
. Return the structure array of statistics.
b
is a 2-by-1 vector of regression coefficients.
In your current working folder, define a function called classifyGLM.m
that:
Accepts measurements with columns corresponding to those in x
, regression coefficients whose dimensions correspond to b
, a link function, the structure of GLM statistics, and any valid glmval
name-value pair argument
Returns predictions and confidence interval margins of error
function [yhat,lo,hi] = classifyGLM(b,x,link,varargin) %#codegen
%CLASSIFYGLM Classify measurements using GLM model
% CLASSIFYGLM classifies the n observations in the n-by-1 vector x using
% the GLM model with regression coefficients b and link function link,
% and then returns the n-by-1 vector of predicted values in yhat.
% CLASSIFYGLM also returns margins of error for the predictions using
% additional information in the GLM statistics structure stats.
narginchk(3,Inf);
if(isstruct(varargin{1}))
stats = varargin{1};
[yhat,lo,hi] = glmval(b,x,link,stats,varargin{2:end});
else
yhat = glmval(b,x,link,varargin{:});
end
end
Generate a MEX function from classifyGLM.m
. Because C uses static typing, codegen
must determine the properties of all variables in MATLAB® files at compile time. To ensure that the MEX function can use the same inputs, use the -args
argument to specify the following in the order given:
Regression coefficients b
as a compile-time constant
In-sample observations x
Link function as a compile-time constant
Resulting GLM statistics as a compile-time constant
Name 'Confidence'
as a compile-time constant
Confidence level 0.9
To designate arguments as compile-time constants, use coder.Constant
.
codegen
generates the MEX file classifyGLM_mex.mexw64
in your current folder. The file extension depends on your system platform.
Compare predictions by using glmval
and classifyGLM_mex
. Specify name-value pair arguments in the same order as in the -args
argument in the call to codegen
.
agree1 =
logical
1
agree2 =
logical
1
agree3 =
logical
1
The generated MEX function produces the same predictions as predict
.