# CompactLinearModel

Compact linear regression model

## Description

`CompactLinearModel` is a compact version of a full linear regression model object `LinearModel`. Because a compact model does not store the input data used to fit the model or information related to the fitting process, a `CompactLinearModel` object consumes less memory than a `LinearModel` object. You can still use a compact model to predict responses using new input data, but some `LinearModel` object functions do not work with a compact model.

## Creation

Create a `CompactLinearModel` model from a full, trained `LinearModel` model by using `compact`.

## 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, as given by `NumCoefficients`.

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

• `tStat`t-statistic for a two-sided test with the null hypothesis that the coefficient is zero

• `pValue`p-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

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`

Loglikelihood of response values, specified as a numeric value, based on the assumption that each response value follows a normal distribution. The mean of the normal distribution is the fitted (predicted) response value, and the variance is the `MSE`.

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`

Mean squared error (residuals), specified as a numeric value.

MSE = SSE / DFE,

where MSE is the mean squared error, SSE is the sum of squared errors, and DFE is the degrees of freedom.

Data Types: `single` | `double`

Root mean squared error (residuals), specified as a numeric value.

RMSE = sqrt(MSE),

where RMSE is the root mean squared error and MSE is the mean squared error.

Data Types: `single` | `double`

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

• `Ordinary` — Ordinary (unadjusted) R-squared

• `Adjusted` — R-squared adjusted for the number of coefficients

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`,

where `SST` is the total sum of squares, and `SSR` is the regression sum of squares.

For details, see Coefficient of Determination (R-Squared).

To obtain either 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`:

`r2 = mdl.Rsquared.Adjusted`

Data Types: `struct`

Sum of squared errors (residuals), specified as a numeric value. If the model was trained with observation weights, the sum of squares in the `SSE` calculation is the weighted sum of squares.

For a linear model with an intercept, the Pythagorean theorem implies

`SST = SSE + SSR`,

where `SST` is the total sum of squares, `SSE` is the sum of squared errors, and `SSR` is the regression sum of squares.

For more information on the calculation of `SST` for a robust linear model, see `SST`.

Data Types: `single` | `double`

Regression sum of squares, specified as a numeric value. `SSR` is equal to the sum of the squared deviations between the fitted values and the mean of the response. If the model was trained with observation weights, the sum of squares in the `SSR` calculation is the weighted sum of squares.

For a linear model with an intercept, the Pythagorean theorem implies

`SST = SSE + SSR`,

where `SST` is the total sum of squares, `SSE` is the sum of squared errors, and `SSR` is the regression sum of squares.

For more information on the calculation of `SST` for a robust linear model, see `SST`.

Data Types: `single` | `double`

Total sum of squares, specified as a numeric value. `SST` is equal to the sum of squared deviations of the response vector `y` from the `mean(y)`. If the model was trained with observation weights, the sum of squares in the `SST` calculation is the weighted sum of squares.

For a linear model with an intercept, the Pythagorean theorem implies

`SST = SSE + SSR`,

where `SST` is the total sum of squares, `SSE` is the sum of squared errors, and `SSR` is the regression sum of squares.

For a robust linear model, `SST` is not calculated as the sum of squared deviations of the response vector `y` from the `mean(y)`. It is calculated as ```SST = SSE + SSR```.

Data Types: `single` | `double`

### Fitting Method

Robust fit information, specified as a structure with the fields described in this table.

FieldDescription
`WgtFun`Robust weighting function, such as `'bisquare'` (see `'RobustOpts'`)
`Tune`Tuning constant. This field is empty (`[]`) if `WgtFun` is `'ols'` or if `WgtFun` is a function handle for a custom weight function with the default tuning constant 1.
`Weights`Vector of weights used in the final iteration of robust fit. This field is empty for a `CompactLinearModel` object.

This structure is empty unless you fit the model using robust regression.

Data Types: `struct`

### Input Data

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
`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

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

• Categorical variable — Vector of distinct variable values

`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`

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 linear regression model using one input for each predictor `predict` Predict responses of linear regression model `random` Simulate responses with random noise for linear regression model
 `anova` Analysis of variance for linear regression model `coefCI` Confidence intervals of coefficient estimates of linear regression model `coefTest` Linear hypothesis test on linear regression model coefficients `partialDependence` Compute partial dependence
 `plotEffects` Plot main effects of predictors in linear regression model `plotInteraction` Plot interaction effects of two predictors in linear regression model `plotPartialDependence` Create partial dependence plot (PDP) and individual conditional expectation (ICE) plots `plotSlice` Plot of slices through fitted linear regression surface
 `gather` Gather properties of Statistics and Machine Learning Toolbox object from GPU

## Examples

collapse all

Fit a linear regression model to data and reduce the size of a full, fitted linear regression 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 linear regression model to the data.

`mdl = fitlm(X,Y);`

Compact the model.

`compactMdl = compact(mdl);`

The compact model discards the original sample data and some information related to the fitting process.

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 81538 11409065 ```

The compact model consumes less memory than the full model.

## Version History

Introduced in R2016a