Generalized linear regression model class
mdl = stepwiseglm(
a generalized linear model of a table or dataset array
or of the responses
y to a data matrix
with unimportant predictors excluded. For details, see
Deviance— Deviance of the fit
Deviance of the fit, stored as a numeric value. Deviance is useful for comparing two models when one is a special case of the other. 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 on deviance, see Deviance.
Diagnostics— Diagnostic information
Diagnostic information for the model, stored as a table. Diagnostics
can help identify outliers and influential observations.
the following fields:
|Diagonal elements of ||Leverage indicates to what extent the predicted value for an
observation is determined by the observed value for that observation.
A value close to |
|Cook's measure of scaled change in fitted values|
|Projection matrix to compute fitted from observed responses|
All of these quantities are computed on the scale of the linear predictor. So, for example, in the equation that defines the hat matrix,
Yfit = glm.Fitted.LinearPredictor Y = glm.Fitted.LinearPredictor + glm.Residuals.LinearPredictor
Dispersion— Scale factor of the variance of the response
Scale factor of the variance of the response, stored as a structure.
the variance function for the distribution.
For example, the variance function for the binomial distribution
where p is the probability parameter and n is
the sample size parameter. If
Dispersion is near
the variance of the data appears to agree with the theoretical variance
of the binomial distribution. If
1, the data are “overdispersed”
relative to the binomial distribution.
DispersionEstimated— Flag to indicate use of dispersion scale factor
Flag to indicate whether
Dispersion scale factor to compute standard
errors for the coefficients in
stored as a logical value. If
the theoretical value of the variance.
DispersionEstimated can be
DispersionEstimated by setting
DispersionFlag name-value pair in
Distribution— Generalized distribution information
Generalized distribution information, stored as a structure with the following fields relating to the generalized distribution:
|Name of the distribution, one of |
|Function that computes the components of the deviance as a function of the fitted parameter values and the response values.|
|Function that computes the theoretical variance for the distribution
as a function of the fitted parameter values. When |
Fitted— Fitted response values based on input data
Fitted (predicted) values based on the input data, stored as a table with one row for each observation and the following columns.
|Predicted values on the scale of the response.|
|Predicted values on the scale of the linear predictor. These
are the same as the link function applied to the |
|Fitted probabilities (this column is included only with the binomial distribution).|
To obtain any of the columns as a vector, index into the property
using dot notation. For example, in the model
f of fitted values on the response scale
f = mdl.Fitted.Response
predict to compute predictions for other
predictor values, or to compute confidence bounds on
Link— Link function
Link function, stored as a structure with the following fields:
|Name of the link function, or |
|The function that defines f, a function handle.|
|Derivative of f, a function handle.|
|Inverse of f, a function handle.|
The link is a function f that links the distribution parameter μ to the fitted linear combination Xb of the predictors:
f(μ) = Xb.
Offset— Offset variable
Offset variable, stored as a numeric vector with the same length
as the number of rows in the data.
Offset is passed
stepwiseglm in the
pair. The fitting function used
Offset as a predictor
variable, but with the coefficient set to exactly
In other words, the formula for fitting was
~ Offset + (terms
involving real predictors)
Offset predictor having coefficient
For example, consider a Poisson regression model. Suppose the
number of counts is known for theoretical reasons to be proportional
to a predictor
A. By using the log link function
and by specifying
log(A) as an offset, you can
force the model to satisfy this theoretical constraint.
Residuals— Residuals for fitted model
Residuals for the fitted model, stored as a table with one row for each observation and the following columns.
|Observed minus fitted values.|
|Residuals on the linear predictor scale, equal to the adjusted response value minus the fitted linear combination of the predictors.|
|Raw residuals divided by the estimated standard deviation of the response.|
|Residuals defined on transformed data with the transformation chosen to remove skewness.|
|Residuals based on the contribution of each observation to the deviance.|
To obtain any of these columns as a vector, index into the property
using dot notation. For example, in a model
the ordinary raw residual vector
r = mdl.Residuals.Raw
Rows not used in the fit because of missing values (in
Rows not used in the fit because of excluded values (in
NaN values, with the following exceptions:
raw contains the difference between
the observed and predicted values.
standardized is the residual, standardized
in the usual way.
studentized matches the standardized
values because this residual is not used in the estimate of the residual
|addTerms||Add terms to generalized linear model|
|compact||Compact generalized linear regression model|
|fit||Create generalized linear regression model|
|plotDiagnostics||Plot diagnostics of generalized linear regression model|
|plotResiduals||Plot residuals of generalized linear regression model|
|removeTerms||Remove terms from generalized linear model|
|step||Improve generalized linear regression model by adding or removing terms|
|stepwise||Create generalized linear regression model by stepwise regression|
|coefCI||Confidence intervals of coefficient estimates of generalized linear model|
|coefTest||Linear hypothesis test on generalized linear regression model coefficients|
|devianceTest||Analysis of deviance|
|disp||Display generalized linear regression model|
|feval||Evaluate generalized linear regression model prediction|
|plotSlice||Plot of slices through fitted generalized linear regression surface|
|predict||Predict response of generalized linear regression model|
|random||Simulate responses for generalized linear regression model|
Value. To learn how value classes affect copy operations, see Copying Objects (MATLAB).
Fit a logistic regression model of probability of smoking as a function of age, weight, and sex, using a two-way interactions model.
hospital dataset array.
load hospital ds = hospital; % just to use the ds name
Specify the model using a formula that allows up to two-way interactions.
modelspec = 'Smoker ~ Age*Weight*Sex - Age:Weight:Sex';
Create the generalized linear model.
mdl = fitglm(ds,modelspec,'Distribution','binomial')
mdl = Generalized linear regression model: logit(Smoker) ~ 1 + Sex*Age + Sex*Weight + Age*Weight Distribution = Binomial Estimated Coefficients: Estimate SE tStat pValue ___________ _________ ________ _______ (Intercept) -6.0492 19.749 -0.3063 0.75938 Sex_Male -2.2859 12.424 -0.18399 0.85402 Age 0.11691 0.50977 0.22934 0.81861 Weight 0.031109 0.15208 0.20455 0.83792 Sex_Male:Age 0.020734 0.20681 0.10025 0.92014 Sex_Male:Weight 0.01216 0.053168 0.22871 0.8191 Age:Weight -0.00071959 0.0038964 -0.18468 0.85348 100 observations, 93 error degrees of freedom Dispersion: 1 Chi^2-statistic vs. constant model: 5.07, p-value = 0.535
The large -value indicates the model might not differ statistically from a constant.
Create response data using just three of 20 predictors, and create a generalized linear model stepwise to see if it uses just the correct predictors.
Create data with 20 predictors, and Poisson response using just three of the predictors, plus a constant.
rng default % for reproducibility X = randn(100,20); mu = exp(X(:,[5 10 15])*[.4;.2;.3] + 1); y = poissrnd(mu);
Fit a generalized linear model using the Poisson distribution.
mdl = stepwiseglm(X,y,... 'constant','upper','linear','Distribution','poisson')
1. Adding x5, Deviance = 134.439, Chi2Stat = 52.24814, PValue = 4.891229e-13 2. Adding x15, Deviance = 106.285, Chi2Stat = 28.15393, PValue = 1.1204e-07 3. Adding x10, Deviance = 95.0207, Chi2Stat = 11.2644, PValue = 0.000790094 mdl = Generalized linear regression model: log(y) ~ 1 + x5 + x10 + x15 Distribution = Poisson Estimated Coefficients: Estimate SE tStat pValue ________ ________ ______ __________ (Intercept) 1.0115 0.064275 15.737 8.4217e-56 x5 0.39508 0.066665 5.9263 3.0977e-09 x10 0.18863 0.05534 3.4085 0.0006532 x15 0.29295 0.053269 5.4995 3.8089e-08 100 observations, 96 error degrees of freedom Dispersion: 1 Chi^2-statistic vs. constant model: 91.7, p-value = 9.61e-20
The default link function for a generalized linear model is the canonical link function.
Canonical Link Functions for Generalized Linear Models
|Distribution||Link Function Name||Link Function||Mean (Inverse) Function|
|f(μ) = μ||μ = Xb|
|f(μ) = log(μ/(1–μ))||μ = exp(Xb) / (1 + exp(Xb))|
|f(μ) = log(μ)||μ = exp(Xb)|
|f(μ) = 1/μ||μ = 1/(Xb)|
|f(μ) = 1/μ2||μ = (Xb)–1/2|
The hat matrix H is defined in terms of the data matrix X and a diagonal weight matrix W:
H = X(XTWX)–1XTWT.
W has diagonal elements wi:
g is the link function mapping yi to xib.
is the derivative of the link function g.
V is the variance function.
μi is the ith mean.
The diagonal elements Hii satisfy
where n is the number of observations (rows of X), and p is the number of coefficients in the regression model.
The leverage of observation i is the value of the ith diagonal term, hii, of the hat matrix H. Because the sum of the leverage values is p (the number of coefficients in the regression model), an observation i can be considered to be an outlier if its leverage substantially exceeds p/n, where n is the number of observations.
The Cook’s distance Di of observation i is
is the dispersion parameter (estimated or theoretical).
ei is the linear predictor residual, , where
g is the link function.
yi is the observed response.
xi is the observation.
is the estimated coefficient vector.
p is the number of coefficients in the regression model.
hii is the ith diagonal element of the Hat Matrix H.
Deviance of a model M1 is twice the difference between the loglikelihood of that model and the saturated model, MS. The saturated model is the model with the maximum number of parameters that can be estimated. For example, if there are n observations yi, i = 1, 2, ..., n, with potentially different values for XiTβ, 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. Then the deviance of model M1 is
where b1 are the estimated parameters for model M1 and bS are the estimated parameters for the saturated model. The deviance has a chi-square 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 model M1.
If M1 and M2 are two different generalized linear models, then the fit of the models can be assessed by comparing the deviances D1 and D2 of these models. The difference of the deviances is
Asymptotically, this difference has a chi-square distribution
with degrees of freedom v equal to the number of
parameters that are estimated in one model but fixed (typically at
0) in the other. That is, it is equal to the difference in the number
of parameters estimated in M1 and M2.
You can get the p-value for this test using
1 - chi2cdf(D,V), where D = D2 – D1.
Usage notes and limitations:
Code generation does
not support categorical predictors. You cannot supply training data in a table that contains at
least one categorical predictor, and you cannot use the
name-value pair argument. To dummy-code variables that you want
treated as categorical, preprocess the categorical data by using
dummyvar before fitting the model..
Inverse fields of the
name-value pair argument cannot be anonymous functions. That is, you
cannot generate code using a generalized linear model that was created
using anonymous functions for links. Instead, declare functions for link