MathWorks Machine Translation
The automated translation of this page is provided by a general purpose third party translator tool.
MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.
Create generalized linear regression model
mdl = fitglm(tbl)
mdl = fitglm(X,y)
mdl = fitglm(___,modelspec)
mdl = fitglm(___,Name,Value)
returns
a generalized linear 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.
Make a logistic binomial model of the probability of smoking as a function of age, weight, and sex, using a twoway interactions model.
Load the hospital
dataset array.
load hospital
dsa = hospital;
Specify the model using a formula that allows up to twoway interactions between the variables age, weight, and sex. Smoker is the response variable.
modelspec = 'Smoker ~ Age*Weight*Sex  Age:Weight:Sex';
Fit a logistic binomial model.
mdl = fitglm(dsa,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^2statistic vs. constant model: 5.07, pvalue = 0.535
All of the pvalues (under pValue) are large. This means none of the coefficients are significant. The large value for the test of the model, 0.535, indicates that this model might not differ statistically from a constant model.
Create sample data with 20 predictors, and Poisson response using just three of the predictors, plus a constant.
rng('default') % for reproducibility X = randn(100,7); mu = exp(X(:,[1 3 6])*[.4;.2;.3] + 1); y = poissrnd(mu);
Fit a generalized linear model using the Poisson distribution.
mdl = fitglm(X,y,'linear','Distribution','poisson')
mdl = Generalized linear regression model: log(y) ~ 1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 Distribution = Poisson Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ __________ (Intercept) 0.88723 0.070969 12.502 7.3149e36 x1 0.44413 0.052337 8.4858 2.1416e17 x2 0.0083388 0.056527 0.14752 0.88272 x3 0.21518 0.063416 3.3932 0.00069087 x4 0.058386 0.065503 0.89135 0.37274 x5 0.060824 0.073441 0.8282 0.40756 x6 0.34267 0.056778 6.0352 1.5878e09 x7 0.04316 0.06146 0.70225 0.48252 100 observations, 92 error degrees of freedom Dispersion: 1 Chi^2statistic vs. constant model: 119, pvalue = 1.55e22
The p
values of 2.14e17, 0.00069, and 1.58e09 indicate that the coefficients of the variables x1
, x3
, and x6are statistically significant.
tbl
— Input dataInput data, specified as a table or dataset array. When modelspec
is
a formula, it specifies the variables to be used as the predictors
and response. Otherwise, if you do not specify the predictor and response
variables, the last variable is the response variable and the others
are the predictor variables by default.
Predictor variables and response variables can be numeric, or any grouping variable type, such as logical or categorical (see Grouping Variables).
To set a different column as the response variable, use the ResponseVar
namevalue
pair argument. To use a subset of the columns as predictors, use the PredictorVars
namevalue
pair argument.
Data Types: single
 double
 logical
X
— Predictor variablesPredictor variables, specified as an nbyp matrix,
where n is the number of observations and p is
the number of predictor variables. Each column of X
represents
one variable, and each row represents one observation.
By default, there is a constant term in the model, unless you
explicitly remove it, so do not include a column of 1s in X
.
Data Types: single
 double
 logical
y
— Response variableResponse variable, specified as an nby1
vector, where n is the number of observations.
Each entry in y
is the response for the corresponding
row of X
.
modelspec
— Model specification'Y ~ terms'
Model specification, which is the starting model for stepwiseglm
,
specified as one of the following:
Character vector specifying the type of model.
Character Vector  Model Type 

'constant'  Model contains only a constant (intercept) term. 
'linear'  Model contains an intercept and linear terms for each predictor. 
'interactions'  Model contains an intercept, linear terms, and all products of pairs of distinct predictors (no squared terms). 
'purequadratic'  Model contains an intercept, linear terms, and squared terms. 
'quadratic'  Model contains an intercept, linear terms, interactions, and squared terms. 
'poly  Model is a polynomial with all terms up to degree i in
the first predictor, degree j in the second
predictor, etc. Use numerals 0 through 9 .
For example, 'poly2111' has a constant plus all
linear and product terms, and also contains terms with predictor 1
squared. 
tby(p+1) matrix, namely Terms Matrix, specifying terms to include in model, where t is the number of terms and p is the number of predictor variables, and plus one is for the response variable.
Character vector representing a Formula in the form
'
,Y
~ terms
'
where
the terms
are in Wilkinson Notation.
Example: 'quadratic'
Specify optional commaseparated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
'Distribution','normal','link','probit','Exclude',[23,59]
specifies
that the distribution of the response is normal, and instructs fitglm
to
use the probit link function and exclude the 23rd and 59th observations
from the fit.'BinomialSize'
— Number of trials for binomial distributionNumber of trials for binomial distribution, that is the sample
size, specified as the commaseparated pair consisting of a scalar
value or a vector of the same length as the response. This is the
parameter n
for the fitted binomial distribution. BinomialSize
applies
only when the Distribution
parameter is 'binomial'
.
If BinomialSize
is a scalar value, that means
all observations have the same number of trials.
As an alternative to BinomialSize
, you can
specify the response as a twocolumn vector with counts in column
1 and BinomialSize
in column 2.
Data Types: single
 double
'CategoricalVars'
— Categorical variablesCategorical variables in the fit, specified as the commaseparated
pair consisting of 'CategoricalVars'
and either
a cell array of character vectors of the names of the categorical
variables in the table or dataset array tbl
, or
a logical or numeric index vector indicating which columns are categorical.
If data is in a table or dataset array tbl
,
then the default is to treat all categorical or logical variables,
character arrays, or cell arrays of character vectors as categorical
variables.
If data is in matrix X
, then the
default value of this namevalue pair argument is an empty matrix []
.
That is, no variable is categorical unless you specify it.
For example, you can specify the observations 2 and 3 out of 6 as categorical using either of the following examples.
Example: 'CategoricalVars',[2,3]
Example: 'CategoricalVars',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
'DispersionFlag'
— Indicator to compute dispersion parameterfalse
for 'binomial'
and 'poisson'
distributions (default)  true
Indicator to compute dispersion parameter for 'binomial'
and 'poisson'
distributions,
specified as the commaseparated pair consisting of 'DispersionFlag'
and
one of the following.
true  Estimate a dispersion parameter when computing standard errors 
false  Default. Use the theoretical value when computing standard errors 
The fitting function always estimates the dispersion for other distributions.
Example: 'DispersionFlag',true
'Distribution'
— Distribution of the response variable'normal'
(default)  'binomial'
 'poisson'
 'gamma'
 'inverse gaussian'
Distribution of the response variable, specified as the commaseparated
pair consisting of 'Distribution'
and one of the
following.
'normal'  Normal distribution 
'binomial'  Binomial distribution 
'poisson'  Poisson distribution 
'gamma'  Gamma distribution 
'inverse gaussian'  Inverse Gaussian distribution 
Example: 'Distribution','gamma'
'Exclude'
— Observations to excludeObservations to exclude from the fit, specified as the commaseparated
pair consisting of 'Exclude'
and a logical or numeric
index vector indicating which observations to exclude from the fit.
For example, you can exclude observations 2 and 3 out of 6 using either of the following examples.
Example: 'Exclude',[2,3]
Example: 'Exclude',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
'Intercept'
— Indicator for constant termtrue
(default)  false
Indicator the for constant term (intercept) in the fit, specified
as the commaseparated pair consisting of 'Intercept'
and
either true
to include or false
to
remove the constant term from the model.
Use 'Intercept'
only when specifying the
model using a character vector, not a formula or matrix.
Example: 'Intercept',false
'Link'
— Link functionLink function to use in place of the canonical link function,
specified as the commaseparated pair consisting of 'Link'
and
one of the following.
Link Function Name  Link Function  Mean (Inverse) Function 

'identity'  f(μ) = μ  μ = Xb 
'log'  f(μ) = log(μ)  μ = exp(Xb) 
'logit'  f(μ) = log(μ/(1–μ))  μ = exp(Xb) / (1 + exp(Xb)) 
'probit'  f(μ) = Φ^{–1}(μ)  μ = Φ(Xb) 
'comploglog'  f(μ) = log(–log(1 – μ))  μ = 1 – exp(–exp(Xb)) 
'reciprocal'  f(μ) = 1/μ  μ = 1/(Xb) 
p (a number)  f(μ) = μ^{p}  μ = Xb^{1/p} 
S (a structure)with three fields. Each field holds a function handle that accepts a vector of inputs and returns a vector of the same size:
 f(μ) = S.Link (μ)  μ = S.Inverse (Xb) 
The link function defines the relationship f(μ) = X*b between the mean response μ and the linear combination of predictors X*b.
For more information on the canonical link functions, see Definitions
.
Example: 'Link','probit'
'Offset'
— Offset variablevector
 character vectorOffset variable in the fit, specified as the commaseparated
pair consisting of 'Offset'
and a vector or name
of a variable with the same length as the response.
fitglm
and stepwiseglm
use Offset
as
an additional predictor, with a coefficient value fixed at 1.0. In
other words, the formula for fitting is
μ ~ Offset + (terms involving real predictors)
with the Offset
predictor having coefficient 1
.
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.
Data Types: single
 double
 char
'PredictorVars'
— Predictor variablesPredictor variables to use in the fit, specified as the commaseparated
pair consisting of 'PredictorVars'
and either a
cell array of character vectors of the variable names in the table
or dataset array tbl
, or a logical or numeric index
vector indicating which columns are predictor variables.
The character vectors should be among the names in tbl
,
or the names you specify using the 'VarNames'
namevalue
pair argument.
The default is all variables in X
, or all
variables in tbl
except for ResponseVar
.
For example, you can specify the second and third variables as the predictor variables using either of the following examples.
Example: 'PredictorVars',[2,3]
Example: 'PredictorVars',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
 cell
'ResponseVar'
— Response variabletbl
(default)  character vector containing variable name  logical or numeric index vectorResponse variable to use in the fit, specified as the commaseparated
pair consisting of 'ResponseVar'
and either a character
vector containing the variable name in the table or dataset array tbl
,
or a logical or numeric index vector indicating which column is the
response variable. You typically need to use 'ResponseVar'
when
fitting a table or dataset array tbl
.
For example, you can specify the fourth variable, say yield
,
as the response out of six variables, in one of the following ways.
Example: 'ResponseVar','yield'
Example: 'ResponseVar',[4]
Example: 'ResponseVar',logical([0 0 0 1 0 0])
Data Types: single
 double
 logical
 char
'VarNames'
— Names of variables in fit{'x1','x2',...,'xn','y'}
(default)  cell array of character vectorsNames of variables in fit, specified as the commaseparated
pair consisting of 'VarNames'
and a cell array
of character vectors including the names for the columns of X
first,
and the name for the response variable y
last.
'VarNames'
is not applicable to variables
in a table or dataset array, because those variables already have
names.
For example, if in your data, horsepower, acceleration, and model year of the cars are the predictor variables, and miles per gallon (MPG) is the response variable, then you can name the variables as follows.
Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}
Data Types: cell
'Weights'
— Observation weightsones(n,1)
(default)  nby1 vector of nonnegative scalar valuesObservation weights, specified as the commaseparated pair consisting
of 'Weights'
and an nby1 vector
of nonnegative scalar values, where n is the number
of observations.
Data Types: single
 double
