Create generalized linear regression model
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 two-way interactions model.
Load the hospital
dataset array.
load hospital dsa = hospital;
Specify the model using a formula that allows up to two-way 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^2-statistic vs. constant model: 5.07, p-value = 0.535
All of the p-values (under pValue
) are large. This means none of the coefficients are significant. The large p-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.3149e-36 x1 0.44413 0.052337 8.4858 2.1416e-17 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.5878e-09 x7 0.04316 0.06146 0.70225 0.48252 100 observations, 92 error degrees of freedom Dispersion: 1 Chi^2-statistic vs. constant model: 119, p-value = 1.55e-22
The p
-values of 2.14e-17, 0.00069, and 1.58e-09 indicate that the coefficients of the variables x1
, x3
, and x6
are statistically significant.
X
— Predictor variablesPredictor 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
y
— Response variableResponse variable, specified as a vector or matrix.
If 'Distribution'
is not 'binomial'
, then y
must be 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
. The data type must be single or double.
If 'Distribution'
is 'binomial'
, then y
can be an n-by-1 vector or n-by-2 matrix with counts in column 1 and BinomialSize
in column 2.
Data Types: single
| double
| logical
| categorical
modelspec
— Model specification'linear'
(default) | character vector or string scalar naming the model | t-by-(p + 1) terms matrix | character vector or string scalar formula in the form 'y ~ terms'
Model specification, specified as one of these values.
A character vector or string scalar naming the model.
Value | Model Type |
---|---|
'constant' |
Model contains only a constant (intercept) term. |
'linear' |
Model contains an intercept and linear term for each predictor. |
'interactions' |
Model contains an intercept, linear term for each predictor, and all products of pairs of distinct predictors (no squared terms). |
'purequadratic' |
Model contains an intercept term and linear and squared terms for each predictor. |
'quadratic' |
Model contains an intercept term, linear and squared terms for each predictor, and all products of pairs of distinct predictors. |
'poly |
Model is a polynomial with all terms up to degree i in the first predictor, degree j in the second predictor, and so on. Specify the maximum degree for each predictor by using numerals 0 though 9. The model contains interaction terms, but the degree of each interaction term does not exceed the maximum value of the specified degrees. For example, 'poly13' has an intercept and x1, x2, x22, x23, x1*x2, and x1*x22 terms, where x1 and x2 are the first and second predictors, respectively. |
A t-by-(p + 1) matrix, or a Terms Matrix, specifying terms in the model, where t is the number of terms and p is the number of predictor variables, and +1 accounts for the response variable. A terms matrix is convenient when the number of predictors is large and you want to generate the terms programmatically.
A character vector or string scalar Formula in the form
'y ~ terms'
,
where the terms
are in Wilkinson Notation. The variable names in the formula must be variable names in tbl
or variable names specified by Varnames
. Also, the variable names must be valid MATLAB identifiers.
The software determines the order of terms in a fitted model by using the order of terms in tbl
or X
. Therefore, the order of terms in the model can be different from the order of terms in the specified formula.
Example: 'quadratic'
Specify optional comma-separated pairs of Name,Value
arguments. Name
is the argument name and Value
is the corresponding value. Name
must appear inside 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 comma-separated pair consisting of 'BinomialSize'
and the variable name in tbl
, a numeric scalar, or a numeric 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 matrix with counts in column 1 and BinomialSize
in column 2.
Data Types: single
| double
| char
| string
B0
— Initial values for coefficient estimates
Initial values for the coefficient estimates, specified as a numeric vector. The default values are initial fitted values derived from the input data.
Data Types: single
| double
CategoricalVars
— Categorical variable listCategorical variable list, specified as the comma-separated pair consisting of 'CategoricalVars'
and either a string array or cell array of character vectors containing categorical variable names 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, by default, fitglm
treats all categorical values, logical values, character arrays, string arrays, and cell arrays of character vectors as categorical variables.
If data is in matrix X
, then the default value of 'CategoricalVars'
is an empty matrix []
. That is, no variable is categorical unless you specify it as categorical.
For example, you can specify the second and third variables out of six as categorical using either of the following:
Example: 'CategoricalVars',[2,3]
Example: 'CategoricalVars',logical([0 1 1 0 0 0])
Data Types: single
| double
| logical
| string
| cell
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 comma-separated pair consisting of 'DispersionFlag'
and one of the following.
true |
Estimate a dispersion parameter when computing standard errors. The estimated dispersion parameter value is the sum of squared Pearson residuals divided by the degrees of freedom for error (DFE). |
false |
Default. Use the theoretical value of 1 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 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'
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.
Desktop Basics - MATLAB & Simulink
Array Indexing - MATLAB & Simulink
Workspace Variables - MATLAB & Simulink
Text and Characters - MATLAB & Simulink
Calling Functions - MATLAB & Simulink
2-D and 3-D Plots - MATLAB & Simulink
Programming and Scripts - MATLAB & Simulink
Help and Documentation - MATLAB & Simulink
Creating, Concatenating, and Expanding Matrices - MATLAB & Simulink
Removing Rows or Columns from a Matrix
Reshaping and Rearranging Arrays
Add Title and Axis Labels to Chart
Change Color Scheme Using a Colormap
How Surface Plot Data Relates to a Colormap
How Image Data Relates to a Colormap
Time-Domain Response Data and Plots
Time-Domain Responses of Discrete-Time Model
Time-Domain Responses of MIMO Model
Time-Domain Responses of Multiple Models
Introduction: PID Controller Design
Introduction: Root Locus Controller Design
Introduction: Frequency Domain Methods for Controller Design
DC Motor Speed: PID Controller Design
DC Motor Position: PID Controller Design
Cruise Control: PID Controller Design
Suspension: Root Locus Controller Design
Aircraft Pitch: Root Locus Controller Design
Inverted Pendulum: Root Locus Controller Design
Get Started with Deep Network Designer
Create Simple Image Classification Network Using Deep Network Designer
Build Networks with Deep Network Designer
Classify Image Using GoogLeNet
Classify Webcam Images Using Deep Learning
Transfer Learning with Deep Network Designer
Train Deep Learning Network to Classify New Images
Deep Learning Processor Customization and IP Generation
Prototype Deep Learning Networks on FPGA
Deep Learning Processor Architecture
Deep Learning INT8 Quantization
Quantization of Deep Neural Networks
Custom Processor Configuration Workflow
Estimate Performance of Deep Learning Network by Using Custom Processor Configuration
Preprocess Images for Deep Learning
Preprocess Volumes for Deep Learning
Transfer Learning Using AlexNet
Time Series Forecasting Using Deep Learning
Create Simple Sequence Classification Network Using Deep Network Designer
Classify Image Using Pretrained Network
Train Classification Models in Classification Learner App
Train Regression Models in Regression Learner App
Explore the Random Number Generation UI
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
Discriminant Analysis Classification
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
Gaussian Process Regression Models
Gaussian Process Regression Models 2
Understanding Support Vector Machine Regression
Extract Voices from Music Signal
Align Signals with Different Start Times
Find a Signal in a Measurement
Extract Features of a Clock Signal
Filtering Data With Signal Processing Toolbox Software
Find Periodicity Using Frequency Analysis
Find and Track Ridges Using Reassigned Spectrogram
Classify ECG Signals Using Long Short-Term Memory Networks
Waveform Segmentation Using Deep Learning
Label Signal Attributes, Regions of Interest, and Points
Introduction to Streaming Signal Processing in MATLAB
Filter Frames of a Noisy Sine Wave Signal in MATLAB
Filter Frames of a Noisy Sine Wave Signal in Simulink
Lowpass Filter Design in MATLAB
Tunable Lowpass Filtering of Noisy Input in Simulink
Signal Processing Acceleration Through Code Generation
Signal Visualization and Measurements in MATLAB
Estimate the Power Spectrum in MATLAB
Design of Decimators and Interpolators
Multirate Filtering in MATLAB and Simulink