Compact generalized linear regression model class
CompactGeneralizedLinearModel
is a compact version of a full
generalized linear regression model object GeneralizedLinearModel
. Because a compact model does not store the input
data used to fit the model or information related to the fitting process, a
CompactGeneralizedLinearModel
object consumes less memory than a
GeneralizedLinearModel
object. You can still use a compact model to
predict responses using new input data, but some GeneralizedLinearModel
object functions do not work with a compact model.
Create a CompactGeneralizedLinearModel
model from a full, trained
GeneralizedLinearModel
model by using compact
.
fitglm
returns CompactGeneralizedLinearModel
when you work with tall arrays, and returns GeneralizedLinearModel
when
you work with inmemory tables and arrays.
CoefficientCovariance
— Covariance matrix of coefficient estimatesThis property is readonly.
Covariance matrix of coefficient estimates, specified as a pbyp matrix of numeric values. p is the number of coefficients in the fitted model.
For details, see Coefficient Standard Errors and Confidence Intervals.
Data Types: single
 double
CoefficientNames
— Coefficient namesThis property is readonly.
Coefficient names, specified as a cell array of character vectors, each containing the name of the corresponding term.
Data Types: cell
Coefficients
— Coefficient valuesThis property is readonly.
Coefficient values, specified as a table.
Coefficients
contains one row for each coefficient and these
columns:
Estimate
— Estimated
coefficient value
SE
— Standard error
of the estimate
tStat
— tstatistic for a test that the
coefficient is zero
pValue
— pvalue for the
tstatistic
Use anova
(only for a linear regression model) or
coefTest
to perform other tests on the coefficients. Use
coefCI
to find the confidence intervals of the coefficient
estimates.
To obtain any of these columns as a vector, index into the property
using dot notation. For example, obtain the estimated coefficient vector in the model
mdl
:
beta = mdl.Coefficients.Estimate
Data Types: table
NumCoefficients
— Number of model coefficientsThis property is readonly.
Number of model coefficients, specified as a positive integer.
NumCoefficients
includes coefficients that are set to zero when
the model terms are rank deficient.
Data Types: double
NumEstimatedCoefficients
— Number of estimated coefficientsThis property is readonly.
Number of estimated coefficients in the model, specified as a positive integer.
NumEstimatedCoefficients
does not include coefficients that are
set to zero when the model terms are rank deficient.
NumEstimatedCoefficients
is the degrees of freedom for
regression.
Data Types: double
Deviance
— Deviance of fitThis property is readonly.
Deviance of the fit, specified as a numeric value. The deviance is useful for comparing two models when one model is a special case of the other model. The difference between the deviance of the two models has a chisquare distribution with degrees of freedom equal to the difference in the number of estimated parameters between the two models. For more information, see Deviance.
Data Types: single
 double
DFE
— Degrees of freedom for errorThis property is readonly.
Degrees of freedom for the error (residuals), equal to the number of observations minus the number of estimated coefficients, specified as a positive integer.
Data Types: double
Dispersion
— Scale factor of variance of responseThis property is readonly.
Scale factor of the variance of the response, specified as a numeric scalar.
If the 'DispersionFlag'
namevalue pair argument of
fitglm
or stepwiseglm
is
true
, then the function estimates the
Dispersion
scale factor in computing the variance of the
response. The variance of the response equals the theoretical variance multiplied by the
scale factor.
For example, the variance function for the binomial distribution is
p(1–p)/n, where
p is the probability parameter and n is the
sample size parameter. If Dispersion
is near 1
,
the variance of the data appears to agree with the theoretical variance of the binomial
distribution. If Dispersion
is larger than 1
, the
data set is “overdispersed” relative to the binomial distribution.
Data Types: double
DispersionEstimated
— Flag to indicate use of dispersion scale factorThis property is readonly.
Flag to indicate whether fitglm
used the Dispersion
scale factor to compute standard errors for the coefficients in Coefficients.SE
, specified as a logical value. If DispersionEstimated
is false
, fitglm
used the theoretical value of the variance.
DispersionEstimated
can be false
only for the binomial and Poisson distributions.
Set DispersionEstimated
by setting the 'DispersionFlag'
namevalue pair argument of fitglm
or stepwiseglm
.
Data Types: logical
LogLikelihood
— LoglikelihoodThis property is readonly.
Loglikelihood of the model distribution at the response values, specified as a numeric value. The mean is fitted from the model, and other parameters are estimated as part of the model fit.
Data Types: single
 double
