fitglm
Create generalized linear regression model
Syntax
Description
specifies which table variable contains the response data.mdl
= fitglm(tbl
,ResponseVarName
)
returns a generalized linear regression model with additional options specified by one or
more mdl
= fitglm(___,Name,Value
)Name,Value
pair arguments.
For example, you can specify which variables are categorical, the distribution of the response variable, and the link function to use.
Examples
Input Arguments
Name-Value Arguments
Output Arguments
More About
Tips
The generalized linear model
mdl
is a standard linear model unless you specify otherwise with theDistribution
name-value pair.For methods such as
plotResiduals
ordevianceTest
, or properties of theGeneralizedLinearModel
object, seeGeneralizedLinearModel
.After training a model, you can generate C/C++ code that predicts responses for new data. Generating C/C++ code requires MATLAB Coder™. For details, see Introduction to Code Generation.
Algorithms
fitglm
calculates the model coefficients using iteratively reweighted least squares (IRLS). If you specify observation weights using theWeights
name-value argument,fitglm
multiplies the weights in the IRLS algorithm by the observation weights.fitglm
treats a categorical predictor as follows:A model with a categorical predictor that has L levels (categories) includes L – 1 indicator variables. The model uses the first category as a reference level, so it does not include the indicator variable for the reference level. If the data type of the categorical predictor is
categorical
, then you can check the order of categories by usingcategories
and reorder the categories by usingreordercats
to customize the reference level. For more details about creating indicator variables, see Automatic Creation of Dummy Variables.fitglm
treats the group of L – 1 indicator variables as a single variable. If you want to treat the indicator variables as distinct predictor variables, create indicator variables manually by usingdummyvar
. Then use the indicator variables, except the one corresponding to the reference level of the categorical variable, when you fit a model. For the categorical predictorX
, if you specify all columns ofdummyvar(X)
and an intercept term as predictors, then the design matrix becomes rank deficient.Interaction terms between a continuous predictor and a categorical predictor with L levels consist of the element-wise product of the L – 1 indicator variables with the continuous predictor.
Interaction terms between two categorical predictors with L and M levels consist of the (L – 1)*(M – 1) indicator variables to include all possible combinations of the two categorical predictor levels.
You cannot specify higher-order terms for a categorical predictor because the square of an indicator is equal to itself.
fitglm
considersNaN
,''
(empty character vector),""
(empty string),<missing>
, and<undefined>
values intbl
,X
, andY
to be missing values.fitglm
does not use observations with missing values in the fit. TheObservationInfo
property of a fitted model indicates whether or notfitglm
uses each observation in the fit.
Alternative Functionality
Use
stepwiseglm
to select a model specification automatically. Usestep
,addTerms
, orremoveTerms
to adjust a fitted model.
References
[1] Collett, D. Modeling Binary Data. New York: Chapman & Hall, 2002.
[2] Dobson, A. J. An Introduction to Generalized Linear Models. New York: Chapman & Hall, 1990.
[3] McCullagh, P., and J. A. Nelder. Generalized Linear Models. New York: Chapman & Hall, 1990.