# CompactGeneralizedLinearModel

Compact generalized linear regression model class

## Description

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 use still use a compact model to predict responses using new input data, but some GeneralizedLinearModel object functions do not work with a compact model.

## Creation

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 in-memory tables and arrays.

## Properties

expand all

### Coefficient Estimates

Covariance matrix of coefficient estimates, specified as a p-by-p 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

Coefficient names, specified as a cell array of character vectors, each containing the name of the corresponding term.

Data Types: cell

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

• tStatt-statistic for a test that the coefficient is zero

• pValuep-value for the t-statistic

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

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

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

### Summary Statistics

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 chi-square 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

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

Scale factor of the variance of the response, specified as a numeric scalar.

If the 'DispersionFlag' name-value 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

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' name-value pair argument of fitglm or stepwiseglm.

Data Types: logical

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

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 likelihood-based 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 best-fitting model. The best-fitting 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

R-squared value for the model, specified as a structure with five fields:

• Ordinary — Ordinary (unadjusted) R-squared

• LLR — Loglikelihood ratio

• Deviance — Deviance

The R-squared value is the proportion of the total sum of squares explained by the model. The ordinary R-squared value relates to the SSR and SST properties:

Rsquared = SSR/SST

To obtain any of these values as a scalar, index into the property using dot notation. For example, obtain the adjusted R-squared value in the model mdl:

Data Types: struct

Sum of squared errors (residuals), specified as a numeric value.

Data Types: single | double

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

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

### Input Data

Generalized distribution information, specified as a structure with the fields described in this table.

FieldDescription
NameName of the distribution: 'normal', 'binomial', 'poisson', 'gamma', or 'inverse gaussian'
DevianceFunctionFunction that computes the components of the deviance as a function of the fitted parameter values and the response values
VarianceFunctionFunction that computes the theoretical variance for the distribution as a function of the fitted parameter values. When DispersionEstimated is true, Dispersion multiplies the variance function in the computation of the coefficient standard errors.

Data Types: struct

Model information, specified as a LinearFormula object.

Display the formula of the fitted model mdl using dot notation:

mdl.Formula

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' name-value pair argument) or rows with missing values.

Data Types: double

Number of predictor variables used to fit the model, specified as a positive integer.

Data Types: double

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

Names of predictors used to fit the model, specified as a cell array of character vectors.

Data Types: cell

Response variable name, specified as a character vector.

Data Types: char

Information about variables contained in Variables, specified as a table with one row for each variable and the columns described in this table.

ColumnDescription
ClassVariable class, specified as a cell array of character vectors, such as 'double' and 'categorical'
Range

Variable range, specified as a cell array of vectors

• Continuous variable — Two-element vector [min,max], the minimum and maximum values

• Categorical variable — Vector of distinct variable values

InModelIndicator of which variables are in the fitted model, specified as a logical vector. The value is true if the model includes the variable.
IsCategoricalIndicator 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

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' name-value 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

## Object Functions

expand all

 feval Predict responses of generalized linear regression model using one input for each predictor predict Predict responses of generalized linear regression model random Simulate responses with random noise for generalized linear regression model
 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 linear or generalized linear regression model

## Examples

collapse all

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.

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

15518     4382502

The compact model consumes less memory than the full model.

expand all