mdl
— Generalized linear modelGeneralizedLinearModel
objectGeneralized linear model representing a leastsquares fit of
the link of the response to the data, returned as a GeneralizedLinearModel
object.
For properties and methods of the generalized linear model object, mdl
,
see the GeneralizedLinearModel
class
page.
A terms matrix is a tby(p + 1) matrix specifying terms in a model, where t is the number of terms, p is the number of predictor variables, and plus one is for the response variable.
The value of T(i,j)
is the exponent of variable j
in
term i
. Suppose there are three predictor variables A
, B
,
and C
:
[0 0 0 0] % Constant term or intercept [0 1 0 0] % B; equivalently, A^0 * B^1 * C^0 [1 0 1 0] % A*C [2 0 0 0] % A^2 [0 1 2 0] % B*(C^2)
0
at
the end of each term represents the response variable. In general,If you have the variables in a table or dataset array,
then 0
must represent the response variable depending
on the position of the response variable. The following example illustrates
this.
Load the sample data and define the dataset array.
load hospital dsa = dataset(hospital.Sex,hospital.BloodPressure(:,1),hospital.Age,... hospital.Smoker,'VarNames',{'Sex','BloodPressure','Age','Smoker'});
Represent the linear model 'BloodPressure ~ 1 + Sex
+ Age + Smoker'
in a terms matrix. The response variable
is in the second column of the dataset array, so there must be a column
of 0s for the response variable in the second column of the terms
matrix.
T = [0 0 0 0;1 0 0 0;0 0 1 0;0 0 0 1]
T = 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1
Redefine the dataset array.
dsa = dataset(hospital.BloodPressure(:,1),hospital.Sex,hospital.Age,... hospital.Smoker,'VarNames',{'BloodPressure','Sex','Age','Smoker'});
Now, the response variable is the first term in the dataset
array. Specify the same linear model, 'BloodPressure ~ 1
+ Sex + Age + Smoker'
, using a terms matrix.
T = [0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]
T = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
If you have the predictor and response variables in
a matrix and column vector, then you must include 0
for
the response variable at the end of each term. The following example
illustrates this.
Load the sample data and define the matrix of predictors.
load carsmall
X = [Acceleration,Weight];
Specify the model 'MPG ~ Acceleration + Weight + Acceleration:Weight
+ Weight^2'
using a term matrix and fit the model to the
data. This model includes the main effect and twoway interaction
terms for the variables, Acceleration
and Weight
,
and a secondorder term for the variable, Weight
.
T = [0 0 0;1 0 0;0 1 0;1 1 0;0 2 0]
T = 0 0 0 1 0 0 0 1 0 1 1 0 0 2 0
Fit a linear model.
mdl = fitlm(X,MPG,T)
mdl = Linear regression model: y ~ 1 + x1*x2 + x2^2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 48.906 12.589 3.8847 0.00019665 x1 0.54418 0.57125 0.95261 0.34337 x2 0.012781 0.0060312 2.1192 0.036857 x1:x2 0.00010892 0.00017925 0.6076 0.545 x2^2 9.7518e07 7.5389e07 1.2935 0.19917 Number of observations: 94, Error degrees of freedom: 89 Root Mean Squared Error: 4.1 Rsquared: 0.751, Adjusted RSquared 0.739 Fstatistic vs. constant model: 67, pvalue = 4.99e26
Only the intercept and x2
term, which correspond
to the Weight
variable, are significant at the
5% significance level.
Now, perform a stepwise regression with a constant model as the starting model and a linear model with interactions as the upper model.
T = [0 0 0;1 0 0;0 1 0;1 1 0];
mdl = stepwiselm(X,MPG,[0 0 0],'upper',T)
1. Adding x2, FStat = 259.3087, pValue = 1.643351e28 mdl = Linear regression model: y ~ 1 + x2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 49.238 1.6411 30.002 2.7015e49 x2 0.0086119 0.0005348 16.103 1.6434e28 Number of observations: 94, Error degrees of freedom: 92 Root Mean Squared Error: 4.13 Rsquared: 0.738, Adjusted RSquared 0.735 Fstatistic vs. constant model: 259, pvalue = 1.64e28
The results of the stepwise regression are consistent with the
results of fitlm
in the previous step.
A formula for model specification is a character
vector of the form '
Y
~ terms
'
where
Y
is the response name.
terms
contains
Variable names
+
means include the next variable