ModelCriterion
— Criterion for model comparisonThis property is readonly.
Criterion for model comparison, specified as a structure with these fields:
AIC
— Akaike information criterion.
AIC = –2*logL + 2*m
, where logL
is the
loglikelihood and m
is the number of estimated
parameters.
AICc
— Akaike information criterion corrected for
the sample size. AICc = AIC + (2*m*(m + 1))/(n – m – 1)
,
where n
is the number of observations.
BIC
— Bayesian information criterion.
BIC = –2*logL + m*log(n)
.
CAIC
— Consistent Akaike information criterion.
CAIC = –2*logL + m*(log(n) + 1)
.
Information criteria are model selection tools that you can use to compare multiple models fit to the same data. These criteria are likelihoodbased measures of model fit that include a penalty for complexity (specifically, the number of parameters). Different information criteria are distinguished by the form of the penalty.
When you compare multiple models, the model with the lowest information criterion value is the bestfitting model. The bestfitting model can vary depending on the criterion used for model comparison.
To obtain any of the criterion values as a scalar, index into the property using dot
notation. For example, obtain the AIC value aic
in the model
mdl
:
aic = mdl.ModelCriterion.AIC
Data Types: struct
Rsquared
— Rsquared value for modelThis property is readonly.
Rsquared value for the model, specified as a structure with five fields.
Field  Description  Equation 

Ordinary  Ordinary (unadjusted) Rsquared 
$${R}_{\text{Ordinary}}^{2}=1\frac{\text{SSE}}{\text{SST}}$$

