fitglm
statistics: mdl = fitglm (X, y)
statistics: mdl = fitglm (X, y, modelspec)
statistics: mdl = fitglm (tbl)
statistics: mdl = fitglm (tbl, modelspec)
statistics: mdl = fitglm (…, Name, Value)
Fit a generalized linear regression model.
mdl = fitglm (X, y) fits a generalized linear model
of the response vector y on the columns of the -by-
numeric predictor matrix X, and returns a GeneralizedLinearModel
object. mdl = fitglm (tbl) instead takes the predictors
and response from the table tbl (the last column is the response unless
overridden). By default the response is 'normal' with an identity
link, an intercept is included, and the model is additive in the predictors.
For the 'binomial' distribution y holds the number of
successes, and the number of trials is given either by the
'BinomialSize' pair or by passing y as an
-by- matrix whose first column holds the successes and
whose second holds the trials. The two forms describe the same model; when
both are given, the trials supplied with the response are used. A trial
count must be a positive integer, while a success count need not be whole.
modelspec selects the model terms. It is either a Wilkinson formula
string (e.g. 'y ~ x1 + x2*x3'), a keyword ('constant',
'linear', 'interactions', 'purequadratic',
'quadratic', or 'full'), or a terms matrix.
The following Name/Value pairs are accepted:
| Name | Value |
|---|---|
'Distribution' | the response distribution:
'normal' (default), 'binomial', 'poisson',
'gamma', or 'inverse gaussian'. |
'Link' | the link function. Defaults to the canonical link
of the distribution; accepts any link name understood by glmfit or a
numeric exponent for a power link. |
'Weights' | a vector of nonnegative observation weights. |
'Offset' | a vector added as a fixed term to the linear predictor. |
'BinomialSize' | for the 'binomial' distribution,
the number of trials (a scalar or a per-observation vector); y holds
the number of successes. Changed in 1.9.0: y was previously
read as the proportion of successes. Multiply an existing proportion by
the trials to keep its meaning. |
'Intercept' | a logical value (default true) whether
to include an intercept term. |
'DispersionFlag' | a logical value forcing the dispersion
parameter to be estimated (true) or held at 1 (false). |
'CategoricalVars' | predictors to treat as categorical (a logical vector, numeric indices, or a cell array of names). |
'Exclude' | observations to exclude from the fit (a logical vector or numeric indices). |
'VarNames' | a cell array of variable names (predictors followed by the response) for numeric X. |
'PredictorVars', 'ResponseVar' | for table input, the predictor and response variable names. |
Source Code: fitglm
A categorical predictor expands to indicator columns, one per level bar the
reference level, which the intercept carries. When the model has no
intercept, the first categorical predictor is given an indicator for
every one of its levels instead, so that its coefficients are the group
means; any further categorical predictor stays reference coded, which keeps
the design full rank. This differs from MATLAB, which omits the reference
level whether or not an intercept is present and so cannot fit the reference
group at all – for a three-level grouping variable g, MATLAB fits
y ~ g - 1 with two coefficients, predicts exactly 0 for every
observation in the omitted group, and reports a negative . This
implementation returns three coefficients, one per group.
See also: GeneralizedLinearModel, fitlm, glmfit, glmval, lassoglm
Source Code: fitglm
Poisson regression of counts on two predictors.
X = [0.1, 1.2; 0.4, 0.7; 1.1, 0.2; 1.5, 1.9; 0.3, 0.5; 1.8, 1.1; 0.9, 0.3]; y = [1; 0; 2; 3; 1; 4; 2]; mdl = fitglm (X, y, 'Distribution', 'poisson')
mdl =
Generalized linear regression model:
log(y) ~ 1 + x1 + x2
Distribution = Poisson, Link = log
Coefficients:
3x4 table
Estimate SE tStat pValue
__________ ________ _________ _________
(Intercept) -0.509786 0.708331 -0.7197 0.47171
x1 1.08685 0.567552 1.91498 0.0554956
x2 -0.0251014 0.515609 -0.048683 0.961172
Number of observations: 7, Error degrees of freedom: 4
Dispersion: 1
Deviance: 2.12771
Chi^2-statistic vs. constant model: 5.00447, p-value = 0.0819016
Logistic regression with an interaction, specified by a formula.
X = [0.1, 1.2; 0.4, 0.7; 1.1, 0.2; 1.5, 1.9; 0.3, 0.5; 1.8, 1.1; 0.9, 0.3];
y = [0; 0; 1; 1; 0; 1; 1];
tbl = array2table ([X, y], 'VariableNames', {'x1', 'x2', 'y'});
mdl = fitglm (tbl, 'y ~ x1 + x2 + x1:x2', 'Distribution', 'binomial')
warning: glmfit: maximum number of iterations has been reached.
warning: called from
glmfit at line 386 column 5
GeneralizedLinearModel at line 1072 column 8
fitglm at line 104 column 5
__eval_demo__ at line 84 column 9
__demo_notebook__ at line 43 column 3
__build_demos__ at line 79 column 7
function_texi2html at line 135 column 5
package_texi2html at line 336 column 9
warning: matrix singular to machine precision
warning: called from
GeneralizedLinearModel at line 1233 column 7
fitglm at line 104 column 5
__eval_demo__ at line 84 column 9
__demo_notebook__ at line 43 column 3
__build_demos__ at line 79 column 7
function_texi2html at line 135 column 5
package_texi2html at line 336 column 9
mdl =
Generalized linear regression model:
logit(y) ~ 1 + x1*x2
Distribution = Binomial, Link = logit
Coefficients:
4x4 table
Estimate SE tStat pValue
________ ___________ ____________ ________
(Intercept) -147.344 1.45637e+08 -1.01172e-06 0.999999
x1 251.364 1.46027e+08 1.72135e-06 0.999999
x2 -22.7796 1.57375e+08 -1.44747e-07 1
x1:x2 -39.1402 1.3322e+08 -2.93802e-07 1
Number of observations: 7, Error degrees of freedom: 3
Dispersion: 1
Deviance: 3.10862e-15
Chi^2-statistic vs. constant model: 9.56071, p-value = 0.0226942