Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

GeneralizedLinearModel.fit

Class: GeneralizedLinearModel

Create generalized linear regression model

GeneralizedLinearModel.fit will be removed in a future release. Use fitglm instead.

Syntax

mdl = GeneralizedLinearModel.fit(tbl)
mdl = GeneralizedLinearModel.fit(X,y)
mdl = GeneralizedLinearModel.fit(...,modelspec)
mdl = GeneralizedLinearModel.fit(...,Name,Value)
mdl = GeneralizedLinearModel.fit(...,modelspec,Name,Value)

Description

mdl = GeneralizedLinearModel.fit(tbl) creates a generalized linear model of a table or dataset array tbl.

mdl = GeneralizedLinearModel.fit(X,y) creates a generalized linear model of the responses y to a data matrix X.

mdl = GeneralizedLinearModel.fit(...,modelspec) creates a generalized linear model as specified by modelspec.

mdl = GeneralizedLinearModel.fit(...,Name,Value) or mdl = GeneralizedLinearModel.fit(...,modelspec,Name,Value) creates a generalized linear model with additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Input 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 name-value pair argument. To use a subset of the columns as predictors, use the PredictorVars name-value pair argument.

Data Types: single | double | logical

Predictor variables, specified as an n-by-p 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

Response variable, specified as an n-by-1 vector, where n is the number of observations. Each entry in y is the response for the corresponding row of X.

Model specification, which is the starting model for stepwiseglm, specified as one of the following:

  • Character vector specifying the type of model.

    Character VectorModel 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.
    'polyijk'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.

  • t-by-(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'

Name-Value Pair Arguments

Specify optional comma-separated 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.

expand all

Number of trials for binomial distribution, that is the sample size, specified as the comma-separated 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 two-column vector with counts in column 1 and BinomialSize in column 2.

Data Types: single | double

Categorical variables in the fit, specified as the comma-separated 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 name-value 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

Indicator to compute dispersion parameter for 'binomial' and 'poisson' distributions, specified as the comma-separated pair consisting of 'DispersionFlag' and one of the following.

trueEstimate a dispersion parameter when computing standard errors
falseDefault. Use the theoretical value when computing standard errors

The fitting function always estimates the dispersion for other distributions.

Example: 'DispersionFlag',true

Distribution of the response variable, specified as the comma-separated 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'

Observations to exclude from the fit, specified as the comma-separated 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

Indicator the for constant term (intercept) in the fit, specified as the comma-separated 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 function to use in place of the canonical link function, specified as the comma-separated pair consisting of 'Link' and one of the following.

Link Function NameLink FunctionMean (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μ = Xb1/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:
  • S.Link — The link function

  • S.Inverse — The inverse link function

  • S.Derivative — The derivative of the link function

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 variable in the fit, specified as the comma-separated 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

Predictor variables to use in the fit, specified as the comma-separated 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' name-value 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

Response variable to use in the fit, specified as the comma-separated 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

Names of variables in fit, specified as the comma-separated 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

Observation weights, specified as the comma-separated pair consisting of 'Weights' and an n-by-1 vector of nonnegative scalar values, where n is the number of observations.

Data Types: single | double

Output Arguments

expand all

Generalized linear model representing a least-squares 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.

Examples

expand all

Fit a logistic regression model of probability of smoking as a function of age, weight, and sex, using a two-way interactions model.

Load the 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 $p$-value indicates the model might not differ statistically from a constant.

Definitions

expand all

Tips

  • The generalized linear model mdl is a standard linear model unless you specify otherwise with the Distribution name-value pair.

  • For other methods such as devianceTest, or properties of the GeneralizedLinearModel object, see GeneralizedLinearModel.

Alternatives

You can also construct a generalized linear model using fitglm.

Use stepwiseglm to select a model specification automatically. Use step, addTerms, or removeTerms 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.

Was this topic helpful?