means do not include the next
variable
:
defines an interaction, a product
of terms
*
defines an interaction and all lowerorder terms
^
raises the predictor to a power,
exactly as in *
repeated, so ^
includes
lower order terms as well
()
groups terms
Note:
Formulas include a constant (intercept) term by default. To
exclude a constant term from the model, include 
For example,
'Y ~ A + B + C'
means a threevariable
linear model with intercept.'Y ~ A + B +
C  1'
is a threevariable linear model without intercept.'Y ~ A + B + C + B^2'
is a threevariable
model with intercept and a B^2
term.'Y
~ A + B^2 + C'
is the same as the previous example because B^2
includes
a B
term.'Y ~ A + B +
C + A:B'
includes an A*B
term.'Y
~ A*B + C'
is the same as the previous example because A*B
= A + B + A:B
.'Y ~ A*B*C  A:B:C'
has
all interactions among A
, B
,
and C
, except the threeway interaction.'Y
~ A*(B + C + D)'
has all linear terms, plus products of A
with
each of the other variables.
Wilkinson notation describes the factors present in models. The notation relates to factors present in models, not to the multipliers (coefficients) of those factors.
Wilkinson Notation  Factors in Standard Notation 

1  Constant (intercept) term 
A^k , where k is a positive
integer  A , A^{2} ,
..., A^{k} 
A + B  A , B 
A*B  A , B , A*B 
A:B  A*B only 
B  Do not include B 
A*B + C  A , B , C , A*B 
A + B + C + A:B  A , B , C , A*B 
A*B*C  A:B:C  A , B , C , A*B , A*C , B*C 
A*(B + C)  A , B , C , A*B , A*C 
Statistics and Machine Learning Toolbox™ notation always includes a constant term
unless you explicitly remove the term using 1
.
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 

