Categories &

Functions List

Class Definition: CompactLinearModel

statistics: CompactLinearModel

Compact linear regression model

The CompactLinearModel class stores a fitted linear regression model without the training data. A CompactLinearModel object is returned by the compact method of a LinearModel object, and retains everything needed to inspect, predict from, and run inference on the fit, while discarding the observations and per-observation diagnostics that a LinearModel object carries. This makes a CompactLinearModel object smaller to store than the LinearModel it was compacted from.

The properties of a CompactLinearModel object fall into four groups:

GroupProperties
Coefficient estimatesCoefficients (a table of estimates, standard errors, t-statistics, and p-values for each term), CoefficientCovariance, CoefficientNames, and the coefficient counts NumCoefficients and NumEstimatedCoefficients.
Summary statistics of the fitDFE, MSE, RMSE, Rsquared (ordinary and adjusted), SSE, SSR, SST, LogLikelihood, and ModelCriterion (AIC, BIC, etc.).
Fitting method informationRobust, which records the weighting function and tuning constant used when the model is fit by robust regression, and is empty for an ordinary least squares fit.
Input data propertiesFormula, NumObservations, NumPredictors, NumVariables, PredictorNames, ResponseName, VariableInfo, and VariableNames.

Source Code: CompactLinearModel

Because the training data is discarded, a CompactLinearModel object has no Fitted, Residuals, Diagnostics, or ObservationInfo properties, and none of its methods refit the model. Once created, the following methods are available on a CompactLinearModel object:

MethodDescription
predictPredict responses at new predictor values given in a matrix or table. Can also return pointwise or simultaneous confidence intervals alongside the point predictions.
fevalPredict responses given predictors as separate scalar or vector arguments (one per predictor variable) instead of a single matrix, so a CompactLinearModel object can be evaluated the same way as a plain function handle. Returns point predictions only.
randomSimulate new response values at new predictor locations by adding independent Gaussian noise, drawn from the estimated error variance MSE, to the fitted response.
coefCIReturn Wald confidence intervals for every fitted coefficient at a chosen significance level (default 0.05).
coefTestTest a linear hypothesis on the fitted coefficients. With no arguments, tests the overall model F-test that all non-intercept coefficients are zero; a custom hypothesis can be given as a contrast matrix and, if needed, right-hand-side values. Returns the p-value, and optionally the F-statistic and its numerator degrees of freedom.
plotEffectsPlot the estimated main effect and 95% confidence interval of each predictor, evaluated between its observed minimum and maximum with all other predictors held at their observed means.
plotInteractionPlot the main and conditional effects of two predictors, or the adjusted response as a function of one predictor for several fixed values of the other, to visualize whether the two predictors interact.
anovaAnalysis of variance for the fitted model. Type 3 raises an error on a model missing a lower-order relative of one of its terms, since a CompactLinearModel object has no data to refit with.

Source Code: CompactLinearModel

Create a CompactLinearModel object by using the compact method of a fitted LinearModel object, or the class constructor directly.

See also: LinearModel, compact

Source Code: CompactLinearModel

The CompactLinearModel class contains the following properties:

A p-by-p numeric matrix of covariance values for the coefficient estimates, where p is the number of coefficients in the fitted model as given by NumCoefficients. This property is read-only.

A cell array of character vectors, each containing the name of the corresponding model term (e.g., '(Intercept)', 'x1', 'x1:x2'). This property is read-only.

A table with one row for each coefficient and four columns:

  • Estimate - estimated coefficient value
  • SE - standard error of the estimate
  • tStat - t-statistic for a two-sided test
  • pValue - p-value for the t-statistic

Coefficients that are dropped due to rank deficiency have Estimate = 0, SE = 0, tStat = NaN, pValue = NaN. This property is read-only.

A positive integer giving the total number of coefficients in the fitted model, including any coefficients set to zero because the model terms are rank deficient. This property is read-only.

A positive integer giving the number of coefficients actually estimated, i.e., not set to zero due to rank deficiency. NumEstimatedCoefficients equals the degrees of freedom for regression. This property is read-only.

A positive integer equal to the number of observations minus the number of estimated coefficients: DFE = NumObservations - NumEstimatedCoefficients. This property is read-only.