Adjusted  Rsquared adjusted for the number of coefficients 
$${R}_{\text{Adjusted}}^{2}=1\frac{\text{SSE}}{\text{SST}}\cdot \frac{N1}{\text{DFE}}$$ N is the number of
observations ( 
LLR  Loglikelihood ratio 
$${R}_{\text{LLR}}^{2}=1\frac{L}{{L}_{0}}$$ L is the loglikelihood of
the fitted model ( 
Deviance  Deviance Rsquared 
$${R}_{\text{Deviance}}^{2}=1\frac{D}{{D}_{0}}$$ D is the deviance of the
fitted model ( 
AdjGeneralized  Adjusted generalized Rsquared 
$${R}_{\text{AdjGeneralized}}^{2}=\frac{1\mathrm{exp}\left(\frac{2\left({L}_{0}L\right)}{N}\right)}{1\mathrm{exp}\left(\frac{2{L}_{0}}{N}\right)}$$ R^{2}_{AdjGeneralized} is the Nagelkerke adjustment [2] to a formula proposed by Maddala [3], Cox and Snell [4], and Magee [5] for logistic regression models. 
To obtain any of these values as a scalar, index into the property using dot notation.
For example, to obtain the adjusted Rsquared value in the model mdl
,
enter:
r2 = mdl.Rsquared.Adjusted
Data Types: struct
SSE
— Sum of squared errorsThis property is readonly.
Sum of squared errors (residuals), specified as a numeric value.
Data Types: single
 double
SSR
— Regression sum of squaresThis property is readonly.
Regression sum of squares, specified as a numeric value. The regression sum of squares is equal to the sum of squared deviations of the fitted values from their mean.
Data Types: single
 double
SST
— Total sum of squaresThis property is readonly.
Total sum of squares, specified as a numeric value. The total sum of squares is equal
to the sum of squared deviations of the response vector y
from the
mean(y)
.
Data Types: single
 double
Distribution
— Generalized distribution informationThis property is readonly.
Generalized distribution information, specified as a structure with the fields described in this table.
Field  Description 

Name  Name of the distribution: 'normal' , 'binomial' ,
'poisson' , 'gamma' , or
'inverse gaussian' 
DevianceFunction  Function that computes the components of the deviance as a function of the fitted parameter values and the response values 
VarianceFunction  Function that computes the theoretical variance for the distribution as a function of the
fitted parameter values. When DispersionEstimated is
true , the software multiplies the variance
function by Dispersion in the computation of the
coefficient standard errors. 
Data Types: struct
Formula
— Model informationLinearFormula
objectThis property is readonly.
Model information, specified as a LinearFormula
object.
Display the formula of the fitted model mdl
using dot
notation:
mdl.Formula
Link
— Link functionThis property is readonly.
Link function, specified as a structure with the fields described in this table.
Field  Description 

Name  Name of the link function, specified as a character vector. If you specify the link function
using a function handle, then Name is
'' . 
Link  Function f that defines the link function, specified as a function handle 
Derivative  Derivative of f, specified as a function handle 
Inverse  Inverse of f, specified as a function handle 
The link function is a function f that links the distribution parameter μ to the fitted linear combination Xb of the predictors:
f(μ) = Xb.
Data Types: struct
NumObservations
— Number of observationsThis property is readonly.
Number of observations the fitting function used in fitting, specified
as a positive integer. NumObservations
is the
number of observations supplied in the original table, dataset,
or matrix, minus any excluded rows (set with the
'Exclude'
namevalue pair
argument) or rows with missing values.
Data Types: double
NumPredictors
— Number of predictor variablesThis property is readonly.
Number of predictor variables used to fit the model, specified as a positive integer.
Data Types: double
NumVariables
— Number of variablesThis property is readonly.
Number of variables in the input data, specified as a positive integer.
NumVariables
is the number of variables in the original table or
dataset, or the total number of columns in the predictor matrix and response
vector.
NumVariables
also includes any variables that are not used to fit
the model as predictors or as the response.
Data Types: double
PredictorNames
— Names of predictors used to fit modelThis property is readonly.
Names of predictors used to fit the model, specified as a cell array of character vectors.
Data Types: cell
ResponseName
— Response variable nameThis property is readonly.
Response variable name, specified as a character vector.
Data Types: char
VariableInfo
— Information about variablesThis property is readonly.
Information about variables contained in Variables
, specified as a
table with one row for each variable and the columns described in this table.
Column  Description 

Class  Variable class, specified as a cell array of character vectors, such
as 'double' and
'categorical' 
Range  Variable range, specified as a cell array of vectors

InModel  Indicator of which variables are in the fitted model, specified as a
logical vector. The value is true if the model
includes the variable. 
IsCategorical  Indicator of categorical variables, specified as a logical vector.
The value is true if the variable is
categorical. 
VariableInfo
also includes any variables that are not used to fit
the model as predictors or as the response.
Data Types: table
VariableNames
— Names of variablesThis property is readonly.
Names of variables, specified as a cell array of character vectors.
If the fit is based on a table or dataset, this property provides the names of the variables in the table or dataset.
If the fit is based on a predictor matrix and response vector,
VariableNames
contains the values specified by the
'VarNames'
namevalue pair argument of the fitting
method. The default value of 'VarNames'
is
{'x1','x2',...,'xn','y'}
.
VariableNames
also includes any variables that are not used to fit
the model as predictors or as the response.
Data Types: cell
coefCI  Confidence intervals of coefficient estimates of generalized linear regression model 
coefTest  Linear hypothesis test on generalized linear regression model coefficients 
devianceTest  Analysis of deviance for generalized linear regression model 
partialDependence  Compute partial dependence 
plotPartialDependence  Create partial dependence plot (PDP) and individual conditional expectation (ICE) plots 
plotSlice  Plot of slices through fitted generalized linear regression surface 
gather  Gather properties of machine learning model from GPU 
Fit a generalized linear regression model to data and reduce the size of a full, fitted model by discarding the sample data and some information related to the fitting process.
Load the largedata4reg
data set, which contains 15,000 observations and 45 predictor variables.
load largedata4reg
Fit a generalized linear regression model to the data using the first 15 predictor variables.
mdl = fitglm(X(:,1:15),Y);
Compact the model.
compactMdl = compact(mdl);
The compact model discards the original sample data and some information related to the fitting process, so it uses less memory than the full model.
Compare the size of the full model mdl
and the compact model compactMdl
.
vars = whos('compactMdl','mdl'); [vars(1).bytes,vars(2).bytes]
ans = 1×2
15517 4382500
The compact model consumes less memory than the full model.
Deviance is a generalization of the residual sum of squares. It measures the goodness of fit compared to a saturated model.
Deviance of a model M_{1} is twice the difference between the loglikelihood of the model M_{1} and the saturated model M_{s}. A saturated model is a model with the maximum number of parameters that you can estimate.
For example, if you have n observations (y_{i}, i = 1, 2, ..., n) with potentially different values for X_{i}^{T}β, then you can define a saturated model with n parameters. Let L(b,y) denote the maximum value of the likelihood function for a model with the parameters b. Then the deviance of the model M_{1} is
$$2\left(\mathrm{log}L\left({b}_{1},y\right)\mathrm{log}L\left({b}_{S},y\right)\right),$$
where b_{1} and b_{s} contain the estimated parameters for the model M_{1} and the saturated model, respectively. The deviance has a chisquare distribution with n – p degrees of freedom, where n is the number of parameters in the saturated model and p is the number of parameters in the model M_{1}.
Assume you have two different generalized linear regression models M_{1} and M_{2}, and M_{1} has a subset of the terms in M_{2}. You can assess the fit of the models by comparing the deviances D_{1} and D_{2} of the two models. The difference of the deviances is
$$\begin{array}{l}D={D}_{2}{D}_{1}=2\left(\mathrm{log}L\left({b}_{2},y\right)\mathrm{log}L\left({b}_{S},y\right)\right)+2\left(\mathrm{log}L\left({b}_{1},y\right)\mathrm{log}L\left({b}_{S},y\right)\right)\\ \text{\hspace{1em}}\text{\hspace{1em}}\text{\hspace{1em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}=2\left(\mathrm{log}L\left({b}_{2},y\right)\mathrm{log}L\left({b}_{1},y\right)\right).\end{array}$$
Asymptotically, the difference D has a chisquare distribution with degrees
of freedom v equal to the difference in the number of parameters
estimated in M_{1} and
M_{2}. You can obtain the
pvalue for this test by using
1 – chi2cdf(D,v)
.
Typically, you examine D using a model M_{2} with a constant term and no predictors. Therefore, D has a chisquare distribution with p – 1 degrees of freedom. If the dispersion is estimated, the difference divided by the estimated dispersion has an F distribution with p – 1 numerator degrees of freedom and n – p denominator degrees of freedom.
[1] McFadden, Daniel. "Conditional logit analysis of qualitative choice behavior." in Frontiers in Econometrics, edited by P. Zarembka,105–42. New York: Academic Press, 1974.
[2] Nagelkerke, N. J. D. "A Note on a General Definition of the Coefficient of Determination." Biometrika 78, no. 3 (1991): 691–92.
[3] Maddala, Gangadharrao S. LimitedDependent and Qualitative Variables in Econometrics. Econometric Society Monographs. New York, NY: Cambridge University Press, 1983.
[4] Cox, D. R., and E. J. Snell. Analysis of Binary Data. 2nd ed. Monographs on Statistics and Applied Probability 32. London; New York: Chapman and Hall, 1989.
[5] Magee, Lonnie. "R 2 Measures Based on Wald and Likelihood Ratio Joint Significance Tests." The American Statistician 44, no. 3 (August 1990): 250–53.
Usage notes and limitations:
When you fit a model by using fitglm
or stepwiseglm
, you cannot specify
Link
, Derivative
, and
Inverse
fields of the 'Link'
namevalue pair
argument as anonymous functions. That is, you cannot generate code using a
generalized linear model that was created using anonymous functions for links.
Instead, define functions for link components.
For more information, see Introduction to Code Generation.
Usage notes and limitations:
The following object functions fully support GPU arrays:
The following object functions support model objects fitted with GPU array input arguments:
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
You have a modified version of this example. Do you want to open this example with your edits?
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.