'normal'  'identity'  f(μ) = μ  μ = Xb 
'binomial'  'logit'  f(μ) = log(μ/(1–μ))  μ = exp(Xb) / (1 + exp(Xb)) 
'poisson'  'log'  f(μ) = log(μ)  μ = exp(Xb) 
'gamma'  1  f(μ) = 1/μ  μ = 1/(Xb) 
'inverse gaussian'  2  f(μ) = 1/μ^{2}  μ = (Xb)^{–1/2} 
The generalized linear model mdl
is
a standard linear model unless you specify otherwise with the Distribution
namevalue
pair.
For methods such as plotResiduals
or devianceTest
,
or properties of the GeneralizedLinearModel
object,
see GeneralizedLinearModel
.
Use stepwiseglm
to select a model specification
automatically. Use step
, addTerms
, or removeTerms
to
adjust a fitted model.
[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.
This function supports tall arrays for outofmemory data with some limitations.
If any input argument to fitglm
is
a tall array, then all of the other inputs must be tall arrays as
well. This includes nonempty variables supplied with the 'Weights'
, 'Exclude'
, 'Offset'
,
and 'BinomialSize'
namevalue pairs.
The default number of iterations is 5. You can change
the number of iterations using the 'Options'
namevalue
pair to pass in an options structure. Create an options structure
using statset
to specify a different value for MaxIter
.
For tall data, fitglm
returns
a CompactGeneralizedLinearModel
object that contains
most of the same properties as a GeneralizedLinearModel
object.
The main difference is that the compact object is sensitive to memory
requirements. The compact object does not include properties that
include the data, or that include an array of the same size as the
data. The compact object does not contain these GeneralizedLinearModel
properties:
Diagnostics
Fitted
Offset
ObservationInfo
ObservationNames
Residuals
Steps
Variables
You can compute the residuals directly from the compact
object returned by GLM = fitglm(X,Y)
using
RES = Y  predict(GLM,X); S = sqrt(GLM.SSE/GLM.DFE); histogram(RES,linspace(3*S,3*S,51))
For more information, see Tall Arrays (MATLAB).
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
You can also select a location from the following list: