Logistic regression create generalized linear regression model - MATLAB fitglm 2

fitglm

Exclude — Observations to exclude
logical or numeric index vector

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

Intercept — Indicator for constant term
true (default) | false

Indicator for the 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 or string scalar, 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 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(μ), where Φ is the cumulative distribution function of the standard normal distribution. μ = Φ(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 Canonical Link Function.

Example: 'Link','probit'

Data Types: char | string | single | double | struct

Options — Optimization options
statset('fitglm') (default) | structure

Optimization options, specified as a structure. This argument determines the control parameters for the iterative algorithm that fitglm uses.

Create the 'Options' value by using the function statset or by creating a structure array containing the fields and values described in this table.

Field Name Value Default Value
Display

Amount of information displayed by the algorithm

  • 'off' — Displays no information

  • 'final' — Displays the final output

 

'off'
MaxIter

Maximum number of iterations allowed, specified as a positive integer

100
TolX

Termination tolerance for the parameters, specified as a positive scalar

1e-6

You can also enter statset('fitglm') in the Command Window to see the names and default values of the fields that fitglm accepts in the 'Options' name-value argument.

Example: 'Options',statset('Display','final','MaxIter',1000) specifies to display the final information of the iterative algorithm results, and change the maximum number of iterations allowed to 1000.

Data Types: struct

Offset — Offset variable
[ ] (default) | numeric vector | character vector | string scalar

Offset variable in the fit, specified as the comma-separated pair consisting of 'Offset' and the variable name in tbl or a numeric vector with the same length as the response.

fitglm uses Offset as an additional predictor with a coefficient value fixed at 1. In other words, the formula for fitting is

f(μ) = Offset + X*b,

where f is the link function, μ is the mean response, and X*b is the linear combination of predictors X. The Offset predictor has 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 | string

PredictorVars — Predictor variables
string array | cell array of character vectors | logical or numeric index vector

Predictor variables to use in the fit, specified as the comma-separated pair consisting of 'PredictorVars' and either a string array or 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 string values or 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 | string | cell

ResponseVar — Response variable
last column in tbl (default) | character vector or string scalar containing variable name | logical or numeric index vector

Response variable to use in the fit, specified as the comma-separated pair consisting of 'ResponseVar' and either a character vector or string scalar 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 | string

VarNames — Names of variables
{'x1','x2',...,'xn','y'} (default) | string array | cell array of character vectors

Names of variables, specified as the comma-separated pair consisting of 'VarNames' and a string array or 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.

The variable names do not have to be valid MATLAB identifiers. However, if the names are not valid, you cannot use a formula when you fit or adjust a model; for example:

  • You cannot use a formula to specify the terms to add or remove when you use the addTerms function or the removeTerms function, respectively.

  • You cannot use a formula to specify the lower and upper bounds of the model when you use the step or stepwiseglm function with the name-value pair arguments 'Lower' and 'Upper', respectively.

 

Before specifying 'VarNames',varNames, you can verify the variable names in varNames by using the isvarname function. If the variable names are not valid, then you can convert them by using the matlab.lang.makeValidName function.

Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}

Data Types: string | cell

Weights — Observation weights
ones(n,1) (default) | n-by-1 vector of nonnegative scalar values

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

mdl — Generalized linear regression model
GeneralizedLinearModel object

Generalized linear regression model, specified as a GeneralizedLinearModel object created using fitglm or stepwiseglm.

More About

A terms matrix T is a t-by-(p + 1) matrix specifying terms in a model, where t is the number of terms, p is the number of predictor variables, and +1 accounts for the response variable. The value of T(i,j) is the exponent of variable j in term i.

For example, suppose that an input includes three predictor variables x1x2, and x3 and the response variable y in the order x1x2x3, and y. Each row of T represents one term:

  • [0 0 0 0] — Constant term or intercept

  • [0 1 0 0] — x2; equivalently, x1^0 * x2^1 * x3^0

  • [1 0 1 0] — x1*x3

  • [2 0 0 0] — x1^2

  • [0 1 2 0] — x2*(x3^2)

 

The 0 at the end of each term represents the response variable. In general, a column vector of zeros in a terms matrix represents the position of the response variable. If you have the predictor and response variables in a matrix and column vector, then you must include 0 for the response variable in the last column of each row.