A scalar numeric value equal to the log-likelihood of the response values, assuming each response is normally distributed with mean equal to the fitted value and variance equal to SSE/n (the MLE variance estimate). This property is read-only.

A structure with four fields:

  • AIC - Akaike information criterion: -2 × logL + 2 × m
  • AICc - AIC corrected for sample size: AIC + (2×m×(m+1))/(n-m-1)
  • BIC - Bayesian information criterion: -2 × logL + m × log(n)
  • CAIC - Consistent AIC: -2 × logL + m × (log(n) + 1)

Here logL is LogLikelihood, m is NumEstimatedCoefficients, and n is NumObservations. This property is read-only.

A scalar numeric value equal to SSE / DFE, where SSE is the sum of squared errors and DFE is the degrees of freedom for error. This property is read-only.

A scalar numeric value equal to sqrt(MSE). This property is read-only.

A structure with two fields:

  • Ordinary - coefficient of determination: R^2 = SSR / SST
  • Adjusted - adjusted R^2 that accounts for the number of coefficients in the model

This property is read-only.

A scalar numeric value equal to the sum of squared residuals. For a model with an intercept, SST = SSE + SSR. For weighted fits, this is the weighted sum of squares. This property is read-only.

A scalar numeric value equal to the sum of squared deviations of the fitted values from the mean of the response. For a model with an intercept, SST = SSE + SSR. For weighted fits, this is the weighted sum of squares. This property is read-only.

A scalar numeric value equal to the sum of squared deviations of the response from its mean. For a model with an intercept, SST = SSE + SSR. For a robust fit, SST = SSE + SSR rather than the deviation from the mean. For weighted fits, this is the weighted sum of squares. This property is read-only.

A structure with three fields:

  • WgtFun - robust weighting function name, e.g. 'bisquare'
  • Tune - tuning constant; empty if WgtFun is 'ols' or a function handle with the default tuning constant
  • Weights - vector of final iteration weights; always empty for a CompactLinearModel object

This structure is empty unless the model was fit using robust regression. This property is read-only.

A structure representing the model formula with fields including ResponseName, LinearPredictor, PredictorNames, TermNames, HasIntercept, Terms (the terms matrix), and InModel. This property is read-only.

A positive integer giving the number of observations actually used in fitting the original model. Rows with missing values and rows excluded via the 'Exclude' name-value argument are not counted. This property is read-only.

A positive integer giving the number of predictor variables used to fit the model. This property is read-only.

A positive integer giving the total number of variables in the input data used to fit the original model, counting predictors, the response, and any unused columns. This property is read-only.

A cell array of character vectors containing the names of the predictor variables used to fit the model. This property is read-only.

A character vector containing the name of the response variable. This property is read-only.

A table with one row per variable including any unused variables, and four columns:

  • Class - variable class as a character vector, e.g. 'double' or 'categorical'
  • Range - for continuous variables, a two-element vector [min, max]; for categorical variables, a vector of the distinct values
  • InModel - logical; true if the variable is in the fitted model
  • IsCategorical - logical; true if the variable is categorical

This property is read-only.

A cell array of character vectors containing the names of all variables used to fit the original model, including predictors, the response, and unused variables. This property is read-only.

The CompactLinearModel class offers the following public methods:

CompactLinearModel: cmdl = CompactLinearModel ()
CompactLinearModel: cmdl = CompactLinearModel (mdl)

cmdl = CompactLinearModel () returns a CompactLinearModel object with all properties empty.

cmdl = CompactLinearModel (mdl) copies the coefficient estimates, fit statistics, and input data description from the fitted LinearModel object mdl into a new CompactLinearModel object cmdl, discarding the training data, per-observation diagnostics, and stepwise fitting history. If mdl was fit using robust regression, the Weights field of cmdl.Robust is emptied, although the rest of the Robust structure is retained.

The usual way to obtain a CompactLinearModel object is to call the compact method on an already-fitted LinearModel object, rather than calling this constructor directly.

See also: LinearModel, compact

CompactLinearModel: ci = coefCI (mdl)
CompactLinearModel: ci = coefCI (mdl, alpha)

ci = coefCI (mdl) returns 95% confidence intervals for every coefficient in mdl using a default significance level of 0.05.

ci = coefCI (mdl, alpha) uses the significance level alpha, a scalar in [0, 1]. The resulting intervals have coverage 100(1-\alpha)\%. Setting alpha to 0 produces intervals of infinite width; setting it to 1 collapses each interval to the corresponding point estimate.

The output ci is a k-by-2 numeric matrix where k = mdl.NumCoefficients. Row j contains the interval for the j-th coefficient, whose name is stored in mdl.CoefficientNames{j}. Column 1 is the lower bound and column 2 is the upper bound. The midpoint of each interval equals the corresponding point estimate in mdl.Coefficients.Estimate.

Intervals use the Wald method: b_j \pm t_{(1-\alpha/2,\,\mathrm{DFE})}\,\mathrm{SE}(b_j), where b_j is the coefficient estimate, \mathrm{SE}(b_j) is its standard error from mdl.Coefficients.SE, and the critical value is the 1-\alpha/2 quantile of the t-distribution with mdl.DFE degrees of freedom. In rank-deficient models, aliased coefficients have \mathrm{SE} = 0 and their row in ci is [0, 0].

CompactLinearModel: p = coefTest (mdl)
CompactLinearModel: p = coefTest (mdl, H)
CompactLinearModel: p = coefTest (mdl, H, C)
CompactLinearModel: [p, F] = coefTest (…)
CompactLinearModel: [p, F, r] = coefTest (…)

coefTest tests whether one or more linear combinations of the fitted coefficients equal specified constants. Each linear combination is encoded as a row of the contrast matrix H, and the right-hand side is given by C.

p = coefTest (mdl) performs the overall model F-test: it tests the joint null hypothesis that every coefficient except the intercept is zero. The returned p-value matches the F-statistic line printed at the bottom of the model display.

p = coefTest (mdl, H) tests the null hypothesis H \beta = 0, where \beta is the full coefficient vector of length k = mdl.NumCoefficients. H must be a full-rank numeric matrix with k columns; each row specifies one linear constraint. To test a single coefficient, use a row vector with a 1 in that coefficient’s position and zeros elsewhere; the resulting F-statistic equals the square of the corresponding t-statistic in mdl.Coefficients. To test a categorical predictor that expands to multiple indicator columns, include one row per indicator in H.

p = coefTest (mdl, H, C) tests H \beta = C instead of zero. C must be a numeric vector with the same number of elements as rows of H; both row and column vectors are accepted.

The second output F is the value of the F-statistic: F = (H\hat{\beta} - C)^\prime (H V H^\prime)^{-1} (H\hat{\beta} - C) / r, where V is mdl.CoefficientCovariance and r is the number of rows of H. The third output r is that numerator degrees of freedom; the denominator degrees of freedom is mdl.DFE. Under the null hypothesis F follows an F(r, \mathrm{DFE}) distribution and the p-value is the upper-tail probability. When H is rank-deficient but contains no NaN, both p and F are returned as NaN without an error.

CompactLinearModel: ypred = predict (mdl, Xnew)
CompactLinearModel: [ypred, yci] = predict (mdl, Xnew)
CompactLinearModel: [ypred, yci] = predict (mdl, Xnew, Name, Value)

ypred = predict (mdl, Xnew) returns the fitted response values at the new predictor locations in Xnew. Xnew can be a numeric matrix with one column per predictor in the same order as the training data, or a table whose column names match mdl.PredictorNames. Rows containing NaN are returned as NaN without error. Unlike LinearModel, Xnew is required: a CompactLinearModel object does not store the training data, so there is no default to fall back on when it is omitted.

[ypred, yci] = predict (…) also returns yci, an n-by-2 matrix of confidence bounds where column 1 is the lower bound and column 2 is the upper bound. By default these are 95% pointwise confidence intervals on the mean response.

Name-Value pair arguments:

NameValue
'Alpha'Significance level for the confidence interval, specified as a scalar in [0,1]. The interval has coverage 100(1-\alpha)\%. Default is 0.05, giving a 95% interval.
'Prediction'Type of interval to compute. "curve" (default) gives a confidence interval on the mean response f(x). "observation" gives a wider prediction interval for a single future observation y = f(x) + \varepsilon, which accounts for both estimation uncertainty and irreducible noise; it adds mdl.MSE to the variance before computing the half-width.
'Simultaneous'Logical flag controlling whether the bounds are simultaneous or pointwise. When true, Scheff'{e}’s method is used so the entire predicted curve lies within the band with 100(1-\alpha)\% confidence; these bands are always wider than pointwise ones. Default is false.
CompactLinearModel: ysim = random (mdl, Xnew)

ysim = random (mdl, Xnew) computes the fitted response at each row of Xnew and then adds independent Gaussian noise to each value. The noise is drawn from N(0, \sigma^2) where \sigma^2 is the estimated error variance mdl.MSE (mean squared error of the fit). The result is a column vector of the same length as the number of rows in Xnew.

Xnew is required and must be non-empty. It can be a numeric matrix with one column per predictor in the same order as the training data, or a table whose column names match mdl.PredictorNames.

Because the added noise is drawn freshly on every call, two calls with the same Xnew will generally produce different output. To get reproducible results, set the random seed with rand ('state', s) before calling random.

For deterministic predictions without noise, use predict or feval. predict also provides confidence intervals on the mean response.

CompactLinearModel: ypred = feval (mdl, X)
CompactLinearModel: ypred = feval (mdl, x1, x2, …, xp)

ypred = feval (mdl, X) accepts a single numeric matrix X with one column per predictor in the same order as the training data, or a table whose column names match mdl.PredictorNames. The output is an n-by-1 column vector. Rows that contain NaN in any predictor column are returned as NaN.

ypred = feval (mdl, x1, x2, …, xp) accepts exactly mdl.NumPredictors separate arguments, one per predictor variable. All non-scalar arguments must have the same size; a scalar argument is broadcast to that size automatically. The output shape follows the shape of the non-scalar inputs: column vector inputs give a column vector output, row vector inputs give a row vector output, and all-scalar inputs give a scalar. This form is convenient when predictor data is already stored in separate vectors rather than a combined matrix.

feval gives the same numerical predictions as predict but does not support confidence intervals. Use predict when you also need bounds on the response. Because a CompactLinearModel object behaves like a function through feval, it can be passed directly to routines that accept a function handle, such as fminsearch or integral.

CompactLinearModel: plotEffects (mdl)
CompactLinearModel: plotEffects (ax, mdl)
CompactLinearModel: h = plotEffects (…)

plotEffects (mdl) creates a horizontal dot-and-line plot with one row per predictor. Each dot shows the estimated main effect on the response from changing that predictor from its minimum observed value to its maximum observed value, while holding all other predictors fixed at their observed means. A horizontal line through each dot shows the 95% confidence interval for that effect.

The main effect for predictor xs is defined as g(x_{s,\max}) - g(x_{s,\min}), where the adjusted response function g evaluates the model at the specified value of xs with all other predictors set to their observed means. For numeric predictors the sign of the effect can be positive or negative depending on the direction of the relationship. Because a CompactLinearModel does not retain the training data, these values come from a summary computed once when the model was fitted, rather than recomputed from the original observations.

plotEffects (ax, mdl) creates the plot in the axes object ax instead of the current axes returned by gca.

h = plotEffects (…) returns a vector of p+1 graphics handles where p is the number of predictors. h(1) is the line object containing the effect estimate markers (one circle per predictor, plotted as a single line object with XData of length p and YData = 1:p). h(j+1) is the confidence interval line for predictor j, with XData = [ci_lo, ci_hi] and YData = [j, j].

The y-axis tick labels follow the format 'varname: min to max', showing the predictor name and the minimum and maximum observed values used to compute the effect.

CompactLinearModel: plotInteraction (mdl, var1, var2)
CompactLinearModel: plotInteraction (mdl, var1, var2, ptype)
CompactLinearModel: plotInteraction (ax, …)
CompactLinearModel: h = plotInteraction (…)

plotInteraction (mdl, var1, var2) creates a plot of the main effects of var1 and var2 together with their conditional effects, with horizontal lines through each effect value indicating its 95% confidence interval. var1 and var2 are each a character vector or string naming a variable in mdl.VariableNames, or a positive integer indexing into mdl.VariableNames; neither may name the response variable, and they must be different variables.

The main effect of a predictor is the change in the adjusted response between the two predictor values that produce the minimum and maximum adjusted response, with the other predictor averaged over its own observed values row by row. For a numeric predictor these two values are its observed minimum and maximum; for a categorical predictor every level is evaluated and the levels producing the minimum and maximum adjusted response are used, so the effect is always nonnegative.

The conditional effect of var1 is its effect recomputed with var2 additionally held fixed at each of a small set of conditioning values, and likewise the conditional effect of var2 holds var1 fixed. The conditioning values are the observed minimum, mean of the minimum and maximum, and maximum for a numeric predictor, or every level for a categorical predictor. When the main effect and conditional effect points for a predictor do not align vertically, the model exhibits an interaction between var1 and var2. Because a CompactLinearModel does not retain the training data, these values come from a summary computed once when the model was fitted, rather than recomputed from the original observations.

plotInteraction (mdl, var1, var2, ptype) selects the plot type. ptype is 'effects' (default), as described above, or 'predictions', which instead plots the adjusted response as a function of var2 for each conditioning value of var1 held fixed, evaluated over 101 equally spaced points spanning the observed range of var2 when var2 is numeric, or at each level of var2 when it is categorical.

plotInteraction (ax, …) plots into the axes object ax instead of the current axes returned by gca.

h = plotInteraction (…) returns a vector of line handles. When ptype is 'effects', h(1) is the marker line through the two main effect points, h(2) and h(3) are the confidence interval lines for the main effects of var1 and var2, and the remaining entries are the conditional effect points and their confidence intervals, tagged 'conditional1' for var1 and 'conditional2' for var2. The main effect line objects are tagged 'main'. When ptype is 'predictions', each entry in h corresponds to one adjusted response curve, one per conditioning value of var1.

CompactLinearModel: tbl = anova (mdl)
CompactLinearModel: tbl = anova (mdl, anovatype)
CompactLinearModel: tbl = anova (mdl, "components", sstype)

anova (mdl) returns a table tbl with component ANOVA statistics for every term in mdl except the constant term, computed with hierarchical ("h") sums of squares. Each row gives SumSq, DF, MeanSq, F, and pValue for the corresponding term; the trailing Error row gives SumSq = mdl.SSE, DF = mdl.DFE, MeanSq = mdl.MSE, and NaN for F and pValue.

MATLAB reports F = 1 and pValue = 0.5 on that Error row instead. Those are not results: the row’s F is its own MeanSq divided by itself, so it is 1 for every data set, and the pValue follows. MATLAB does not use them consistently either, reporting NaN for the same quantity on the Residual row of its summary table. This implementation reports NaN in both places.

Every statistic is computed from mdl.Coefficients and mdl.CoefficientCovariance alone; a CompactLinearModel never refits, because it does not retain the training data.

anova (mdl, anovatype) selects "components" (default) or "summary". For "summary", tbl always contains rows Total, Model, and Residual, and additionally . Linear and . Nonlinear whenever mdl contains an interaction term or a continuous term of degree greater than 1. Total reports mdl.SST with DF = NumObservations - 1; Model reports mdl.SSR with DF = NumCoefficients - HasIntercept; Residual reports mdl.SSE with DF = mdl.DFE. Unlike anova on a LinearModel, tbl never contains . Lack of fit or . Pure error rows, since identifying observations with identical predictor values requires the training data that a CompactLinearModel does not retain.

anova (mdl, "components", sstype) selects the sum of squares used for the component table: 1 (sequential, reduction from adding each term in formula order), 2 (reduction from adding the term to a model containing every term that does not contain it), "h" (default; as Type 2, but a higher-degree term in the same continuous variable, such as a squared term, is also treated as containing the lower-degree term), or 3 (reduction from adding the term to a model containing every other term, with categorical predictors recoded using sum-to-zero deviation contrasts instead of mdl’s reference-level coding). sstype is ignored when anovatype is "summary". If mdl is missing a lower-order relative of one of its terms (e.g. an interaction without one of its main effects, or a categorical predictor fit without an intercept), type 3 raises an error, since a CompactLinearModel has no data to refit with.

See also: LinearModel, coefTest