A formula for model specification is a character vector or string scalar of the form 'y ~ terms'.

  • y is the response name.

  • terms represents the predictor terms in a model using Wilkinson notation.

To represent predictor and response variables, use the variable names of the table input tbl or the variable names specified by using VarNames. The default value of VarNames is {'x1','x2',...,'xn','y'}.

For example:

  • 'y ~ x1 + x2 + x3' specifies a three-variable linear model with intercept.

  • 'y ~ x1 + x2 + x3 – 1' specifies a three-variable linear model without intercept. Note that formulas include a constant (intercept) term by default. To exclude a constant term from the model, you must include –1 in the formula.

 

A formula includes a constant term unless you explicitly remove the term using –1.

Wilkinson notation describes the terms present in a model. The notation relates to the terms present in a model, not to the multipliers (coefficients) of those terms.

Wilkinson notation uses these symbols:

  • + means include the next variable.

  •  means do not include the next variable.

  • : defines an interaction, which is a product of terms.

  • * defines an interaction and all lower-order terms.

  • ^ raises the predictor to a power, exactly as in * repeated, so ^ includes lower-order terms as well.

  • () groups terms.

 

This table shows typical examples of Wilkinson notation.

Wilkinson Notation Terms in Standard Notation
1 Constant (intercept) term
x1^k, where k is a positive integer x1x12, ..., x1k
x1 + x2 x1x2
x1*x2 x1x2x1*x2
x1:x2 x1*x2 only
–x2 Do not include x2
x1*x2 + x3 x1x2x3x1*x2
x1 + x2 + x3 + x1:x2 x1x2x3x1*x2
x1*x2*x3 – x1:x2:x3 x1x2x3x1*x2x1*x3x2*x3
x1*(x2 + x3) x1x2x3x1*x2x1*x3

 

The default link function for a generalized linear model is the canonical link function.

Distribution Canonical 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

Tips

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

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

  • After training a model, you can generate C/C++ code that predicts responses for new data. Generating C/C++ code requires MATLAB Coder™

Algorithms

  • 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 using categories and reorder the categories by using reordercats to customize the reference level.  

    • 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 using dummyvar. 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 predictor X, if you specify all columns of dummyvar(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 considers NaN'' (empty character vector), "" (empty string), <missing>, and <undefined> values in tblX, and Y to be missing values. fitglm does not use observations with missing values in the fit. The ObservationInfo property of a fitted model indicates whether or not fitglm uses each observation in the fit.

Alternative Functionality

  • Use stepwiseglm to select a model specification automatically. Use stepaddTerms, or removeTerms to adjust a fitted model.

 

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.

Machine Learning in MATLAB

Train Classification Models in Classification Learner App

Train Regression Models in Regression Learner App

Distribution Plots

Explore the Random Number Generation UI

Design of Experiments

Machine Learning Models

Logistic regression

Logistic regression create generalized linear regression model - MATLAB fitglm 2

Support Vector Machines for Binary Classification

Support Vector Machines for Binary Classification 2

Support Vector Machines for Binary Classification 3

Support Vector Machines for Binary Classification 4

Support Vector Machines for Binary Classification 5

Assess Neural Network Classifier Performance

Naive Bayes Classification

ClassificationTree class

Discriminant Analysis Classification

Ensemble classifier

ClassificationTree class 2

Train Generalized Additive Model for Binary Classification

Train Generalized Additive Model for Binary Classification 2

Classification Using Nearest Neighbors

Classification Using Nearest Neighbors 2

Classification Using Nearest Neighbors 3

Classification Using Nearest Neighbors 4

Classification Using Nearest Neighbors 5

Linear Regression

Linear Regression 2

Linear Regression 3

Linear Regression 4

Nonlinear Regression

Nonlinear Regression 2

Visualizing Multivariate Data

Generalized Linear Models

Generalized Linear Models 2

RegressionTree class

RegressionTree class 2

Neural networks

Gaussian Process Regression Models

Gaussian Process Regression Models 2

Understanding Support Vector Machine Regression

Understanding Support Vector Machine Regression 2

RegressionEnsemble



matlab assignment help


matlab assignment help